feat: 新增pgsql数据操作&redis集群操作

This commit is contained in:
meilin.huang
2022-07-10 12:14:06 +08:00
parent 729a3d7028
commit 1c18a01bf6
30 changed files with 584 additions and 185 deletions

View File

@@ -1,10 +1,11 @@
package rediscli
import (
"context"
"fmt"
"time"
"github.com/go-redis/redis"
"github.com/go-redis/redis/v8"
)
var cli *redis.Client
@@ -19,7 +20,7 @@ func GetCli() *redis.Client {
// get key value
func Get(key string) string {
val, err := cli.Get(key).Result()
val, err := cli.Get(context.TODO(), key).Result()
switch {
case err == redis.Nil:
fmt.Println("key does not exist")
@@ -33,32 +34,32 @@ func Get(key string) string {
// set key value
func Set(key string, val string, expiration time.Duration) {
cli.Set(key, val, expiration)
cli.Set(context.TODO(), key, val, expiration)
}
func HSet(key string, field string, val interface{}) {
cli.HSet(key, field, val)
cli.HSet(context.TODO(), key, field, val)
}
// hget
func HGet(key string, field string) string {
val, _ := cli.HGet(key, field).Result()
val, _ := cli.HGet(context.TODO(), key, field).Result()
return val
}
// hget
func HExist(key string, field string) bool {
val, _ := cli.HExists(key, field).Result()
val, _ := cli.HExists(context.TODO(), key, field).Result()
return val
}
// hgetall
func HGetAll(key string) map[string]string {
vals, _ := cli.HGetAll(key).Result()
vals, _ := cli.HGetAll(context.TODO(), key).Result()
return vals
}
// hdel
func HDel(key string, fields ...string) int {
return int(cli.HDel(key, fields...).Val())
return int(cli.HDel(context.TODO(), key, fields...).Val())
}