Skip to content

Commit

Permalink
feat: Support cache key separator. (mgtv-tech#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
caihao committed Nov 11, 2024
1 parent be0bef1 commit 6806dcf
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ type Options struct {
remote remote.Remote // Remote is distributed cache, such as Redis.
local local.Local // Local is memory cache, such as FreeCache.
codec string // Value encoding and decoding method. Default is "msgpack.Name". You can also customize it.
separatorDisable bool // Disable separator for cache key. Default is false. If true, the cache key will not be split into multiple parts.
separatorDisabled bool // Disable separator for cache key. Default is false. If true, the cache key will not be split into multiple parts.
separator string // Separator for cache key. Default is ":".
errNotFound error // Error to return for cache miss. Used to prevent cache penetration.
remoteExpiry time.Duration // Remote cache ttl, Default is 1 hour.
Expand Down
2 changes: 1 addition & 1 deletion README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ name string // 缓存名称,用于日志标
remote remote.Remote // remote 是分布式缓存,例如 Redis。
local local.Local // local 是内存缓存,例如 FreeCache。
codec string // value的编码和解码方法。默认为 "msgpack.Name"。也可以自定义。
separatorDisable bool // Disable separator for cache key. Default is false. If true, the cache key will not be split into multiple parts.
separatorDisabled bool // Disable separator for cache key. Default is false. If true, the cache key will not be split into multiple parts.
separator string // Separator for cache key. Default is ":".
errNotFound error // 缓存未命中时返回的错误。用于防止缓存穿透(即缓存空对象)。
remoteExpiry time.Duration // 远程缓存 TTL,默认为 1 小时。
Expand Down
8 changes: 4 additions & 4 deletions cacheopt.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type (
remote remote.Remote // Remote is distributed cache, such as Redis.
local local.Local // Local is memory cache, such as FreeCache.
codec string // Value encoding and decoding method. Default is "msgpack.Name". You can also customize it.
disableSeparator bool // Disable separator for cache key. Default is false. If true, the cache key will not be split into multiple parts.
separatorDisabled bool // Disable separator for cache key. Default is false. If true, the cache key will not be split into multiple parts.
separator string // Separator for cache key. Default is ":".
errNotFound error // Error to return for cache miss. Used to prevent cache penetration.
remoteExpiry time.Duration // Remote cache ttl, Default is 1 hour.
Expand Down Expand Up @@ -109,7 +109,7 @@ func newOptions(opts ...Option) Options {
if o.eventChBufSize <= 0 {
o.eventChBufSize = defaultEventChBufSize
}
if o.separator == "" && !o.disableSeparator {
if o.separator == "" && !o.separatorDisabled {
o.separator = defaultSeparator
}
return o
Expand Down Expand Up @@ -219,13 +219,13 @@ func WithEventHandler(eventHandler func(event *Event)) Option {

func WithSeparatorDisable(separatorDisable bool) Option {
return func(o *Options) {
o.disableSeparator = separatorDisable
o.separatorDisabled = separatorDisable
}
}

func WithSeparator(separator string) Option {
return func(o *Options) {
if !o.disableSeparator {
if !o.separatorDisabled {
o.separator = separator
}
}
Expand Down
2 changes: 1 addition & 1 deletion cacheopt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestCacheOptions(t *testing.T) {
assert.Equal(t, defaultEventChBufSize, o.eventChBufSize)
assert.Nil(t, o.eventHandler)
assert.Equal(t, defaultSeparator, o.separator)
assert.Equal(t, false, o.disableSeparator)
assert.Equal(t, false, o.separatorDisabled)
})

t.Run("with name", func(t *testing.T) {
Expand Down

0 comments on commit 6806dcf

Please sign in to comment.