wp-go/cache/vars.go

100 lines
1.9 KiB
Go
Raw Permalink Normal View History

2022-09-19 09:39:00 +00:00
package cache
import (
2022-09-20 04:00:09 +00:00
"context"
2022-09-20 08:11:20 +00:00
"errors"
"fmt"
"github.com/fthvgb1/wp-go/safety"
2022-09-19 09:39:00 +00:00
"sync"
"time"
)
2023-01-20 10:10:13 +00:00
type VarCache[T any] struct {
v safety.Var[vars[T]]
2022-10-14 09:15:43 +00:00
}
2023-01-20 10:10:13 +00:00
type vars[T any] struct {
data T
2022-09-19 09:39:00 +00:00
mutex *sync.Mutex
2023-01-20 10:10:13 +00:00
setCacheFunc func(...any) (T, error)
2022-09-19 09:39:00 +00:00
expireTime time.Duration
setTime time.Time
2022-09-20 04:00:09 +00:00
incr int
2022-09-19 09:39:00 +00:00
}
2023-01-20 10:10:13 +00:00
func (c *VarCache[T]) GetLastSetTime() time.Time {
2022-10-14 09:15:43 +00:00
return c.v.Load().setTime
2022-10-07 12:37:42 +00:00
}
2023-01-20 10:10:13 +00:00
func NewVarCache[T any](fun func(...any) (T, error), duration time.Duration) *VarCache[T] {
return &VarCache[T]{
v: safety.NewVar(vars[T]{
2022-10-14 09:15:43 +00:00
mutex: &sync.Mutex{},
setCacheFunc: fun,
expireTime: duration,
}),
2022-09-19 09:39:00 +00:00
}
}
2023-01-20 10:10:13 +00:00
func (c *VarCache[T]) IsExpired() bool {
v := c.v.Load()
return time.Duration(v.setTime.UnixNano())+v.expireTime < time.Duration(time.Now().UnixNano())
}
func (c *VarCache[T]) Flush() {
2023-01-20 10:43:11 +00:00
v := c.v.Load()
mu := v.mutex
2022-10-14 09:15:43 +00:00
mu.Lock()
defer mu.Unlock()
2023-01-20 10:43:11 +00:00
var vv T
v.data = vv
c.v.Store(v)
2022-09-19 14:02:34 +00:00
}
2023-01-20 10:10:13 +00:00
func (c *VarCache[T]) GetCache(ctx context.Context, timeout time.Duration, params ...any) (T, error) {
2022-10-14 09:15:43 +00:00
v := c.v.Load()
data := v.data
2022-09-20 08:11:20 +00:00
var err error
2023-01-20 10:10:13 +00:00
if v.expireTime <= 0 || ((time.Duration(v.setTime.UnixNano()) + v.expireTime) < time.Duration(time.Now().UnixNano())) {
2022-10-14 09:15:43 +00:00
t := v.incr
2022-09-20 08:11:20 +00:00
call := func() {
2022-10-14 09:15:43 +00:00
v.mutex.Lock()
defer v.mutex.Unlock()
2022-10-14 13:42:24 +00:00
vv := c.v.Load()
if vv.incr > t {
2022-09-20 04:00:09 +00:00
return
}
2022-10-14 13:42:24 +00:00
r, er := vv.setCacheFunc(params...)
2022-09-20 04:00:09 +00:00
if err != nil {
2022-09-20 08:11:20 +00:00
err = er
2022-09-20 04:00:09 +00:00
return
}
2022-10-14 13:42:24 +00:00
vv.setTime = time.Now()
vv.data = r
2022-09-20 08:55:13 +00:00
data = r
2022-10-14 13:42:24 +00:00
vv.incr++
c.v.Store(vv)
2022-09-20 08:11:20 +00:00
}
if timeout > 0 {
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
2022-12-07 05:26:52 +00:00
done := make(chan struct{}, 1)
2022-09-20 08:11:20 +00:00
go func() {
call()
done <- struct{}{}
close(done)
}()
select {
case <-ctx.Done():
err = errors.New(fmt.Sprintf("get cache %s", ctx.Err().Error()))
case <-done:
2022-09-20 04:00:09 +00:00
2022-09-20 08:11:20 +00:00
}
} else {
call()
2022-09-19 09:39:00 +00:00
}
2022-09-20 08:11:20 +00:00
2022-09-19 09:39:00 +00:00
}
2022-09-20 08:55:13 +00:00
return data, err
2022-09-19 09:39:00 +00:00
}