Files
open-im-server/pkg/localcache/cache.go
T

113 lines
2.5 KiB
Go
Raw Normal View History

2024-01-08 15:39:39 +08:00
package localcache
import (
"context"
2024-01-12 17:51:01 +08:00
"github.com/openimsdk/localcache/link"
2024-01-15 15:23:42 +08:00
"github.com/openimsdk/localcache/lru"
"hash/fnv"
"unsafe"
2024-01-08 15:39:39 +08:00
)
type Cache[V any] interface {
2024-01-15 15:23:42 +08:00
Get(ctx context.Context, key string, fetch func(ctx context.Context) (V, error)) (V, error)
GetLink(ctx context.Context, key string, fetch func(ctx context.Context) (V, error), link ...string) (V, error)
2024-01-08 15:39:39 +08:00
Del(ctx context.Context, key ...string)
2024-01-15 15:23:42 +08:00
DelLocal(ctx context.Context, key ...string)
Stop()
2024-01-08 15:39:39 +08:00
}
func New[V any](opts ...Option) Cache[V] {
opt := defaultOption()
for _, o := range opts {
o(opt)
}
2024-01-22 21:14:21 +08:00
2024-01-12 15:41:05 +08:00
c := cache[V]{opt: opt}
if opt.localSlotNum > 0 && opt.localSlotSize > 0 {
2024-01-15 15:23:42 +08:00
createSimpleLRU := func() lru.LRU[string, V] {
2024-01-22 21:14:21 +08:00
if opt.expirationEvict {
return lru.NewExpirationLRU[string, V](opt.localSlotSize, opt.localSuccessTTL, opt.localFailedTTL, opt.target, c.onEvict)
2024-01-15 15:23:42 +08:00
} else {
2024-01-22 21:14:21 +08:00
return lru.NewLayLRU[string, V](opt.localSlotSize, opt.localSuccessTTL, opt.localFailedTTL, opt.target, c.onEvict)
2024-01-15 15:23:42 +08:00
}
}
if opt.localSlotNum == 1 {
c.local = createSimpleLRU()
} else {
c.local = lru.NewSlotLRU[string, V](opt.localSlotNum, func(key string) uint64 {
h := fnv.New64a()
h.Write(*(*[]byte)(unsafe.Pointer(&key)))
return h.Sum64()
}, createSimpleLRU)
}
2024-01-12 15:41:05 +08:00
if opt.linkSlotNum > 0 {
c.link = link.New(opt.linkSlotNum)
}
}
return &c
2024-01-08 15:39:39 +08:00
}
type cache[V any] struct {
opt *option
2024-01-08 20:36:41 +08:00
link link.Link
2024-01-15 15:23:42 +08:00
local lru.LRU[string, V]
2024-01-08 15:39:39 +08:00
}
2024-01-08 16:47:39 +08:00
func (c *cache[V]) onEvict(key string, value V) {
2024-01-12 15:41:05 +08:00
if c.link != nil {
lks := c.link.Del(key)
for k := range lks {
if key != k { // prevent deadlock
c.local.Del(k)
}
2024-01-10 16:13:55 +08:00
}
2024-01-08 20:36:41 +08:00
}
2024-01-08 16:47:39 +08:00
}
2024-01-08 15:39:39 +08:00
func (c *cache[V]) del(key ...string) {
2024-01-15 15:23:42 +08:00
if c.local == nil {
return
}
2024-01-08 15:39:39 +08:00
for _, k := range key {
c.local.Del(k)
2024-01-15 15:23:42 +08:00
if c.link != nil {
lks := c.link.Del(k)
for k := range lks {
c.local.Del(k)
}
2024-01-10 16:03:49 +08:00
}
2024-01-08 15:39:39 +08:00
}
}
2024-01-15 15:23:42 +08:00
func (c *cache[V]) Get(ctx context.Context, key string, fetch func(ctx context.Context) (V, error)) (V, error) {
return c.GetLink(ctx, key, fetch)
}
func (c *cache[V]) GetLink(ctx context.Context, key string, fetch func(ctx context.Context) (V, error), link ...string) (V, error) {
2024-01-12 15:41:05 +08:00
if c.local != nil {
2024-01-08 15:39:39 +08:00
return c.local.Get(key, func() (V, error) {
2024-01-15 15:23:42 +08:00
if len(link) > 0 {
c.link.Link(key, link...)
2024-01-12 15:41:05 +08:00
}
2024-01-08 15:39:39 +08:00
return fetch(ctx)
})
} else {
return fetch(ctx)
}
}
func (c *cache[V]) Del(ctx context.Context, key ...string) {
for _, fn := range c.opt.delFn {
fn(ctx, key...)
}
2024-01-15 15:23:42 +08:00
c.del(key...)
}
func (c *cache[V]) DelLocal(ctx context.Context, key ...string) {
c.del(key...)
}
func (c *cache[V]) Stop() {
c.local.Stop()
2024-01-08 15:39:39 +08:00
}