wp-go/cache/map.go

220 lines
4.5 KiB
Go
Raw Normal View History

2022-09-20 08:11:20 +00:00
package cache
import (
"context"
"errors"
"fmt"
"sync"
"time"
)
type MapCache[K comparable, V any] struct {
data map[K]mapCacheStruct[V]
mutex *sync.Mutex
setCacheFunc func(...any) (V, error)
setBatchCacheFn func(...any) (map[K]V, error)
expireTime time.Duration
}
type mapCacheStruct[T any] struct {
setTime time.Time
incr int
data T
2022-09-20 08:11:20 +00:00
}
2022-09-27 13:52:15 +00:00
func (m *MapCache[K, V]) SetCacheFunc(fn func(...any) (V, error)) {
m.setCacheFunc = fn
}
func (m *MapCache[K, V]) SetCacheBatchFunc(fn func(...any) (map[K]V, error)) {
m.setBatchCacheFn = fn
}
2022-09-26 08:35:38 +00:00
func NewMapCache[K comparable, V any](fun func(...any) (V, error), expireTime time.Duration) *MapCache[K, V] {
2022-09-20 08:11:20 +00:00
return &MapCache[K, V]{
mutex: &sync.Mutex{},
setCacheFunc: fun,
2022-09-26 08:35:38 +00:00
expireTime: expireTime,
data: make(map[K]mapCacheStruct[V]),
}
}
func NewMapBatchCache[K comparable, V any](fn func(...any) (map[K]V, error), expireTime time.Duration) *MapCache[K, V] {
return &MapCache[K, V]{
mutex: &sync.Mutex{},
setBatchCacheFn: fn,
expireTime: expireTime,
data: make(map[K]mapCacheStruct[V]),
2022-09-20 08:11:20 +00:00
}
}
func (m *MapCache[K, V]) FlushCache(k any) {
m.mutex.Lock()
defer m.mutex.Unlock()
2022-09-20 08:11:20 +00:00
key := k.(K)
delete(m.data, key)
}
func (m *MapCache[K, V]) Get(k K) V {
return m.data[k].data
}
func (m *MapCache[K, V]) Set(k K, v V) {
m.mutex.Lock()
defer m.mutex.Unlock()
m.set(k, v)
2022-09-20 08:11:20 +00:00
}
2022-09-27 07:35:34 +00:00
func (m *MapCache[K, V]) SetByBatchFn(params ...any) error {
m.mutex.Lock()
defer m.mutex.Unlock()
r, err := m.setBatchCacheFn(params...)
if err != nil {
return err
}
for k, v := range r {
m.set(k, v)
}
return nil
}
func (m *MapCache[K, V]) set(k K, v V) {
data, ok := m.data[k]
t := time.Now()
if !ok {
data.data = v
data.setTime = t
data.incr++
m.data[k] = data
} else {
m.data[k] = mapCacheStruct[V]{
data: v,
setTime: t,
}
}
}
func (m *MapCache[K, V]) GetCache(c context.Context, key K, timeout time.Duration, params ...any) (V, error) {
data, ok := m.data[key]
if !ok {
data = mapCacheStruct[V]{}
}
2022-09-28 12:02:43 +00:00
now := time.Duration(time.Now().UnixNano())
2022-09-20 08:11:20 +00:00
var err error
2022-09-28 12:02:43 +00:00
expired := time.Duration(data.setTime.UnixNano())+m.expireTime < now
2022-09-20 13:16:51 +00:00
//todo 这里应该判断下取出的值是否为零值,不过怎么操作呢?
if !ok || (ok && m.expireTime >= 0 && expired) {
t := data.incr
2022-09-20 08:11:20 +00:00
call := func() {
m.mutex.Lock()
defer m.mutex.Unlock()
if data.incr > t {
2022-09-20 08:11:20 +00:00
return
}
r, er := m.setCacheFunc(params...)
2022-09-20 08:11:20 +00:00
if err != nil {
err = er
return
}
data.setTime = time.Now()
data.data = r
m.data[key] = data
data.incr++
}
if timeout > 0 {
ctx, cancel := context.WithTimeout(c, timeout)
defer cancel()
done := make(chan struct{})
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 data.data, err
}
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) {
var needFlush []K
var res []V
t := 0
2022-09-28 12:02:43 +00:00
now := time.Duration(time.Now().UnixNano())
2022-09-27 13:52:15 +00:00
for _, k := range key {
d, ok := m.data[k]
if !ok {
needFlush = append(needFlush, k)
continue
}
2022-09-28 12:02:43 +00:00
expired := time.Duration(d.setTime.UnixNano())+m.expireTime < now
2022-09-27 13:52:15 +00:00
if expired {
needFlush = append(needFlush, k)
}
t = t + d.incr
}
var err error
//todo 这里应该判断下取出的值是否为零值,不过怎么操作呢?
2022-09-27 13:52:15 +00:00
if len(needFlush) > 0 {
call := func() {
m.mutex.Lock()
defer m.mutex.Unlock()
2022-09-27 13:52:15 +00:00
tt := 0
for _, dd := range needFlush {
if ddd, ok := m.data[dd]; ok {
tt = tt + ddd.incr
}
}
if tt > t {
return
}
r, er := m.setBatchCacheFn(params...)
if err != nil {
err = er
return
}
for k, v := range r {
m.set(k, v)
}
2022-09-20 08:11:20 +00:00
}
if timeout > 0 {
ctx, cancel := context.WithTimeout(c, timeout)
2022-09-20 08:11:20 +00:00
defer cancel()
done := make(chan struct{})
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()
}
}
2022-09-27 13:52:15 +00:00
for _, k := range key {
d := m.data[k]
res = append(res, d.data)
}
return res, err
2022-09-20 08:11:20 +00:00
}
2022-09-28 12:02:43 +00:00
func (m *MapCache[K, V]) ClearExpiredCache() {
now := time.Duration(time.Now().UnixNano())
m.mutex.Lock()
defer m.mutex.Unlock()
for k, v := range m.data {
if now > time.Duration(v.setTime.UnixNano())+m.expireTime {
delete(m.data, k)
}
}
}