From a157542a88348f57d9ff003e64fa04ef4b66bc33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Fri, 21 Jun 2024 14:30:59 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9F=BA=E4=BA=8Egocache=E7=9A=84=E5=86=85?= =?UTF-8?q?=E5=AD=98=E7=BC=93=E5=AD=98=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 1 + go.sum | 2 ++ gocache.go | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+) create mode 100644 gocache.go diff --git a/go.mod b/go.mod index 5aa6691..33bfb26 100644 --- a/go.mod +++ b/go.mod @@ -18,6 +18,7 @@ require ( github.com/go-ini/ini v1.67.0 // indirect github.com/mitchellh/go-homedir v1.1.0 // indirect github.com/mozillazg/go-pinyin v0.20.0 // indirect + github.com/patrickmn/go-cache v2.1.0+incompatible // indirect github.com/pkg/errors v0.9.1 // indirect github.com/redis/go-redis/v9 v9.5.3 // indirect github.com/spaolacci/murmur3 v1.1.0 // indirect diff --git a/go.sum b/go.sum index f28e2f8..c5f3d8c 100644 --- a/go.sum +++ b/go.sum @@ -34,6 +34,8 @@ github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= github.com/mozillazg/go-pinyin v0.20.0 h1:BtR3DsxpApHfKReaPO1fCqF4pThRwH9uwvXzm+GnMFQ= github.com/mozillazg/go-pinyin v0.20.0/go.mod h1:iR4EnMMRXkfpFVV5FMi4FNB6wGq9NV6uDWbUuPhP4Yc= +github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc= +github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= diff --git a/gocache.go b/gocache.go new file mode 100644 index 0000000..bbabbf4 --- /dev/null +++ b/gocache.go @@ -0,0 +1,91 @@ +// Package cache ... +// +// Description : cache ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2024-06-14 16:50 +package cache + +import ( + "context" + "errors" + cacheAbstract "git.zhangdeman.cn/zhangdeman/cache/abstract" + gocache "github.com/patrickmn/go-cache" + "sync" + "time" +) + +// Gocache 基于gocache的内存缓存 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:51 2024/6/14 +type Gocache struct{} + +func (g *Gocache) Exist(ctx context.Context, cacheInstanceFlag string, key string) (bool, error) { + _, exist := GocacheStorageClient.Get(cacheInstanceFlag).Get(key) + return exist, nil +} + +func (g *Gocache) Set(ctx context.Context, cacheInstanceFlag string, key string, value string, ttl int64) error { + now := time.Now() + if now.Unix() < ttl { + ttl = ttl - now.Unix() + } + GocacheStorageClient.Get(cacheInstanceFlag).Set(key, value, time.Duration(ttl)*time.Second) + return nil +} + +func (g *Gocache) Get(ctx context.Context, cacheInstanceFlag string, key string) (string, error) { + val, exist := GocacheStorageClient.Get(cacheInstanceFlag).Get(key) + if !exist { + return "", errors.New(key + " : not found") + } + return val.(string), nil +} + +func (g *Gocache) GetWithParse(ctx context.Context, cacheInstanceFlag string, key string, parseFunc cacheAbstract.ResultParseFunc) (string, error) { + val, err := g.Get(ctx, cacheInstanceFlag, key) + if nil != err { + return "", err + } + if nil == parseFunc { + return val, nil + } + return val, parseFunc(val) +} + +func (g *Gocache) Delete(ctx context.Context, cacheInstanceFlag string, key string) error { + GocacheStorageClient.Get(cacheInstanceFlag).Delete(key) + return nil +} + +var GocacheStorageClient = &GocacheStorage{ + lock: &sync.RWMutex{}, + clientTable: make(map[string]*gocache.Cache), +} + +type GocacheStorage struct { + lock *sync.RWMutex + clientTable map[string]*gocache.Cache +} + +func (g *GocacheStorage) Add(cacheInstanceFlag string) { + _ = g.Get(cacheInstanceFlag) +} + +func (g *GocacheStorage) Del(cacheInstanceFlag string) { + g.lock.Lock() + defer g.lock.Unlock() + delete(g.clientTable, cacheInstanceFlag) +} + +func (g *GocacheStorage) Get(cacheInstanceFlag string) *gocache.Cache { + g.lock.Lock() + defer g.lock.Unlock() + if _, exist := g.clientTable[cacheInstanceFlag]; !exist { + g.clientTable[cacheInstanceFlag] = gocache.New(0, 0) + } + return g.clientTable[cacheInstanceFlag] +}