From eed45f51baee13f4ce9d26371a6a513468bdbb7a Mon Sep 17 00:00:00 2001 From: xing Date: Mon, 11 Dec 2023 22:32:00 +0800 Subject: [PATCH] fix bug and add redis cache drive example --- .../devexample/plugintt/redisCache.go.dev | 129 ++++++++++++++++++ .../devexample/plugintt/redisCache.go.mod.dev | 50 +++++++ cache/cachemanager/manger.go | 2 +- 3 files changed, 180 insertions(+), 1 deletion(-) create mode 100644 app/plugins/devexample/plugintt/redisCache.go.dev create mode 100644 app/plugins/devexample/plugintt/redisCache.go.mod.dev diff --git a/app/plugins/devexample/plugintt/redisCache.go.dev b/app/plugins/devexample/plugintt/redisCache.go.dev new file mode 100644 index 0000000..7c4f637 --- /dev/null +++ b/app/plugins/devexample/plugintt/redisCache.go.dev @@ -0,0 +1,129 @@ +package main + +import ( + "context" + "errors" + "fmt" + "github.com/fthvgb1/wp-go/app/pkg/config" + "github.com/fthvgb1/wp-go/app/pkg/dao" + "github.com/fthvgb1/wp-go/app/theme/wp" + "github.com/fthvgb1/wp-go/cache/cachemanager" + "github.com/fthvgb1/wp-go/helper/number" + "github.com/fthvgb1/wp-go/helper/slice" + "github.com/fthvgb1/wp-go/helper/strings" + "github.com/redis/go-redis/v9" + "strconv" + strings2 "strings" + "time" +) + +type RdmCache[K comparable, V any] struct { + expired func() time.Duration + rdb *redis.Client + keyFn func(K) string + name string + resFn func(map[string]string) V + saveData func(V) map[string]string +} + +func (r *RdmCache[K, V]) SetExpiredTime(f func() time.Duration) { + r.expired = f +} + +func (r *RdmCache[K, V]) Get(ctx context.Context, key K) (V, bool) { + var re V + result, err := r.rdb.Exists(ctx, r.keyFn(key)).Result() + if result <= 0 || err != nil { + return re, false + } + + rr, err := r.rdb.HGetAll(ctx, r.keyFn(key)).Result() + + if errors.Is(err, redis.Nil) { + return re, false + } + if err != nil { + return re, false + } + return r.resFn(rr), true +} + +func (r *RdmCache[K, V]) Set(ctx context.Context, key K, val V) { + k := r.keyFn(key) + result, err := r.rdb.HSet(ctx, k, r.saveData(val)).Result() + b, err := r.rdb.Expire(ctx, k, r.expired()).Result() + if err != nil { + fmt.Println(result, b, err) + return + } + fmt.Println(result, err) + +} + +func (r *RdmCache[K, V]) GetExpireTime(ctx context.Context) time.Duration { + return r.expired() +} + +func (r *RdmCache[K, V]) Ttl(ctx context.Context, key K) time.Duration { + result, err := r.rdb.TTL(ctx, r.keyFn(key)).Result() + if err != nil { + return 0 + } + return result +} + +func (r *RdmCache[K, V]) Flush(ctx context.Context) { + +} + +func (r *RdmCache[K, V]) Del(ctx context.Context, key ...K) { + r.rdb.Del(ctx, slice.Map(key, r.keyFn)...) +} + +func (r *RdmCache[K, V]) ClearExpired(ctx context.Context) { + +} +// use step: +//1 go build -gcflags all="-N -l" --race -buildmode=plugin -o redisCache.so main.go && cp ./redisCache.so wp-go/plugins/ +//2 wp-go config add redisCache plugin +func Re(h *wp.Handle) { + vv, ok := cachemanager.GetMapCache[string, dao.PostIds]("listPostIds") + if ok { + _, ok := any(vv.Cache).(*RdmCache[string, dao.PostIds]) + if ok { + return + } + } + rdm := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password set + DB: 0, // use default DB + }) + r := RdmCache[string, dao.PostIds]{ + expired: func() time.Duration { + return time.Minute + }, + keyFn: func(u string) string { + return strings.Join("postIds:", u) + }, + rdb: rdm, + name: "", + resFn: func(m map[string]string) dao.PostIds { + return dao.PostIds{ + Ids: slice.Map(strings2.Split(m["ids"], ","), strings.ToInt[uint64]), + Length: strings.ToInt[int](m["length"]), + } + }, + saveData: func(ids dao.PostIds) map[string]string { + t := slice.Map(ids.Ids, number.IntToString[uint64]) + return map[string]string{ + "ids": strings2.Join(t, ","), + "length": strconv.Itoa(ids.Length), + } + }, + } + cachemanager.NewMapCache[string, dao.PostIds](&r, nil, dao.SearchPostIds, config.GetConfig().CacheTime.PostListCacheTime, "listPostIds", func() time.Duration { + return config.GetConfig().CacheTime.PostListCacheTime + }) + fmt.Println("redis cache inited ok") +} diff --git a/app/plugins/devexample/plugintt/redisCache.go.mod.dev b/app/plugins/devexample/plugintt/redisCache.go.mod.dev new file mode 100644 index 0000000..bbb04fb --- /dev/null +++ b/app/plugins/devexample/plugintt/redisCache.go.mod.dev @@ -0,0 +1,50 @@ +module redisCache + +go 1.21 + +require ( + github.com/fthvgb1/wp-go v0.0.0-20231210111549-d72bed0c8c4e + github.com/redis/go-redis/v9 v9.3.0 +) + +replace github.com/fthvgb1/wp-go v0.0.0-20231210111549-d72bed0c8c4e => ../wp-go + +require ( + github.com/bytedance/sonic v1.10.2 // indirect + github.com/cespare/xxhash/v2 v2.2.0 // indirect + github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect + github.com/chenzhuoyu/iasm v0.9.1 // indirect + github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect + github.com/dlclark/regexp2 v1.10.0 // indirect + github.com/elliotchance/phpserialize v1.3.3 // indirect + github.com/gabriel-vasile/mimetype v1.4.3 // indirect + github.com/gin-contrib/sessions v0.0.5 // indirect + github.com/gin-contrib/sse v0.1.0 // indirect + github.com/gin-gonic/gin v1.9.1 // indirect + github.com/go-playground/locales v0.14.1 // indirect + github.com/go-playground/universal-translator v0.18.1 // indirect + github.com/go-playground/validator/v10 v10.16.0 // indirect + github.com/goccy/go-json v0.10.2 // indirect + github.com/gorilla/context v1.1.2 // indirect + github.com/gorilla/securecookie v1.1.2 // indirect + github.com/gorilla/sessions v1.2.2 // indirect + github.com/jmoiron/sqlx v1.3.5 // indirect + github.com/json-iterator/go v1.1.12 // indirect + github.com/klauspost/cpuid/v2 v2.2.6 // indirect + github.com/leodido/go-urn v1.2.4 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/pelletier/go-toml/v2 v2.1.0 // indirect + github.com/twitchyliquid64/golang-asm v0.15.1 // indirect + github.com/ugorji/go/codec v1.2.11 // indirect + golang.org/x/arch v0.6.0 // indirect + golang.org/x/crypto v0.15.0 // indirect + golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa // indirect + golang.org/x/net v0.18.0 // indirect + golang.org/x/sys v0.14.0 // indirect + golang.org/x/text v0.14.0 // indirect + google.golang.org/protobuf v1.31.0 // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/cache/cachemanager/manger.go b/cache/cachemanager/manger.go index 96e36b5..e0090ea 100644 --- a/cache/cachemanager/manger.go +++ b/cache/cachemanager/manger.go @@ -289,5 +289,5 @@ func GetVarCache[T any](name string) (*cache.VarCache[T], bool) { func GetMapCache[K comparable, V any](name string) (*cache.MapCache[K, V], bool) { vv, err := getMap[K, V](name) - return vv, err != nil + return vv, err == nil }