2023-04-16 00:50:36 +08:00
|
|
|
package api
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"mayfly-go/internal/redis/api/form"
|
|
|
|
|
"mayfly-go/pkg/biz"
|
|
|
|
|
"mayfly-go/pkg/req"
|
2023-10-12 12:14:56 +08:00
|
|
|
"mayfly-go/pkg/utils/collx"
|
2023-04-16 00:50:36 +08:00
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func (r *Redis) Hscan(rc *req.Ctx) {
|
2023-10-27 17:41:45 +08:00
|
|
|
ri, key := r.checkKeyAndGetRedisConn(rc)
|
2024-02-24 16:30:29 +08:00
|
|
|
count := rc.F.QueryIntDefault("count", 10)
|
|
|
|
|
match := rc.F.Query("match")
|
|
|
|
|
cursor := rc.F.QueryIntDefault("cursor", 0)
|
2023-04-16 00:50:36 +08:00
|
|
|
contextTodo := context.TODO()
|
|
|
|
|
|
|
|
|
|
cmdable := ri.GetCmdable()
|
|
|
|
|
keys, nextCursor, err := cmdable.HScan(contextTodo, key, uint64(cursor), match, int64(count)).Result()
|
|
|
|
|
biz.ErrIsNilAppendErr(err, "hcan err: %s")
|
|
|
|
|
keySize, err := cmdable.HLen(contextTodo, key).Result()
|
|
|
|
|
biz.ErrIsNilAppendErr(err, "hlen err: %s")
|
|
|
|
|
|
2023-10-12 12:14:56 +08:00
|
|
|
rc.ResData = collx.M{
|
2023-04-16 00:50:36 +08:00
|
|
|
"keys": keys,
|
|
|
|
|
"cursor": nextCursor,
|
|
|
|
|
"keySize": keySize,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *Redis) Hdel(rc *req.Ctx) {
|
2023-10-27 17:41:45 +08:00
|
|
|
ri, key := r.checkKeyAndGetRedisConn(rc)
|
2024-02-24 16:30:29 +08:00
|
|
|
field := rc.F.Query("field")
|
2023-04-16 00:50:36 +08:00
|
|
|
|
2023-10-12 12:14:56 +08:00
|
|
|
rc.ReqParam = collx.Kvs("redis", ri.Info, "key", key, "field", field)
|
2023-04-16 00:50:36 +08:00
|
|
|
delRes, err := ri.GetCmdable().HDel(context.TODO(), key, field).Result()
|
|
|
|
|
biz.ErrIsNilAppendErr(err, "hdel err: %s")
|
|
|
|
|
rc.ResData = delRes
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *Redis) Hget(rc *req.Ctx) {
|
2023-10-27 17:41:45 +08:00
|
|
|
ri, key := r.checkKeyAndGetRedisConn(rc)
|
2024-02-24 16:30:29 +08:00
|
|
|
field := rc.F.Query("field")
|
2023-04-16 00:50:36 +08:00
|
|
|
|
|
|
|
|
res, err := ri.GetCmdable().HGet(context.TODO(), key, field).Result()
|
|
|
|
|
biz.ErrIsNilAppendErr(err, "hget err: %s")
|
|
|
|
|
rc.ResData = res
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *Redis) Hset(rc *req.Ctx) {
|
2024-02-24 16:30:29 +08:00
|
|
|
hashValue := req.BindJsonAndValid(rc, new(form.HashValue))
|
2023-04-16 00:50:36 +08:00
|
|
|
|
|
|
|
|
hv := hashValue.Value[0]
|
2023-10-27 17:41:45 +08:00
|
|
|
ri := r.getRedisConn(rc)
|
2023-10-12 12:14:56 +08:00
|
|
|
rc.ReqParam = collx.Kvs("redis", ri.Info, "hash", hv)
|
2023-09-09 23:34:26 +08:00
|
|
|
|
|
|
|
|
res, err := ri.GetCmdable().HSet(context.TODO(), hashValue.Key, hv["field"].(string), hv["value"]).Result()
|
2023-04-16 00:50:36 +08:00
|
|
|
biz.ErrIsNilAppendErr(err, "hset失败: %s")
|
|
|
|
|
rc.ResData = res
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-21 22:52:20 +08:00
|
|
|
func (r *Redis) SaveHashValue(rc *req.Ctx) {
|
2024-02-24 16:30:29 +08:00
|
|
|
hashValue := req.BindJsonAndValid(rc, new(form.HashValue))
|
2023-04-16 00:50:36 +08:00
|
|
|
|
2023-10-27 17:41:45 +08:00
|
|
|
ri := r.getRedisConn(rc)
|
2023-10-12 12:14:56 +08:00
|
|
|
rc.ReqParam = collx.Kvs("redis", ri.Info, "hash", hashValue)
|
2023-04-16 00:50:36 +08:00
|
|
|
cmd := ri.GetCmdable()
|
|
|
|
|
|
|
|
|
|
key := hashValue.Key
|
|
|
|
|
contextTodo := context.TODO()
|
|
|
|
|
for _, v := range hashValue.Value {
|
|
|
|
|
res := cmd.HSet(contextTodo, key, v["field"].(string), v["value"])
|
|
|
|
|
biz.ErrIsNilAppendErr(res.Err(), "保存hash值失败: %s")
|
|
|
|
|
}
|
|
|
|
|
if hashValue.Timed != 0 && hashValue.Timed != -1 {
|
|
|
|
|
cmd.Expire(context.TODO(), key, time.Second*time.Duration(hashValue.Timed))
|
|
|
|
|
}
|
|
|
|
|
}
|