2023-10-27 12:51:46 +00:00
|
|
|
package cachemanager
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"github.com/fthvgb1/wp-go/helper/number"
|
2023-11-07 07:18:34 +00:00
|
|
|
"github.com/fthvgb1/wp-go/helper/slice"
|
|
|
|
str "github.com/fthvgb1/wp-go/helper/strings"
|
2023-10-27 12:51:46 +00:00
|
|
|
"github.com/fthvgb1/wp-go/taskPools"
|
2023-11-07 07:18:34 +00:00
|
|
|
"reflect"
|
2023-10-27 12:51:46 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestFlushMapVal(t *testing.T) {
|
|
|
|
_ = number.Range(1, 5, 0)
|
|
|
|
t.Run("t1", func(t *testing.T) {
|
|
|
|
count := 0
|
|
|
|
vv := NewMemoryMapCache(func(ctx2 context.Context, ks []int, a ...any) (map[int]int, error) {
|
|
|
|
r := make(map[int]int)
|
|
|
|
for _, k := range ks {
|
|
|
|
r[k] = k * k
|
|
|
|
}
|
|
|
|
count++
|
|
|
|
return r, nil
|
|
|
|
}, nil, time.Second, "test")
|
|
|
|
|
|
|
|
gets, err := GetMultiple[int]("test", ctx, number.Range(1, 10), time.Second)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(t, "err:", err)
|
|
|
|
}
|
|
|
|
p := taskPools.NewPools(10)
|
|
|
|
for i := 0; i < 20; i++ {
|
|
|
|
i := i
|
|
|
|
p.Execute(func() {
|
|
|
|
if i%2 == 0 {
|
|
|
|
vv.Get(ctx, 5)
|
|
|
|
} else {
|
|
|
|
vv.Set(ctx, i, i)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
p.Wait()
|
|
|
|
fmt.Println(gets, count)
|
|
|
|
FlushMapVal("test", 3, 4)
|
|
|
|
fmt.Println(vv.Get(ctx, 3))
|
|
|
|
fmt.Println(vv.Get(ctx, 4))
|
|
|
|
get, err := Get[int]("test", ctx, 3, time.Second)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(t, "err", err)
|
|
|
|
}
|
|
|
|
fmt.Println(get, count)
|
|
|
|
fmt.Println(vv.Get(ctx, 5))
|
|
|
|
FlushAnyVal("test")
|
|
|
|
fmt.Println(vv.Get(ctx, 5))
|
|
|
|
fmt.Println(vv.Get(ctx, 6))
|
2023-11-07 07:18:34 +00:00
|
|
|
//fmt.Println(GetVarCache("test"))
|
2023-10-27 12:51:46 +00:00
|
|
|
})
|
|
|
|
}
|
2023-10-31 11:23:38 +00:00
|
|
|
|
|
|
|
func TestSetExpireTime(t *testing.T) {
|
|
|
|
t.Run("t1", func(t *testing.T) {
|
2023-11-07 07:18:34 +00:00
|
|
|
c := NewMemoryMapCache[string, string](func(ctx2 context.Context, strings []string, a ...any) (map[string]string, error) {
|
|
|
|
return slice.ToMap(strings, func(v string) (string, string) {
|
|
|
|
return v, str.Join(v, "__", v)
|
|
|
|
}, false), nil
|
|
|
|
}, nil, time.Second, "xx")
|
2023-10-31 11:23:38 +00:00
|
|
|
c.Set(ctx, "xx", "yy")
|
|
|
|
fmt.Println(c.Get(ctx, "xx"))
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
fmt.Println(c.Get(ctx, "xx"))
|
2023-11-02 14:40:13 +00:00
|
|
|
ChangeExpireTime(3*time.Second, "xx")
|
2023-10-31 11:23:38 +00:00
|
|
|
c.Set(ctx, "xx", "yyy")
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
fmt.Println(c.Get(ctx, "xx"))
|
|
|
|
time.Sleep(3 * time.Second)
|
|
|
|
fmt.Println(c.Get(ctx, "xx"))
|
2023-11-07 07:18:34 +00:00
|
|
|
cc, _ := GetMapCache[string, string]("xx")
|
|
|
|
fmt.Println(reflect.DeepEqual(c, cc))
|
|
|
|
cc.Set(ctx, "fff", "xxxx")
|
|
|
|
cc.Set(ctx, "ffx", "eex")
|
|
|
|
cc.Set(ctx, "ww", "vv")
|
|
|
|
m, err := cc.GetBatchToMap(ctx, []string{"fff", "ffx", "ww", "kkkk"}, time.Second)
|
|
|
|
fmt.Println(m, err)
|
|
|
|
fmt.Println(GetMultipleToMap[string]("xx", ctx, []string{"fff", "ffx", "ww", "kkkk"}, time.Second))
|
|
|
|
v := NewVarMemoryCache(func(ct context.Context, a ...any) (string, error) {
|
|
|
|
return "ssss", nil
|
|
|
|
}, 3*time.Second, "ff")
|
|
|
|
vv, _ := GetVarCache[string]("ff")
|
|
|
|
fmt.Println(reflect.DeepEqual(v, vv))
|
2023-10-31 11:23:38 +00:00
|
|
|
})
|
|
|
|
}
|