数据缓存实现 #1

Merged
zhangdeman merged 2 commits from feature/abstract into master 2024-06-21 14:32:48 +08:00
5 changed files with 316 additions and 0 deletions

37
abstract/i_cache.go Normal file
View File

@ -0,0 +1,37 @@
// Package cache ...
//
// Description : cache ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2023-03-21 12:09
package cache
import (
"context"
)
// ResultParseFunc 结解析方法
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:18 2024/6/21
type ResultParseFunc func(res string) error
// ICache 缓存的接口约束
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:09 2024/6/21
type ICache interface {
// Exist 缓存是否存在
Exist(ctx context.Context, cacheInstanceFlag string, key string) (bool, error)
// Set 设置缓存
Set(ctx context.Context, cacheInstanceFlag string, key string, value string, ttl int64) error
// Get 读取原始缓存数据
Get(ctx context.Context, cacheInstanceFlag string, key string) (string, error)
// GetWithParse 获取结果并解析
GetWithParse(ctx context.Context, cacheInstanceFlag string, key string, parseFunc ResultParseFunc) (string, error)
// Delete 删除缓存
Delete(ctx context.Context, cacheInstanceFlag string, key string) error
}

28
go.mod
View File

@ -1,3 +1,31 @@
module git.zhangdeman.cn/zhangdeman/cache module git.zhangdeman.cn/zhangdeman/cache
go 1.22.4 go 1.22.4
require git.zhangdeman.cn/zhangdeman/redis v0.0.0-20240621044326-9daae920468f
require (
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240617073616-39e82fd033ed // indirect
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240311030808-e2a2e6a3c211 // indirect
git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20240122104027-4928421213c0 // indirect
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240618035451-8d48a6bd39dd // indirect
git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e // indirect
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240612083858-8d056baada2e // indirect
github.com/BurntSushi/toml v1.4.0 // indirect
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
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
github.com/tidwall/gjson v1.17.1 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

65
go.sum Normal file
View File

@ -0,0 +1,65 @@
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240617073616-39e82fd033ed h1:BGv+y6ZdtkxI4HeSIHun0QRrIzjLnUht0bKdyO2t6n4=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240617073616-39e82fd033ed/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240311030808-e2a2e6a3c211 h1:I/wOsRpCSRkU9vo1u703slQsmK0wnNeZzsWQOGtIAG0=
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240311030808-e2a2e6a3c211/go.mod h1:SrtvrQRdzt+8KfYzvosH++gWxo2ShPTzR1m3VQ6uX7U=
git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20240122104027-4928421213c0 h1:gUDlQMuJ4xNfP2Abl1Msmpa3fASLWYkNlqDFF/6GN0Y=
git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20240122104027-4928421213c0/go.mod h1:VHb9qmhaPDAQDcS6vUiDCamYjZ4R5lD1XtVsh55KsMI=
git.zhangdeman.cn/zhangdeman/redis v0.0.0-20240620104455-27e193a0ea47 h1:Taq1XyvDgFM/xTSEw3cUOWh4krgqfEDIzcVrqQ7Z3UE=
git.zhangdeman.cn/zhangdeman/redis v0.0.0-20240620104455-27e193a0ea47/go.mod h1:OImVgoNTw9qWxGrgrym0wtEMdx0ekaH7vJJKIgSpdEg=
git.zhangdeman.cn/zhangdeman/redis v0.0.0-20240621044326-9daae920468f h1:jWKmzxECoaNywEiay2TCf0+s2L1U/Kgggfywf8aZZyQ=
git.zhangdeman.cn/zhangdeman/redis v0.0.0-20240621044326-9daae920468f/go.mod h1:OImVgoNTw9qWxGrgrym0wtEMdx0ekaH7vJJKIgSpdEg=
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240618035451-8d48a6bd39dd h1:2Y37waOVCmVvx0Rp8VGEptE2/2JVMImtxB4dKKDk/3w=
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240618035451-8d48a6bd39dd/go.mod h1:6+7whkCmb4sJDIfH3HxNuXRveaM0gCCNWd2uXZqNtIE=
git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e h1:Q973S6CcWr1ICZhFI1STFOJ+KUImCl2BaIXm6YppBqI=
git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e/go.mod h1:VpPjBlwz8U+OxZuxzHQBv1aEEZ3pStH6bZvT21ADEbI=
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240612083858-8d056baada2e h1:+PeWa2QdYBWnL32CfAAgy0dlaRCVNmYZDH4q+9w7Gfg=
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240612083858-8d056baada2e/go.mod h1:US/pcq2vstE3iyxIHf53w8IeXKkZys7bj/ozLWkRYeE=
github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0=
github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 h1:OYA+5W64v3OgClL+IrOD63t4i/RW7RqrAVl9LTZ9UqQ=
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394/go.mod h1:Q8n74mJTIgjX4RBBcHnJ05h//6/k6foqmgE45jTQtxg=
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A=
github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
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=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/redis/go-redis/v9 v9.5.3 h1:fOAp1/uJG+ZtcITgZOfYFmTKPE7n4Vclj1wZFgRciUU=
github.com/redis/go-redis/v9 v9.5.3/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M=
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/tidwall/gjson v1.17.1 h1:wlYEnwqAHgzmhNUFfw7Xalt2JzQvsMx2Se4PcoFCT/U=
github.com/tidwall/gjson v1.17.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

91
gocache.go Normal file
View File

@ -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]
}

95
redis.go Normal file
View File

@ -0,0 +1,95 @@
// Package cache ...
//
// Description : cache ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2024-06-21 14:18
package cache
import (
"context"
cacheAbstract "git.zhangdeman.cn/zhangdeman/cache/abstract"
"git.zhangdeman.cn/zhangdeman/redis"
)
var (
RedisClient = &Redis{}
)
// Redis 基于redis缓存
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:19 2023/3/21
type Redis struct {
}
// Exist 缓存是否存在
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:02 2024/6/21
func (r *Redis) Exist(ctx context.Context, instanceFlag string, key string) (bool, error) {
if nil == ctx {
ctx = context.Background()
}
res := redis.Wrapper.Exist(ctx, instanceFlag, key)
return res.Result == "1", res.Err
}
// Set 设置缓存
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:42 2023/3/25
func (r *Redis) Set(ctx context.Context, instanceFlag string, key string, value string, ttl int64) error {
if nil == ctx {
ctx = context.Background()
}
return redis.Wrapper.SetEx(ctx, instanceFlag, key, value, ttl, false).Err
}
// Get 获取redis中的原始数据
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:52 2023/4/26
func (r *Redis) Get(ctx context.Context, instanceFlag string, key string) (string, error) {
if nil == ctx {
ctx = context.Background()
}
res := redis.Wrapper.Get(ctx, instanceFlag, key)
return res.Result, res.Err
}
// Delete 删除缓存
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:41 2023/3/25
func (r *Redis) Delete(ctx context.Context, instanceFlag string, key string) error {
if nil == ctx {
ctx = context.Background()
}
return redis.Wrapper.Del(ctx, instanceFlag, key).Err
}
// GetWithParse ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:26 2024/6/21
func (r *Redis) GetWithParse(ctx context.Context, cacheInstanceFlag string, key string, parseFunc cacheAbstract.ResultParseFunc) (string, error) {
var (
res string
err error
)
if res, err = r.Get(ctx, cacheInstanceFlag, key); nil != err {
return "", err
}
if nil == parseFunc {
return res, nil
}
return res, parseFunc(res)
}