Files
mayfly-go/server/pkg/rediscli/rediscli.go

60 lines
1.1 KiB
Go
Raw Normal View History

2021-03-24 17:18:39 +08:00
package rediscli
import (
"context"
2021-03-24 17:18:39 +08:00
"time"
2023-03-17 09:46:41 +08:00
"github.com/redis/go-redis/v9"
2021-03-24 17:18:39 +08:00
)
var cli *redis.Client
func SetCli(client *redis.Client) {
cli = client
}
func GetCli() *redis.Client {
return cli
}
// get key value
2022-12-26 20:53:07 +08:00
func Get(key string) (string, error) {
return cli.Get(context.TODO(), key).Result()
2021-03-24 17:18:39 +08:00
}
// set key value
func Set(key string, val string, expiration time.Duration) {
cli.Set(context.TODO(), key, val, expiration)
2021-03-24 17:18:39 +08:00
}
2022-12-26 20:53:07 +08:00
func Del(key string) {
cli.Del(context.TODO(), key)
}
func HSet(key string, field string, val any) {
cli.HSet(context.TODO(), key, field, val)
2021-03-24 17:18:39 +08:00
}
// hget
func HGet(key string, field string) string {
val, _ := cli.HGet(context.TODO(), key, field).Result()
2021-03-24 17:18:39 +08:00
return val
}
// hget
func HExist(key string, field string) bool {
val, _ := cli.HExists(context.TODO(), key, field).Result()
2021-03-24 17:18:39 +08:00
return val
}
// hgetall
func HGetAll(key string) map[string]string {
vals, _ := cli.HGetAll(context.TODO(), key).Result()
2021-03-24 17:18:39 +08:00
return vals
}
// hdel
func HDel(key string, fields ...string) int {
return int(cli.HDel(context.TODO(), key, fields...).Val())
2021-03-24 17:18:39 +08:00
}