mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-06-03 14:31:43 +08:00
feat: msg local cache
This commit is contained in:
parent
1ae523dcc2
commit
3d77c1c8cf
@ -15,21 +15,12 @@ func New[V any](opts ...Option) Cache[V] {
|
||||
for _, o := range opts {
|
||||
o(opt)
|
||||
}
|
||||
if opt.enable {
|
||||
lc := local.NewCache[V](opt.localSlotNum, opt.localSlotSize, opt.localSuccessTTL, opt.localFailedTTL, opt.target)
|
||||
c := &cache[V]{
|
||||
opt: opt,
|
||||
local: lc,
|
||||
}
|
||||
go func() {
|
||||
c.opt.delCh(c.del)
|
||||
}()
|
||||
return c
|
||||
} else {
|
||||
return &cache[V]{
|
||||
opt: opt,
|
||||
}
|
||||
}
|
||||
c := &cache[V]{opt: opt}
|
||||
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
|
||||
}
|
||||
|
||||
type cache[V any] struct {
|
||||
@ -37,6 +28,10 @@ type cache[V any] struct {
|
||||
local local.Cache[V]
|
||||
}
|
||||
|
||||
func (c *cache[V]) onEvict(key string, value V) {
|
||||
|
||||
}
|
||||
|
||||
func (c *cache[V]) del(key ...string) {
|
||||
for _, k := range key {
|
||||
c.local.Del(k)
|
||||
|
@ -11,36 +11,35 @@ type Cache[V any] interface {
|
||||
Del(key string) bool
|
||||
}
|
||||
|
||||
func NewCache[V any](slotNum, slotSize int, successTTL, failedTTL time.Duration, target Target) Cache[V] {
|
||||
c := &cache[V]{
|
||||
func NewCache[V any](slotNum, slotSize int, successTTL, failedTTL time.Duration, target Target, onEvict EvictCallback[string, V]) Cache[V] {
|
||||
c := &slot[V]{
|
||||
n: uint64(slotNum),
|
||||
slots: make([]*LRU[string, V], slotNum),
|
||||
target: target,
|
||||
}
|
||||
for i := 0; i < slotNum; i++ {
|
||||
c.slots[i] = NewLRU[string, V](slotSize, successTTL, failedTTL, c.target)
|
||||
c.slots[i] = NewLRU[string, V](slotSize, successTTL, failedTTL, c.target, onEvict)
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
type cache[V any] struct {
|
||||
type slot[V any] struct {
|
||||
n uint64
|
||||
slots []*LRU[string, V]
|
||||
target Target
|
||||
}
|
||||
|
||||
func (c *cache[V]) index(s string) uint64 {
|
||||
func (c *slot[V]) index(s string) uint64 {
|
||||
h := fnv.New64a()
|
||||
_, _ = h.Write(*(*[]byte)(unsafe.Pointer(&s)))
|
||||
//_, _ = h.Write([]byte(s))
|
||||
return h.Sum64() % c.n
|
||||
}
|
||||
|
||||
func (c *cache[V]) Get(key string, fetch func() (V, error)) (V, error) {
|
||||
func (c *slot[V]) Get(key string, fetch func() (V, error)) (V, error) {
|
||||
return c.slots[c.index(key)].Get(key, fetch)
|
||||
}
|
||||
|
||||
func (c *cache[V]) Del(key string) bool {
|
||||
func (c *slot[V]) Del(key string) bool {
|
||||
if c.slots[c.index(key)].Del(key) {
|
||||
c.target.IncrDelHit()
|
||||
return true
|
||||
|
5
pkg/common/localcache/local/callback.go
Normal file
5
pkg/common/localcache/local/callback.go
Normal file
@ -0,0 +1,5 @@
|
||||
package local
|
||||
|
||||
import "github.com/hashicorp/golang-lru/v2/simplelru"
|
||||
|
||||
type EvictCallback[K comparable, V any] simplelru.EvictCallback[K, V]
|
@ -14,8 +14,14 @@ type waitItem[V any] struct {
|
||||
value V
|
||||
}
|
||||
|
||||
func NewLRU[K comparable, V any](size int, successTTL, failedTTL time.Duration, target Target) *LRU[K, V] {
|
||||
core, err := simplelru.NewLRU[K, *waitItem[V]](size, nil)
|
||||
func NewLRU[K comparable, V any](size int, successTTL, failedTTL time.Duration, target Target, onEvict EvictCallback[K, V]) *LRU[K, V] {
|
||||
var cb simplelru.EvictCallback[K, *waitItem[V]]
|
||||
if onEvict != nil {
|
||||
cb = func(key K, value *waitItem[V]) {
|
||||
onEvict(key, value.value)
|
||||
}
|
||||
}
|
||||
core, err := simplelru.NewLRU[K, *waitItem[V]](size, cb)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
@ -42,7 +42,7 @@ func (r *cacheTarget) String() string {
|
||||
|
||||
func TestName(t *testing.T) {
|
||||
target := &cacheTarget{}
|
||||
l := NewCache[string](100, 1000, time.Second*20, time.Second*5, target)
|
||||
l := NewCache[string](100, 1000, time.Second*20, time.Second*5, target, nil)
|
||||
//l := NewLRU[string, string](1000, time.Second*20, time.Second*5, target)
|
||||
|
||||
fn := func(key string, n int, fetch func() (string, error)) {
|
||||
|
32
pkg/common/localcache/option/option.go
Normal file
32
pkg/common/localcache/option/option.go
Normal file
@ -0,0 +1,32 @@
|
||||
package option
|
||||
|
||||
var (
|
||||
t = true
|
||||
f = false
|
||||
)
|
||||
|
||||
type Option struct {
|
||||
enable *bool
|
||||
key []string
|
||||
}
|
||||
|
||||
func (o *Option) Enable() *Option {
|
||||
o.enable = &t
|
||||
return o
|
||||
}
|
||||
|
||||
func (o *Option) Disable() *Option {
|
||||
o.enable = &f
|
||||
return o
|
||||
}
|
||||
|
||||
func (o *Option) DelKey(key ...string) *Option {
|
||||
if len(key) > 0 {
|
||||
if o.key == nil {
|
||||
o.key = key
|
||||
} else {
|
||||
o.key = append(o.key, key...)
|
||||
}
|
||||
}
|
||||
return o
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user