feat: upgrade cache

This commit is contained in:
2025-10-18 11:31:47 +08:00
parent eda887e8ff
commit 9f21048ac0
5 changed files with 14 additions and 44 deletions

View File

@ -12,28 +12,20 @@ import (
)
// ResultParseFunc 结解析方法
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:18 2024/6/21
type ResultParseFunc func(res string) error
type ResultParseFunc func(res any) 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)
// TTL key还有多长时间过期
TTL(ctx context.Context, cacheInstanceFlag string, key string) (int64, error)
// Set 设置缓存
Set(ctx context.Context, cacheInstanceFlag string, key string, value string, ttl int64) error
Set(ctx context.Context, cacheInstanceFlag string, key string, value any, ttl int64) error
// Get 读取原始缓存数据
Get(ctx context.Context, cacheInstanceFlag string, key string) (string, error)
Get(ctx context.Context, cacheInstanceFlag string, key string) (any, error)
// GetWithParse 获取结果并解析
GetWithParse(ctx context.Context, cacheInstanceFlag string, key string, parseFunc ResultParseFunc) (string, error)
GetWithParse(ctx context.Context, cacheInstanceFlag string, key string, parseFunc ResultParseFunc) (any, error)
// Delete 删除缓存
Delete(ctx context.Context, cacheInstanceFlag string, key string) error
}

2
go.mod
View File

@ -6,7 +6,7 @@ toolchain go1.24.2
require (
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250916024308-d378e6c57772
git.zhangdeman.cn/zhangdeman/redis v0.0.0-20251013151630-64624ba3f890
git.zhangdeman.cn/zhangdeman/redis v0.0.0-20251018032416-adc90ef927fd
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20251014035305-c0ec06fa6dff
github.com/patrickmn/go-cache v2.1.0+incompatible
)

2
go.sum
View File

@ -28,6 +28,8 @@ git.zhangdeman.cn/zhangdeman/redis v0.0.0-20251013142555-be4a06504143 h1:Xyn6Pcl
git.zhangdeman.cn/zhangdeman/redis v0.0.0-20251013142555-be4a06504143/go.mod h1:U/wWk/XiJyLAXoLs4gtKWPEX4UjN4FDQRvmQs/Jfwco=
git.zhangdeman.cn/zhangdeman/redis v0.0.0-20251013151630-64624ba3f890 h1:cI0IXh+tv644DIx1+7sXvaaRdPW6hak598Z3/9pEMEc=
git.zhangdeman.cn/zhangdeman/redis v0.0.0-20251013151630-64624ba3f890/go.mod h1:kyundk5g6fV4WVQEDMLm9E3ix89rI7RAP+jUZR4Rumg=
git.zhangdeman.cn/zhangdeman/redis v0.0.0-20251018032416-adc90ef927fd h1:lT3fklgTazBOZ8N8G+smLszjcP0hbhLNmHDKAkI7VqM=
git.zhangdeman.cn/zhangdeman/redis v0.0.0-20251018032416-adc90ef927fd/go.mod h1:e52xq9Jzp2Kb+nrsfwteIbZ9VzGFDLuEpmXpSr35jIM=
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/serialize v0.0.0-20241223084948-de2e49144fcd h1:q7GG14qgXKB4MEXQFOe7/UYebsqMfPaSX80TcPdOosI=

View File

@ -51,7 +51,7 @@ func (g *Gocache) TTL(ctx context.Context, cacheInstanceFlag string, key string)
return expireAt.Unix() - time.Now().Unix(), nil
}
func (g *Gocache) Set(ctx context.Context, cacheInstanceFlag string, key string, value string, ttl int64) error {
func (g *Gocache) Set(ctx context.Context, cacheInstanceFlag string, key string, value any, ttl int64) error {
now := time.Now()
if now.Unix() < ttl {
ttl = ttl - now.Unix()
@ -60,7 +60,7 @@ func (g *Gocache) Set(ctx context.Context, cacheInstanceFlag string, key string,
return nil
}
func (g *Gocache) Get(ctx context.Context, cacheInstanceFlag string, key string) (string, error) {
func (g *Gocache) Get(ctx context.Context, cacheInstanceFlag string, key string) (any, error) {
val, exist := GocacheStorageClient.Get(cacheInstanceFlag).Get(key)
if !exist {
return "", errors.New(key + " : not found")
@ -68,7 +68,7 @@ func (g *Gocache) Get(ctx context.Context, cacheInstanceFlag string, key string)
return val.(string), nil
}
func (g *Gocache) GetWithParse(ctx context.Context, cacheInstanceFlag string, key string, parseFunc cacheAbstract.ResultParseFunc) (string, error) {
func (g *Gocache) GetWithParse(ctx context.Context, cacheInstanceFlag string, key string, parseFunc cacheAbstract.ResultParseFunc) (any, error) {
val, err := g.Get(ctx, cacheInstanceFlag, key)
if nil != err {
return "", err

View File

@ -28,10 +28,6 @@ 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()
@ -41,10 +37,6 @@ func (r *Redis) Exist(ctx context.Context, instanceFlag string, key string) (boo
}
// TTL 获取key的剩余有效期
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:11 2024/10/9
func (r *Redis) TTL(ctx context.Context, instanceFlag string, key string) (int64, error) {
if nil == ctx {
ctx = context.Background()
@ -58,11 +50,7 @@ func (r *Redis) TTL(ctx context.Context, instanceFlag string, key string) (int64
}
// 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 {
func (r *Redis) Set(ctx context.Context, instanceFlag string, key string, value any, ttl int64) error {
if nil == ctx {
ctx = context.Background()
}
@ -70,11 +58,7 @@ func (r *Redis) Set(ctx context.Context, instanceFlag string, key string, value
}
// 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) {
func (r *Redis) Get(ctx context.Context, instanceFlag string, key string) (any, error) {
if nil == ctx {
ctx = context.Background()
}
@ -83,10 +67,6 @@ func (r *Redis) Get(ctx context.Context, instanceFlag string, key string) (strin
}
// 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()
@ -95,13 +75,9 @@ func (r *Redis) Delete(ctx context.Context, instanceFlag string, key string) 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) {
func (r *Redis) GetWithParse(ctx context.Context, cacheInstanceFlag string, key string, parseFunc cacheAbstract.ResultParseFunc) (any, error) {
var (
res string
res any
err error
)
if res, err = r.Get(ctx, cacheInstanceFlag, key); nil != err {