Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ecache: 分组的装饰器实现 #31

Closed
flycash opened this issue Dec 29, 2023 · 1 comment
Closed

ecache: 分组的装饰器实现 #31

flycash opened this issue Dec 29, 2023 · 1 comment
Labels
help wanted Extra attention is needed 简单

Comments

@flycash
Copy link
Contributor

flycash commented Dec 29, 2023

仅限中文

使用场景

在大部分的中小公司里面,所有的业务都是共用一个 Redis 之类的集群。

不同的业务之间通过 key 的前缀来区别。

例如说:

  • app1:biz1:key1
  • app2:bizX:keyn

而在同一个应用内部,要求所有使用 cache 的地方都要符合前缀约定,是一个比较难解决的事情,大对数时候只能依赖于测试或者 review 代码来发现是否正确使用了前缀。

行业分析

如果你知道有框架提供了类似功能,可以在这里描述,并且给出文档或者例子

可行方案

如果你有设计思路或者解决方案,请在这里提供。你可以提供多个方案,并且给出自己的选择

而实际上我们可以通过提供一个新的装饰器实现来达成这个目标。

type NamespaceCache struct {
    c Cache
    namespace string
}
// 以 Get 为例子
func (c *NamespaceCache) Get(key string) {
    return c.c.Get(c.namespace + key)
}

这里有一个值得讨论的地方,即分隔符的问题。也就是按照道理来说,类似于 app1:biz1:key1,对应的 Namespace 是app1,然后分隔符是 :。

但是这里我并打算采用下面这种设计:

type NamespaceCache struct {
   c Cache
// 类似于 app1
   namespace string
// 类似于 :
   seperator string
}

func (c *NamespaceCache) Get(key string) {
    return c.c.Get(c.namespace + seperator + key)
}

道理也很简单,因为这纯纯是一个没有必要的设计。在引入分隔符的概念时候,就要考虑不同的人使用不同的分隔符,所以需要 seperator 字段,并且要考虑提供默认的分隔符。

而实际上,我们可以统一认为,类似于 app1: 自身就构成了一个 namespace(命名空间),这样可以将分隔符之类的问题交给用户来解决。

换句话来说,我认为这个实现没有必要维持一个分隔符的概念。

其它

任何你觉得有利于解决问题的补充说明

你使用的是 ecache 哪个版本?

你设置的的 Go 环境?

上传 go env 的结果

@flycash flycash added help wanted Extra attention is needed 简单 labels Dec 29, 2023
@flycash
Copy link
Contributor Author

flycash commented Jan 1, 2024

#33

@flycash flycash closed this as completed Jan 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed 简单
Projects
Status: Done
Development

No branches or pull requests

1 participant