2022-09-20 08:11:20 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2023-10-28 07:19:39 +00:00
|
|
|
"github.com/fthvgb1/wp-go/helper/maps"
|
2022-09-20 08:11:20 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type MapCache[K comparable, V any] struct {
|
2023-10-27 12:51:46 +00:00
|
|
|
Cache[K, V]
|
2023-10-29 10:46:01 +00:00
|
|
|
mux sync.Mutex
|
|
|
|
cacheFunc MapSingleFn[K, V]
|
|
|
|
batchCacheFn MapBatchFn[K, V]
|
|
|
|
getCacheBatch func(c context.Context, key []K, timeout time.Duration, params ...any) ([]V, error)
|
2022-09-26 13:25:41 +00:00
|
|
|
}
|
|
|
|
|
2023-10-26 13:38:31 +00:00
|
|
|
type MapSingleFn[K, V any] func(context.Context, K, ...any) (V, error)
|
|
|
|
type MapBatchFn[K comparable, V any] func(context.Context, []K, ...any) (map[K]V, error)
|
|
|
|
|
2023-10-27 12:51:46 +00:00
|
|
|
func NewMapCache[K comparable, V any](data Cache[K, V], cacheFunc MapSingleFn[K, V], batchCacheFn MapBatchFn[K, V]) *MapCache[K, V] {
|
2023-10-26 13:38:31 +00:00
|
|
|
r := &MapCache[K, V]{
|
2023-10-27 12:51:46 +00:00
|
|
|
Cache: data,
|
2023-10-26 13:38:31 +00:00
|
|
|
mux: sync.Mutex{},
|
|
|
|
cacheFunc: cacheFunc,
|
|
|
|
batchCacheFn: batchCacheFn,
|
|
|
|
}
|
|
|
|
if cacheFunc == nil && batchCacheFn != nil {
|
|
|
|
r.setDefaultCacheFn(batchCacheFn)
|
|
|
|
} else if batchCacheFn == nil && cacheFunc != nil {
|
|
|
|
r.SetDefaultBatchFunc(cacheFunc)
|
|
|
|
}
|
2023-10-29 10:46:01 +00:00
|
|
|
ex, ok := any(data).(Expend[K, V])
|
|
|
|
if !ok {
|
|
|
|
r.getCacheBatch = r.getCacheBatchs
|
|
|
|
} else {
|
|
|
|
r.getCacheBatch = r.getBatches(ex)
|
|
|
|
}
|
2023-10-26 13:38:31 +00:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MapCache[K, V]) SetDefaultBatchFunc(fn MapSingleFn[K, V]) {
|
|
|
|
m.batchCacheFn = func(ctx context.Context, ids []K, a ...any) (map[K]V, error) {
|
|
|
|
var err error
|
|
|
|
rr := make(map[K]V)
|
|
|
|
for _, id := range ids {
|
2023-10-27 12:51:46 +00:00
|
|
|
v, er := fn(ctx, id, a...)
|
2023-10-26 13:38:31 +00:00
|
|
|
if er != nil {
|
|
|
|
err = errors.Join(er)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
rr[id] = v
|
|
|
|
}
|
|
|
|
return rr, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MapCache[K, V]) SetCacheFunc(fn MapSingleFn[K, V]) {
|
2022-10-08 06:01:05 +00:00
|
|
|
m.cacheFunc = fn
|
2022-09-27 13:52:15 +00:00
|
|
|
}
|
2023-02-02 14:56:09 +00:00
|
|
|
|
2023-02-02 11:16:18 +00:00
|
|
|
func (m *MapCache[K, V]) GetLastSetTime(ctx context.Context, k K) (t time.Time) {
|
2023-10-27 12:51:46 +00:00
|
|
|
tt := m.Ttl(ctx, k)
|
2023-02-02 11:16:18 +00:00
|
|
|
if tt <= 0 {
|
|
|
|
return
|
2022-10-07 14:27:34 +00:00
|
|
|
}
|
2023-10-27 12:51:46 +00:00
|
|
|
return time.Now().Add(m.Ttl(ctx, k)).Add(-m.GetExpireTime(ctx))
|
2022-10-07 14:27:34 +00:00
|
|
|
}
|
|
|
|
|
2023-10-26 13:38:31 +00:00
|
|
|
func (m *MapCache[K, V]) SetCacheBatchFn(fn MapBatchFn[K, V]) {
|
2022-10-08 06:01:05 +00:00
|
|
|
m.batchCacheFn = fn
|
|
|
|
if m.cacheFunc == nil {
|
2023-10-26 13:38:31 +00:00
|
|
|
m.setDefaultCacheFn(fn)
|
2022-10-08 06:01:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-26 13:38:31 +00:00
|
|
|
func (m *MapCache[K, V]) setDefaultCacheFn(fn MapBatchFn[K, V]) {
|
|
|
|
m.cacheFunc = func(ctx context.Context, k K, a ...any) (V, error) {
|
2022-11-07 08:04:13 +00:00
|
|
|
var err error
|
|
|
|
var r map[K]V
|
2023-10-26 13:38:31 +00:00
|
|
|
r, err = fn(ctx, []K{k}, a...)
|
2022-11-07 08:04:13 +00:00
|
|
|
|
2022-10-08 06:01:05 +00:00
|
|
|
if err != nil {
|
|
|
|
var rr V
|
|
|
|
return rr, err
|
|
|
|
}
|
2023-02-28 07:17:16 +00:00
|
|
|
return r[k], err
|
2022-10-08 06:01:05 +00:00
|
|
|
}
|
2022-09-27 13:52:15 +00:00
|
|
|
}
|
|
|
|
|
2023-02-02 11:16:18 +00:00
|
|
|
func (m *MapCache[K, V]) Flush(ctx context.Context) {
|
|
|
|
m.mux.Lock()
|
|
|
|
defer m.mux.Unlock()
|
2023-10-27 12:51:46 +00:00
|
|
|
m.Cache.Flush(ctx)
|
2022-09-26 13:25:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MapCache[K, V]) GetCache(c context.Context, key K, timeout time.Duration, params ...any) (V, error) {
|
2023-10-27 12:51:46 +00:00
|
|
|
data, ok := m.Get(c, key)
|
2022-09-20 08:11:20 +00:00
|
|
|
var err error
|
2023-10-27 15:57:15 +00:00
|
|
|
if !ok {
|
2023-10-27 12:51:46 +00:00
|
|
|
ver := m.Ver(c, key)
|
2022-09-20 08:11:20 +00:00
|
|
|
call := func() {
|
2023-02-02 11:16:18 +00:00
|
|
|
m.mux.Lock()
|
|
|
|
defer m.mux.Unlock()
|
2023-10-27 12:51:46 +00:00
|
|
|
if m.Ver(c, key) > ver {
|
|
|
|
data, _ = m.Get(c, key)
|
2022-10-14 09:15:43 +00:00
|
|
|
return
|
|
|
|
}
|
2023-10-26 13:38:31 +00:00
|
|
|
data, err = m.cacheFunc(c, key, params...)
|
2022-09-20 08:11:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2023-02-02 11:16:18 +00:00
|
|
|
m.Set(c, key, data)
|
2022-09-26 13:25:41 +00:00
|
|
|
}
|
|
|
|
if timeout > 0 {
|
|
|
|
ctx, cancel := context.WithTimeout(c, timeout)
|
|
|
|
defer cancel()
|
2022-12-07 05:26:52 +00:00
|
|
|
done := make(chan struct{}, 1)
|
2022-09-26 13:25:41 +00:00
|
|
|
go func() {
|
|
|
|
call()
|
|
|
|
done <- struct{}{}
|
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
err = errors.New(fmt.Sprintf("get cache %v %s", key, ctx.Err().Error()))
|
|
|
|
case <-done:
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
call()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2023-02-02 11:16:18 +00:00
|
|
|
return data, err
|
2022-09-26 13:25:41 +00:00
|
|
|
}
|
|
|
|
|
2022-09-27 13:52:15 +00:00
|
|
|
func (m *MapCache[K, V]) GetCacheBatch(c context.Context, key []K, timeout time.Duration, params ...any) ([]V, error) {
|
2023-10-29 10:46:01 +00:00
|
|
|
return m.getCacheBatch(c, key, timeout, params...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MapCache[K, V]) getCacheBatchs(c context.Context, key []K, timeout time.Duration, params ...any) ([]V, error) {
|
2023-10-28 06:50:06 +00:00
|
|
|
var res = make([]V, 0, len(key))
|
|
|
|
var needIndex = make(map[K]int)
|
2023-10-28 07:03:14 +00:00
|
|
|
var ver = make(map[K]int)
|
2023-10-28 06:50:06 +00:00
|
|
|
for i, k := range key {
|
2023-10-28 05:30:32 +00:00
|
|
|
v, ok := m.Get(c, k)
|
2023-10-28 06:50:06 +00:00
|
|
|
if !ok {
|
2023-10-28 07:03:14 +00:00
|
|
|
ver[k] = m.Ver(c, k)
|
2023-10-28 06:50:06 +00:00
|
|
|
needIndex[k] = i
|
2022-09-27 13:52:15 +00:00
|
|
|
}
|
2023-10-28 06:50:06 +00:00
|
|
|
res = append(res, v)
|
|
|
|
}
|
2023-10-28 07:19:39 +00:00
|
|
|
if len(needIndex) < 1 {
|
2023-10-28 05:30:32 +00:00
|
|
|
return res, nil
|
|
|
|
}
|
2023-10-28 06:50:06 +00:00
|
|
|
|
2022-09-26 13:25:41 +00:00
|
|
|
var err error
|
2023-10-28 05:30:32 +00:00
|
|
|
call := func() {
|
|
|
|
m.mux.Lock()
|
|
|
|
defer m.mux.Unlock()
|
2023-10-28 07:19:39 +00:00
|
|
|
needFlushs := maps.FilterToSlice(needIndex, func(k K, v int) (K, bool) {
|
|
|
|
return k, ver[k] >= m.Ver(c, k)
|
2023-10-28 07:03:14 +00:00
|
|
|
})
|
2023-02-02 11:16:18 +00:00
|
|
|
|
2023-10-28 07:03:14 +00:00
|
|
|
if len(needFlushs) < 1 {
|
|
|
|
for k, i := range needIndex {
|
|
|
|
res[i], _ = m.Get(c, k)
|
|
|
|
}
|
2023-10-28 05:30:32 +00:00
|
|
|
return
|
|
|
|
}
|
2023-02-02 11:16:18 +00:00
|
|
|
|
2023-10-28 07:03:14 +00:00
|
|
|
r, er := m.batchCacheFn(c, needFlushs, params...)
|
2023-10-28 05:30:32 +00:00
|
|
|
if err != nil {
|
|
|
|
err = er
|
|
|
|
return
|
2022-09-20 08:11:20 +00:00
|
|
|
}
|
2023-10-28 07:19:39 +00:00
|
|
|
for k, i := range needIndex {
|
2023-10-28 10:37:00 +00:00
|
|
|
v, ok := r[k]
|
2023-10-28 07:19:39 +00:00
|
|
|
if ok {
|
|
|
|
res[i] = v
|
2023-10-28 10:40:21 +00:00
|
|
|
m.Set(c, k, v)
|
2023-10-28 10:37:00 +00:00
|
|
|
} else {
|
|
|
|
v, ok = m.Get(c, k)
|
|
|
|
if ok {
|
|
|
|
res[i] = v
|
|
|
|
}
|
2023-10-28 06:50:06 +00:00
|
|
|
}
|
2023-10-28 05:30:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if timeout > 0 {
|
|
|
|
ctx, cancel := context.WithTimeout(c, timeout)
|
|
|
|
defer cancel()
|
|
|
|
done := make(chan struct{}, 1)
|
|
|
|
go func() {
|
2022-09-20 08:11:20 +00:00
|
|
|
call()
|
2023-10-28 05:30:32 +00:00
|
|
|
done <- struct{}{}
|
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
err = errors.New(fmt.Sprintf("get cache %v %s", key, ctx.Err().Error()))
|
|
|
|
case <-done:
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
call()
|
|
|
|
}
|
2023-10-28 06:50:06 +00:00
|
|
|
|
2022-09-27 13:52:15 +00:00
|
|
|
return res, err
|
2022-09-20 08:11:20 +00:00
|
|
|
}
|
2023-10-29 10:46:01 +00:00
|
|
|
|
|
|
|
func (m *MapCache[K, V]) getBatches(e Expend[K, V]) func(ctx context.Context, key []K, timeout time.Duration, params ...any) ([]V, error) {
|
|
|
|
cc := e
|
|
|
|
return func(ctx context.Context, key []K, timeout time.Duration, params ...any) ([]V, error) {
|
|
|
|
var res = make([]V, 0, len(key))
|
|
|
|
var needIndex = make(map[K]int)
|
|
|
|
var err error
|
|
|
|
mm, err := cc.Gets(ctx, key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var flushKeys []K
|
|
|
|
for i, k := range key {
|
|
|
|
v, ok := mm[k]
|
|
|
|
if !ok {
|
|
|
|
flushKeys = append(flushKeys, k)
|
|
|
|
needIndex[k] = i
|
|
|
|
} else {
|
|
|
|
var vv V
|
|
|
|
v = vv
|
|
|
|
}
|
|
|
|
res = append(res, v)
|
|
|
|
}
|
|
|
|
if len(needIndex) < 1 {
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
vers := cc.Vers(ctx, flushKeys)
|
|
|
|
|
|
|
|
call := func() {
|
|
|
|
m.mux.Lock()
|
|
|
|
defer m.mux.Unlock()
|
|
|
|
verss := cc.Vers(ctx, flushKeys)
|
|
|
|
needFlushs := maps.FilterToSlice(needIndex, func(k K, v int) (K, bool) {
|
|
|
|
vv, ok := vers[k]
|
|
|
|
vvv, ook := verss[k]
|
|
|
|
if !ok || !ook || vv >= vvv {
|
|
|
|
return k, true
|
|
|
|
}
|
|
|
|
return k, false
|
|
|
|
})
|
|
|
|
|
|
|
|
if len(needFlushs) < 1 {
|
|
|
|
vv, er := cc.Gets(ctx, needFlushs)
|
|
|
|
if er != nil {
|
|
|
|
err = er
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for k, i := range needIndex {
|
|
|
|
v, ok := vv[k]
|
|
|
|
if ok {
|
|
|
|
res[i] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
r, er := m.batchCacheFn(ctx, needFlushs, params...)
|
|
|
|
if err != nil {
|
|
|
|
err = er
|
|
|
|
return
|
|
|
|
}
|
|
|
|
cc.Sets(ctx, r)
|
|
|
|
|
|
|
|
for k, i := range needIndex {
|
|
|
|
v, ok := r[k]
|
|
|
|
if ok {
|
|
|
|
res[i] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if timeout > 0 {
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, timeout)
|
|
|
|
defer cancel()
|
|
|
|
done := make(chan struct{}, 1)
|
|
|
|
go func() {
|
|
|
|
call()
|
|
|
|
done <- struct{}{}
|
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
err = errors.New(fmt.Sprintf("get cache %v %s", key, ctx.Err().Error()))
|
|
|
|
case <-done:
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
call()
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
}
|