From 57893a7c6538e6ddca143193f001137cf4f28edd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Fri, 12 Aug 2022 09:43:31 +0800 Subject: [PATCH] =?UTF-8?q?feat(redis):=E5=A2=9E=E5=8A=A0=E5=88=A0?= =?UTF-8?q?=E9=99=A4Redis=E7=BC=93=E5=AD=98=E5=86=85=E5=AE=B9=E7=9A=84?= =?UTF-8?q?=E5=BF=AB=E9=80=9F=E6=93=8D=E4=BD=9C=E5=87=BD=E6=95=B0=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cache/abstract.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/cache/abstract.go b/cache/abstract.go index 56a790e..f8ae8ef 100644 --- a/cache/abstract.go +++ b/cache/abstract.go @@ -8,6 +8,8 @@ import ( "github.com/vmihailenco/msgpack/v5" ) +// 向Redis缓存中保存一个数据 +// ! 如果需要长期保存一个数据,那么需要向expires传入0。 func Cache[T interface{}](key string, value *T, expires time.Duration) error { serializedValue, err := msgpack.Marshal(value) @@ -18,6 +20,7 @@ func Cache[T interface{}](key string, value *T, expires time.Duration) error { return cmd.Err() } +// 从Redis缓存中获取一个数据 func Retreive[T interface{}](key string) (*T, error) { result, err := global.RedisConn.Get(global.Ctx, key).Result() if err != nil { @@ -35,6 +38,7 @@ func Retreive[T interface{}](key string) (*T, error) { return value, nil } +// 检查Redis缓存中是否存在指定键的记录 func Exists(key string) (bool, error) { result, err := global.RedisConn.Exists(global.Ctx, key).Result() if err != nil { @@ -42,3 +46,13 @@ func Exists(key string) (bool, error) { } return result > 0, nil } + +// 从Redis缓存中删除指定键 +// ! 如果指定键已不存在,那么本函数一样会返回false +func Delete(key string) (bool, error) { + result, err := global.RedisConn.Del(global.Ctx, key).Result() + if err != nil { + return false, err + } + return result > 0, nil +}