optimize again
This commit is contained in:
parent
171b3bc59c
commit
0f467bbc98
17
cache/map.go
vendored
17
cache/map.go
vendored
|
@ -130,14 +130,14 @@ 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 = make([]V, 0, len(key))
|
var res = make([]V, 0, len(key))
|
||||||
var ver = 0
|
|
||||||
var needFlush []K
|
var needFlush []K
|
||||||
var needIndex = make(map[K]int)
|
var needIndex = make(map[K]int)
|
||||||
|
var ver = make(map[K]int)
|
||||||
for i, k := range key {
|
for i, k := range key {
|
||||||
v, ok := m.Get(c, k)
|
v, ok := m.Get(c, k)
|
||||||
if !ok {
|
if !ok {
|
||||||
needFlush = append(needFlush, k)
|
needFlush = append(needFlush, k)
|
||||||
ver += m.Ver(c, k)
|
ver[k] = m.Ver(c, k)
|
||||||
needIndex[k] = i
|
needIndex[k] = i
|
||||||
}
|
}
|
||||||
res = append(res, v)
|
res = append(res, v)
|
||||||
|
@ -151,15 +151,18 @@ func (m *MapCache[K, V]) GetCacheBatch(c context.Context, key []K, timeout time.
|
||||||
m.mux.Lock()
|
m.mux.Lock()
|
||||||
defer m.mux.Unlock()
|
defer m.mux.Unlock()
|
||||||
|
|
||||||
vers := slice.Reduce(needFlush, func(t K, r int) int {
|
needFlushs := slice.Filter(needFlush, func(k K, i int) bool {
|
||||||
return r + m.Ver(c, t)
|
return ver[k] >= m.Ver(c, k)
|
||||||
}, 0)
|
})
|
||||||
|
|
||||||
if vers > ver {
|
if len(needFlushs) < 1 {
|
||||||
|
for k, i := range needIndex {
|
||||||
|
res[i], _ = m.Get(c, k)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
r, er := m.batchCacheFn(c, key, params...)
|
r, er := m.batchCacheFn(c, needFlushs, params...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = er
|
err = er
|
||||||
return
|
return
|
||||||
|
|
Loading…
Reference in New Issue
Block a user