feat: 增加空缓存实现的支持
This commit is contained in:
12
common.go
12
common.go
@@ -8,22 +8,22 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
cacheAbstract "git.zhangdeman.cn/zhangdeman/cache/abstract"
|
||||
"git.zhangdeman.cn/zhangdeman/consts"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// GetCacheInstance 获取缓存实例
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 14:56 2024/6/21
|
||||
func GetCacheInstance(cacheDriver string) cacheAbstract.ICache {
|
||||
switch strings.ToLower(cacheDriver) {
|
||||
case consts.CacheDriverRedis:
|
||||
return RedisClient
|
||||
case consts.CacheDriverGocache:
|
||||
return GocacheClient
|
||||
case consts.CacherDriverNull:
|
||||
return DefaultClient
|
||||
}
|
||||
return nil
|
||||
// 非法驱动统一默认空实现
|
||||
return DefaultClient
|
||||
}
|
||||
|
||||
48
default.go
Normal file
48
default.go
Normal file
@@ -0,0 +1,48 @@
|
||||
// Package cache ...
|
||||
//
|
||||
// Description : cache ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2026-01-05 09:33
|
||||
package cache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
cacheAbstract "git.zhangdeman.cn/zhangdeman/cache/abstract"
|
||||
)
|
||||
|
||||
var (
|
||||
DefaultClient cacheAbstract.ICache = DefaultCache{}
|
||||
)
|
||||
|
||||
type DefaultCache struct {
|
||||
}
|
||||
|
||||
func (d DefaultCache) Exist(ctx context.Context, cacheInstanceFlag string, key string) (bool, error) {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func (d DefaultCache) TTL(ctx context.Context, cacheInstanceFlag string, key string) (int64, error) {
|
||||
// 直接返回已过期
|
||||
return -1, nil
|
||||
}
|
||||
|
||||
func (d DefaultCache) Set(ctx context.Context, cacheInstanceFlag string, key string, value any, ttl int64) error {
|
||||
// 设置成功
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d DefaultCache) Get(ctx context.Context, cacheInstanceFlag string, key string) (any, error) {
|
||||
return nil, errors.New("not found")
|
||||
}
|
||||
|
||||
func (d DefaultCache) GetWithParse(ctx context.Context, cacheInstanceFlag string, key string, parseFunc cacheAbstract.ResultParseFunc) (any, error) {
|
||||
return d.Get(ctx, cacheInstanceFlag, key)
|
||||
}
|
||||
|
||||
func (d DefaultCache) Delete(ctx context.Context, cacheInstanceFlag string, key string) error {
|
||||
return nil
|
||||
}
|
||||
4
go.mod
4
go.mod
@@ -5,10 +5,11 @@ go 1.24.0
|
||||
toolchain go1.24.2
|
||||
|
||||
require (
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20260102134357-189dc7f57c1a
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20260105014807-92a7dc3bb97f
|
||||
git.zhangdeman.cn/zhangdeman/redis v0.0.0-20251231085725-147b12cddf5a
|
||||
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20251225094759-09c64ba2541c
|
||||
github.com/patrickmn/go-cache v2.1.0+incompatible
|
||||
github.com/redis/go-redis/v9 v9.17.2
|
||||
)
|
||||
|
||||
require (
|
||||
@@ -25,7 +26,6 @@ require (
|
||||
github.com/mitchellh/go-homedir v1.1.0 // indirect
|
||||
github.com/mozillazg/go-pinyin v0.21.0 // indirect
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
github.com/redis/go-redis/v9 v9.17.2 // indirect
|
||||
github.com/sbabiv/xml2map v1.2.1 // indirect
|
||||
github.com/spaolacci/murmur3 v1.1.0 // indirect
|
||||
github.com/tidwall/gjson v1.18.0 // indirect
|
||||
|
||||
2
go.sum
2
go.sum
@@ -10,6 +10,8 @@ git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250916024308-d378e6c57772 h1:Yo1ur3
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250916024308-d378e6c57772/go.mod h1:5p8CEKGBxi7qPtTXDI3HDmqKAfIm5i/aBWdrbkbdNjc=
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20260102134357-189dc7f57c1a h1:ppfx1Yedkz8mNMi3kydXvmu+gfL2aYApp0YfWWC4rAk=
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20260102134357-189dc7f57c1a/go.mod h1:5p8CEKGBxi7qPtTXDI3HDmqKAfIm5i/aBWdrbkbdNjc=
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20260105014807-92a7dc3bb97f h1:2p5yxwk472XxY93UTky2Xl2doTU1uqR7vcKara/Dt1E=
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20260105014807-92a7dc3bb97f/go.mod h1:5p8CEKGBxi7qPtTXDI3HDmqKAfIm5i/aBWdrbkbdNjc=
|
||||
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/easymap v0.0.0-20241101082529-28a6c68e38a4 h1:s6d4b6yY+NaK1AzoBD1pxqsuygEHQz0Oie86c45geDw=
|
||||
|
||||
@@ -18,14 +18,10 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
GocacheClient = &Gocache{}
|
||||
GocacheClient cacheAbstract.ICache = &Gocache{}
|
||||
)
|
||||
|
||||
// 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) {
|
||||
|
||||
12
redis.go
12
redis.go
@@ -9,21 +9,19 @@ package cache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
cacheAbstract "git.zhangdeman.cn/zhangdeman/cache/abstract"
|
||||
"git.zhangdeman.cn/zhangdeman/redis"
|
||||
"git.zhangdeman.cn/zhangdeman/wrapper/op_string"
|
||||
redisClient "github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
var (
|
||||
RedisClient = &Redis{}
|
||||
RedisClient cacheAbstract.ICache = &Redis{}
|
||||
)
|
||||
|
||||
// Redis 基于redis缓存
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 14:19 2023/3/21
|
||||
type Redis struct {
|
||||
}
|
||||
|
||||
@@ -33,6 +31,10 @@ func (r *Redis) Exist(ctx context.Context, instanceFlag string, key string) (boo
|
||||
ctx = context.Background()
|
||||
}
|
||||
res := redis.Wrapper.Exist(ctx, instanceFlag, key)
|
||||
if nil != res.Err && errors.Is(res.Err, redisClient.Nil) {
|
||||
// 未查询到数据, 也是数据不存在
|
||||
return false, nil
|
||||
}
|
||||
return res.Result == "1", res.Err
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user