From 9088f515d3c73c1662c62e5b6831f45f3d474f3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Wed, 9 Oct 2024 18:30:12 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BC=93=E5=AD=98=E6=94=AF=E6=8C=81=E8=AF=BB?= =?UTF-8?q?=E5=8F=96=E7=BC=93=E5=AD=98key=E7=9A=84=E5=89=A9=E4=BD=99?= =?UTF-8?q?=E6=9C=89=E6=95=88=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- abstract/i_cache.go | 2 ++ go.mod | 14 ++++++++------ go.sum | 10 ++++++++++ gocache.go | 18 ++++++++++++++++++ redis.go | 14 ++++++++++++++ 5 files changed, 52 insertions(+), 6 deletions(-) diff --git a/abstract/i_cache.go b/abstract/i_cache.go index baa3b71..f72faf4 100644 --- a/abstract/i_cache.go +++ b/abstract/i_cache.go @@ -26,6 +26,8 @@ type ResultParseFunc func(res string) error 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 // Get 读取原始缓存数据 diff --git a/go.mod b/go.mod index fc23f60..a54cdf0 100644 --- a/go.mod +++ b/go.mod @@ -2,15 +2,18 @@ module git.zhangdeman.cn/zhangdeman/cache go 1.22.4 -require git.zhangdeman.cn/zhangdeman/redis v0.0.0-20240621044326-9daae920468f +require ( + git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241009101709-d8400fb206b4 + git.zhangdeman.cn/zhangdeman/redis v0.0.0-20241009101849-a85ebc020329 + git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240924063449-ef80c6cb79d1 + github.com/patrickmn/go-cache v2.1.0+incompatible +) require ( - git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240621065139-666060e4de0b // 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 @@ -18,11 +21,10 @@ 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/redis/go-redis/v9 v9.6.1 // indirect github.com/spaolacci/murmur3 v1.1.0 // indirect - github.com/tidwall/gjson v1.17.1 // indirect + github.com/tidwall/gjson v1.18.0 // 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 diff --git a/go.sum b/go.sum index 96cd3d2..7068618 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,8 @@ git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240617073616-39e82fd033ed h1:BGv+y6 git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240617073616-39e82fd033ed/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k= git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240621065139-666060e4de0b h1:9rQAR9naSQL5DCdjIW0lZwzs+a4R5lW/NYJ4Ng3hqic= git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240621065139-666060e4de0b/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k= +git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241009101709-d8400fb206b4 h1:GUWgrIR7Gw1BIPzU6qUQgnB+7ZCE9nIwudAQTk7cyoU= +git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241009101709-d8400fb206b4/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= @@ -10,12 +12,16 @@ git.zhangdeman.cn/zhangdeman/redis v0.0.0-20240620104455-27e193a0ea47 h1:Taq1Xyv 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/redis v0.0.0-20241009101849-a85ebc020329 h1:RSczQapTVb70rGix191oaQka8FZlVG7TlNzHmrVWv3g= +git.zhangdeman.cn/zhangdeman/redis v0.0.0-20241009101849-a85ebc020329/go.mod h1:MXoPgvolyv2O0DiMLfdxGmys8COSKIDw+XLAKZ0mpjg= 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= +git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240924063449-ef80c6cb79d1 h1:LYw8NJeWcOiyrGjH9weyxnaMit94MlIngL+uskbLjtw= +git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240924063449-ef80c6cb79d1/go.mod h1:+2qNxuRsfyfOvXk9HNwn+CmyPmmhhrQm/eIi1FDU1jw= 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= @@ -44,12 +50,16 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb 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/redis/go-redis/v9 v9.6.1 h1:HHDteefn6ZkTtY5fGUE8tj8uy85AHk6zP7CpzIAM0y4= +github.com/redis/go-redis/v9 v9.6.1/go.mod h1:0C0c6ycQsdpVNQpxb1njEQIqkx5UcsM8FJCQLgE9+RA= 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/gjson v1.18.0 h1:FIDeeyB800efLX89e5a8Y0BNH+LOngJyGrIWxG2FKQY= +github.com/tidwall/gjson v1.18.0/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= diff --git a/gocache.go b/gocache.go index 7a17494..881f5b3 100644 --- a/gocache.go +++ b/gocache.go @@ -32,6 +32,24 @@ func (g *Gocache) Exist(ctx context.Context, cacheInstanceFlag string, key strin return exist, nil } +func (g *Gocache) TTL(ctx context.Context, cacheInstanceFlag string, key string) (int64, error) { + var ( + exist bool + expireAt time.Time + ) + + _, expireAt, exist = GocacheStorageClient.Get(cacheInstanceFlag).GetWithExpiration(key) + if !exist { + // 和redis key不存在返回值一致 + return -2, nil + } + if expireAt.Unix() == 0 { + // 存在并且未设置过期时间, 和redis key返回值一致 + return -1, nil + } + return expireAt.Unix() - time.Now().Unix(), nil +} + func (g *Gocache) Set(ctx context.Context, cacheInstanceFlag string, key string, value string, ttl int64) error { now := time.Now() if now.Unix() < ttl { diff --git a/redis.go b/redis.go index d253a3c..43aaf9f 100644 --- a/redis.go +++ b/redis.go @@ -11,6 +11,7 @@ import ( "context" cacheAbstract "git.zhangdeman.cn/zhangdeman/cache/abstract" "git.zhangdeman.cn/zhangdeman/redis" + "git.zhangdeman.cn/zhangdeman/wrapper" ) var ( @@ -38,6 +39,19 @@ func (r *Redis) Exist(ctx context.Context, instanceFlag string, key string) (boo return res.Result == "1", res.Err } +// 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() + } + res := redis.Wrapper.TTL(ctx, instanceFlag, key) + return wrapper.String(res.Result).ToInt64().Value, res.Err +} + // Set 设置缓存 // // Author : go_developer@163.com<白茶清欢>