optimize again again......
This commit is contained in:
parent
0f467bbc98
commit
71ddf299e4
18
cache/map.go
vendored
18
cache/map.go
vendored
|
@ -4,7 +4,7 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/fthvgb1/wp-go/helper/slice"
|
"github.com/fthvgb1/wp-go/helper/maps"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
@ -130,19 +130,17 @@ 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 needFlush []K
|
|
||||||
var needIndex = make(map[K]int)
|
var needIndex = make(map[K]int)
|
||||||
var ver = 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)
|
|
||||||
ver[k] = m.Ver(c, k)
|
ver[k] = m.Ver(c, k)
|
||||||
needIndex[k] = i
|
needIndex[k] = i
|
||||||
}
|
}
|
||||||
res = append(res, v)
|
res = append(res, v)
|
||||||
}
|
}
|
||||||
if len(needFlush) < 1 {
|
if len(needIndex) < 1 {
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -150,9 +148,8 @@ func (m *MapCache[K, V]) GetCacheBatch(c context.Context, key []K, timeout time.
|
||||||
call := func() {
|
call := func() {
|
||||||
m.mux.Lock()
|
m.mux.Lock()
|
||||||
defer m.mux.Unlock()
|
defer m.mux.Unlock()
|
||||||
|
needFlushs := maps.FilterToSlice(needIndex, func(k K, v int) (K, bool) {
|
||||||
needFlushs := slice.Filter(needFlush, func(k K, i int) bool {
|
return k, ver[k] >= m.Ver(c, k)
|
||||||
return ver[k] >= m.Ver(c, k)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
if len(needFlushs) < 1 {
|
if len(needFlushs) < 1 {
|
||||||
|
@ -171,6 +168,13 @@ func (m *MapCache[K, V]) GetCacheBatch(c context.Context, key []K, timeout time.
|
||||||
m.Set(c, k, v)
|
m.Set(c, k, v)
|
||||||
if i, ok := needIndex[k]; ok {
|
if i, ok := needIndex[k]; ok {
|
||||||
res[i] = v
|
res[i] = v
|
||||||
|
delete(needIndex, k)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for k, i := range needIndex {
|
||||||
|
v, ok := m.Get(c, k)
|
||||||
|
if ok {
|
||||||
|
res[i] = v
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user