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...)
}
}
+12 -10
View File
@@ -8,9 +8,9 @@ import (
func defaultOption() *option {
return &option{
enable: true,
localSlotNum: 500,
localSlotSize: 20000,
linkSlotNum: 500,
localSuccessTTL: time.Minute,
localFailedTTL: time.Second * 5,
delFn: make([]func(ctx context.Context, key ...string), 0, 2),
@@ -19,9 +19,9 @@ func defaultOption() *option {
}
type option struct {
enable bool
localSlotNum int
localSlotSize int
linkSlotNum int
localSuccessTTL time.Duration
localFailedTTL time.Duration
delFn []func(ctx context.Context, key ...string)
@@ -31,25 +31,27 @@ type option struct {
type Option func(o *option)
func WithDisable() Option {
func WithLocalDisable() Option {
return WithLinkSlotNum(0)
}
func WithLinkDisable() Option {
return WithLinkSlotNum(0)
}
func WithLinkSlotNum(linkSlotNum int) Option {
return func(o *option) {
o.enable = false
o.linkSlotNum = linkSlotNum
}
}
func WithLocalSlotNum(localSlotNum int) Option {
if localSlotNum < 1 {
panic("localSlotNum should be greater than 0")
}
return func(o *option) {
o.localSlotNum = localSlotNum
}
}
func WithLocalSlotSize(localSlotSize int) Option {
if localSlotSize < 1 {
panic("localSlotSize should be greater than 0")
}
return func(o *option) {
o.localSlotSize = localSlotSize
}
+1 -14
View File
@@ -5,20 +5,7 @@ func NewOption() *Option {
}
type Option struct {
Enable *bool
Link []string
}
func (o *Option) WithEnable() *Option {
t := true
o.Enable = &t
return o
}
func (o *Option) WithDisable() *Option {
f := false
o.Enable = &f
return o
Link []string
}
func (o *Option) WithLink(key ...string) *Option {