41 lines
793 B
Go
41 lines
793 B
Go
package redisx
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"time"
|
|
|
|
"github.com/redis/go-redis/v9"
|
|
)
|
|
|
|
var (
|
|
Client *redis.Client
|
|
)
|
|
|
|
func Set(key string, value interface{}, expire time.Duration) error {
|
|
return Client.Set(context.Background(), key, value, expire).Err()
|
|
}
|
|
|
|
// Get 获取任意 key 的值,返回 string
|
|
func Get(key string) (string, error) {
|
|
val, err := Client.Get(context.Background(), key).Result()
|
|
if errors.Is(err, redis.Nil) {
|
|
return "", nil
|
|
}
|
|
return val, err
|
|
}
|
|
|
|
// Delete 删除任意 key
|
|
func Delete(key string) error {
|
|
return Client.Del(context.Background(), key).Err()
|
|
}
|
|
|
|
// Exists 判断 key 是否存在
|
|
func Exists(key string) (bool, error) {
|
|
cnt, err := Client.Exists(context.Background(), key).Result()
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
return cnt > 0, nil
|
|
}
|