This commit is contained in:
xing 2023-10-28 13:30:32 +08:00
parent ddf6e62e5a
commit f369cf4f22

28
cache/map.go vendored
View File

@ -131,16 +131,24 @@ func (m *MapCache[K, V]) GetCache(c context.Context, key K, timeout time.Duratio
func (m *MapCache[K, V]) GetCacheBatch(c context.Context, key []K, timeout time.Duration, params ...any) ([]V, error) { func (m *MapCache[K, V]) GetCacheBatch(c context.Context, key []K, timeout time.Duration, params ...any) ([]V, error) {
var res []V var res []V
ver := 0 ver := 0
needFlush := slice.FilterAndMap(key, func(k K) (r K, ok bool) { var needFlush []K
if _, ok := m.Get(c, k); !ok { var needIndex = make(map[int]K)
slice.ForEach(key, func(i int, k K) {
v, ok := m.Get(c, k)
var vv V
if ok {
vv = v
} else {
needFlush = append(needFlush, k)
ver += m.Ver(c, k) ver += m.Ver(c, k)
return k, true needIndex[i] = k
} }
return res = append(res, vv)
}) })
if len(needFlush) < 1 {
return res, nil
}
var err error var err error
if len(needFlush) > 0 {
call := func() { call := func() {
m.mux.Lock() m.mux.Lock()
defer m.mux.Unlock() defer m.mux.Unlock()
@ -178,9 +186,11 @@ func (m *MapCache[K, V]) GetCacheBatch(c context.Context, key []K, timeout time.
} else { } else {
call() call()
} }
for index, k := range needIndex {
v, ok := m.Get(c, k)
if ok {
res[index] = v
}
} }
res = slice.FilterAndMap(key, func(k K) (V, bool) {
return m.Get(c, k)
})
return res, err return res, err
} }