修复redis hash设置不成功问题

This commit is contained in:
junleea 2024-05-31 15:24:33 +08:00
parent 8a697e8bfc
commit 063856395c
1 changed files with 8 additions and 1 deletions

View File

@ -61,8 +61,15 @@ func SetRedis(key string, value string) {
// 设置hash
func SetHash(key string, id int, name, email string) {
ctx := context.Background() // 创建一个上下文
fields := map[string]interface{}{
"id": strconv.Itoa(id),
"name": name,
"email": email,
}
// 设置哈希表的字段值, 0 表示不设置过期时间, 如果需要设置过期时间, 可以设置为 time.Second * 10 等
err := redisClient.HSet(ctx, key, "id", id, "name", name, "email", email, time.Hour*12).Err()
err := redisClient.HSet(ctx, key, fields).Err()
err = redisClient.Expire(ctx, key, time.Hour*10).Err()
if err != nil {
log.Fatalf("Error setting key: %v", err)
}