优化清空redis的key方法。添加加1,减1方法

--RedisUtils.java 新的方法
--emptyAll.lua 清空redis的key的lua脚本
This commit is contained in:
剑雨
2025-05-19 11:20:57 +08:00
parent c19084b7b4
commit 83c4add375
2 changed files with 20 additions and 1 deletions
@@ -45,6 +45,24 @@ public class RedisUtils {
*/
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) {
@@ -139,7 +157,7 @@ public class RedisUtils {
*/
public void emptyAll() {
// Lua 脚本 FLUSHALL是redis清空所有库的命令
String luaScript ="redis.call('FLUSHALL')";
String luaScript =resourceUtils.loadString("lua/emptyAll.lua");
// 创建 DefaultRedisScript 对象
DefaultRedisScript<Void> redisScript = new DefaultRedisScript<>();
@@ -0,0 +1 @@
redis.call('FLUSHALL')