mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-05-06 01:55:58 +08:00
feat: implement Getbatch in rpcCache.
This commit is contained in:
@@ -20,6 +20,7 @@ type EvictCallback[K comparable, V any] simplelru.EvictCallback[K, V]
|
||||
|
||||
type LRU[K comparable, V any] interface {
|
||||
Get(key K, fetch func() (V, error)) (V, error)
|
||||
GetBatch(key []K, fetchBatch func([]K) (map[string]V, error)) (map[string]V, error)
|
||||
SetHas(key K, value V) bool
|
||||
Del(key K) bool
|
||||
Stop()
|
||||
|
||||
@@ -15,10 +15,14 @@
|
||||
package lru
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/golang-lru/v2/simplelru"
|
||||
"github.com/openimsdk/tools/errs"
|
||||
"github.com/openimsdk/tools/log"
|
||||
"github.com/openimsdk/tools/utils/datautil"
|
||||
)
|
||||
|
||||
type layLruItem[V any] struct {
|
||||
@@ -88,20 +92,76 @@ func (x *LayLRU[K, V]) Get(key K, fetch func() (V, error)) (V, error) {
|
||||
return v.value, v.err
|
||||
}
|
||||
|
||||
func (x *LayLRU[K, V]) GetBatch(keys []K, fetch func() ([]V, error)) ([]V, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (x *LayLRU[K, V]) GetBatch(keys []K, fetchBatch func([]K) (map[K]V, error)) (map[K]V, error) {
|
||||
ctx := context.Background()
|
||||
resultMap := make(map[K]V)
|
||||
// errorMap := make(map[K]error)
|
||||
missingKeys := []K{}
|
||||
lazyLruItemMap := make(map[K]*layLruItem[V])
|
||||
|
||||
func (x *LayLRU[K, V]) SetHasBatch(data map[K]V) bool {
|
||||
x.lock.Lock()
|
||||
defer x.lock.Unlock()
|
||||
for key, value := range data {
|
||||
if x.core.Contains(key) {
|
||||
x.core.Add(key, &layLruItem[V]{value: value, expires: time.Now().Add(x.successTTL).UnixMilli()})
|
||||
return true
|
||||
for _, key := range keys {
|
||||
x.lock.Lock()
|
||||
v, ok := x.core.Get(key)
|
||||
lazyLruItemMap[key] = v
|
||||
if ok {
|
||||
x.lock.Unlock()
|
||||
v.lock.Lock()
|
||||
expires, value, err := v.expires, v.value, v.err
|
||||
if expires != 0 && expires > time.Now().UnixMilli() {
|
||||
v.lock.Unlock()
|
||||
resultMap[key] = value
|
||||
x.target.IncrGetHit()
|
||||
} else {
|
||||
missingKeys = append(missingKeys, key)
|
||||
v.lock.Unlock()
|
||||
}
|
||||
if err != nil {
|
||||
log.ZWarn(ctx, "Get Local LRU is failed.", errs.Wrap(err))
|
||||
}
|
||||
continue
|
||||
} else {
|
||||
// initialize and insert new lazyLruItem
|
||||
v = &layLruItem[V]{}
|
||||
lazyLruItemMap[key] = v
|
||||
x.core.Add(key, v)
|
||||
v.lock.Lock()
|
||||
missingKeys = append(missingKeys, key)
|
||||
x.lock.Unlock()
|
||||
}
|
||||
defer v.lock.Unlock()
|
||||
}
|
||||
|
||||
x.lock.Unlock()
|
||||
|
||||
// Fetch missing Key
|
||||
if len(missingKeys) > 0 {
|
||||
failedKey := missingKeys
|
||||
fetchMap, err := fetchBatch(missingKeys)
|
||||
if err != nil {
|
||||
log.ZWarn(ctx, "fetch Key is failed.", errs.Wrap(err))
|
||||
}
|
||||
|
||||
for key, value := range fetchMap {
|
||||
resultMap[key] = value
|
||||
lazyLruItemMap[key].expires = time.Now().Add(x.successTTL).UnixMilli()
|
||||
x.target.IncrGetSuccess()
|
||||
failedKey = datautil.DeleteElems(failedKey, key)
|
||||
}
|
||||
for _, key := range failedKey {
|
||||
lazyLruItemMap[key].expires = time.Now().Add(x.failedTTL).UnixMilli()
|
||||
x.target.IncrGetFailed()
|
||||
}
|
||||
}
|
||||
return false
|
||||
return resultMap, nil
|
||||
}
|
||||
|
||||
func (x *LayLRU[K, V]) SetHasBatch(data map[K]V) {
|
||||
x.lock.Lock()
|
||||
defer x.lock.Unlock()
|
||||
|
||||
for key, value := range data {
|
||||
x.core.Add(key, &layLruItem[V]{value: value, expires: time.Now().Add(x.successTTL).UnixMilli()})
|
||||
}
|
||||
}
|
||||
|
||||
//func (x *LayLRU[K, V]) Set(key K, value V) {
|
||||
|
||||
Reference in New Issue
Block a user