wangzhibo
2026-08-30 c8515170fd399b4d2dfaebb59128d4674e58915e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package xiaozhi.common.redis;
 
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.stereotype.Component;
 
import jakarta.annotation.Resource;
import xiaozhi.common.utils.ResourcesUtils;
 
/**
 * Redis工具类
 * Copyright (c) 人人开源 All rights reserved.
 * Website: https://www.renren.io
 */
@Component
public class RedisUtils {
    @Resource
    private RedisTemplate<String, Object> redisTemplate;
 
    @Autowired
    private ResourcesUtils resourceUtils;
 
    /**
     * 默认过期时长为24小时,单位:秒
     */
    public final static long DEFAULT_EXPIRE = 60 * 60 * 24L;
    /**
     * 过期时长为1小时,单位:秒
     */
    public final static long HOUR_ONE_EXPIRE = (long) 60 * 60;
    /**
     * 过期时长为6小时,单位:秒
     */
    public final static long HOUR_SIX_EXPIRE = 60 * 60 * 6L;
    /**
     * 不设置过期时长
     */
    public final static long NOT_EXPIRE = -1L;
 
    public Long increment(String key, long expire) {
        Long increment = redisTemplate.opsForValue().increment(key, 1L);
        if (expire != NOT_EXPIRE) {
            expire(key, expire);
        }
        return increment;
    }
 
    public Long increment(String key) {
        return redisTemplate.opsForValue().increment(key, 1L);
    }
 
    public Long decrement(String key) {
        return redisTemplate.opsForValue().decrement(key, 1L);
    }
 
 
 
    public void set(String key, Object value, long expire) {
        redisTemplate.opsForValue().set(key, value);
        if (expire != NOT_EXPIRE) {
            expire(key, expire);
        }
    }
 
    public void set(String key, Object value) {
        set(key, value, DEFAULT_EXPIRE);
    }
 
    public Object get(String key, long expire) {
        Object value = redisTemplate.opsForValue().get(key);
        if (expire != NOT_EXPIRE) {
            expire(key, expire);
        }
        return value;
    }
 
    public Object get(String key) {
        return get(key, NOT_EXPIRE);
    }
 
    public void delete(String key) {
        redisTemplate.delete(key);
    }
 
    public void delete(Collection<String> keys) {
        redisTemplate.delete(keys);
    }
 
    public Object hGet(String key, String field) {
        return redisTemplate.opsForHash().get(key, field);
    }
 
    public Map<String, Object> hGetAll(String key) {
        HashOperations<String, String, Object> hashOperations = redisTemplate.opsForHash();
        return hashOperations.entries(key);
    }
 
    public void hMSet(String key, Map<String, Object> map) {
        hMSet(key, map, DEFAULT_EXPIRE);
    }
 
    public void hMSet(String key, Map<String, Object> map, long expire) {
        redisTemplate.opsForHash().putAll(key, map);
 
        if (expire != NOT_EXPIRE) {
            expire(key, expire);
        }
    }
 
    public void hSet(String key, String field, Object value) {
        hSet(key, field, value, DEFAULT_EXPIRE);
    }
 
    public void hSet(String key, String field, Object value, long expire) {
        redisTemplate.opsForHash().put(key, field, value);
 
        if (expire != NOT_EXPIRE) {
            expire(key, expire);
        }
    }
 
    public void expire(String key, long expire) {
        redisTemplate.expire(key, expire, TimeUnit.SECONDS);
    }
 
    public void hDel(String key, Object... fields) {
        redisTemplate.opsForHash().delete(key, fields);
    }
 
    public void leftPush(String key, Object value) {
        leftPush(key, value, DEFAULT_EXPIRE);
    }
 
    public void leftPush(String key, Object value, long expire) {
        redisTemplate.opsForList().leftPush(key, value);
 
        if (expire != NOT_EXPIRE) {
            expire(key, expire);
        }
    }
 
    public Object rightPop(String key) {
        return redisTemplate.opsForList().rightPop(key);
    }
 
 
    /**
     * 清空所有 Redis 数据库中的所有键
     */
    public void emptyAll() {
        // Lua 脚本 FLUSHALL是redis清空所有库的命令
        String luaScript =resourceUtils.loadString("lua/emptyAll.lua");
 
        // 创建 DefaultRedisScript 对象
        DefaultRedisScript<Void> redisScript = new DefaultRedisScript<>();
        redisScript.setScriptText(luaScript); // 设置 Lua 脚本内容
        redisScript.setResultType(Void.class); // 设置返回值类型
 
        // 执行 Lua 脚本
        List<String> keys = Collections.emptyList(); // 如果脚本不依赖 key,可以传入空列表
        redisTemplate.execute(redisScript, keys);
 
    }
 
    /**
     * 获取在redis指定key的值,如果值为空,着设置key的默认值
     * @param key redis的key
     * @param defaultValue 默认值
     * @param expiresInSecond 过期时间
     * @return 返回key的值
     */
    public String getKeyOrCreate(String key, String defaultValue,Long expiresInSecond) {
        // Lua 脚本
        String luaScript = resourceUtils.loadString("lua/getKeyOrCreate.lua");
 
        DefaultRedisScript<String> redisScript = new DefaultRedisScript<>();
        redisScript.setScriptText(luaScript);
        redisScript.setResultType(String.class);
 
        // 执行 Lua 脚本
        List<String> keys = Collections.singletonList(key);
        return redisTemplate.execute(redisScript, keys, defaultValue,expiresInSecond);
    }
 
 
 
}