feat(redis):增加删除Redis缓存内容的快速操作函数。
This commit is contained in:
parent
fddfb4ab15
commit
57893a7c65
14
cache/abstract.go
vendored
14
cache/abstract.go
vendored
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user