feat: local cache

This commit is contained in:
withchao
2024-01-12 15:41:05 +08:00
parent 006e4a1e93
commit 45064ae5ca
28 changed files with 268 additions and 386 deletions
+27 -22
View File
@@ -4,11 +4,11 @@ import (
"context"
"github.com/openimsdk/open-im-server/v3/pkg/common/localcache/link"
"github.com/openimsdk/open-im-server/v3/pkg/common/localcache/local"
opt "github.com/openimsdk/open-im-server/v3/pkg/common/localcache/option"
lopt "github.com/openimsdk/open-im-server/v3/pkg/common/localcache/option"
)
type Cache[V any] interface {
Get(ctx context.Context, key string, fetch func(ctx context.Context) (V, error), opts ...*opt.Option) (V, error)
Get(ctx context.Context, key string, fetch func(ctx context.Context) (V, error), opts ...*lopt.Option) (V, error)
Del(ctx context.Context, key ...string)
}
@@ -17,12 +17,17 @@ func New[V any](opts ...Option) Cache[V] {
for _, o := range opts {
o(opt)
}
c := &cache[V]{opt: opt, link: link.New(opt.localSlotNum)}
c.local = local.NewCache[V](opt.localSlotNum, opt.localSlotSize, opt.localSuccessTTL, opt.localFailedTTL, opt.target, c.onEvict)
go func() {
c.opt.delCh(c.del)
}()
return c
c := cache[V]{opt: opt}
if opt.localSlotNum > 0 && opt.localSlotSize > 0 {
c.local = local.NewCache[V](opt.localSlotNum, opt.localSlotSize, opt.localSuccessTTL, opt.localFailedTTL, opt.target, c.onEvict)
go func() {
c.opt.delCh(c.del)
}()
if opt.linkSlotNum > 0 {
c.link = link.New(opt.linkSlotNum)
}
}
return &c
}
type cache[V any] struct {
@@ -32,10 +37,12 @@ type cache[V any] struct {
}
func (c *cache[V]) onEvict(key string, value V) {
lks := c.link.Del(key)
for k := range lks {
if key != k { // prevent deadlock
c.local.Del(k)
if c.link != nil {
lks := c.link.Del(key)
for k := range lks {
if key != k { // prevent deadlock
c.local.Del(k)
}
}
}
}
@@ -50,16 +57,14 @@ func (c *cache[V]) del(key ...string) {
}
}
func (c *cache[V]) Get(ctx context.Context, key string, fetch func(ctx context.Context) (V, error), opts ...*opt.Option) (V, error) {
enable := c.opt.enable
if len(opts) > 0 && opts[0].Enable != nil {
enable = *opts[0].Enable
}
if enable {
if len(opts) > 0 && len(opts[0].Link) > 0 {
c.link.Link(key, opts[0].Link...)
}
func (c *cache[V]) Get(ctx context.Context, key string, fetch func(ctx context.Context) (V, error), opts ...*lopt.Option) (V, error) {
if c.local != nil {
return c.local.Get(key, func() (V, error) {
if c.link != nil {
for _, o := range opts {
c.link.Link(key, o.Link...)
}
}
return fetch(ctx)
})
} else {
@@ -74,7 +79,7 @@ func (c *cache[V]) Del(ctx context.Context, key ...string) {
for _, fn := range c.opt.delFn {
fn(ctx, key...)
}
if c.opt.enable {
if c.local != nil {
c.del(key...)
}
}