mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-23 23:53:55 +08:00
* 添加清空redis所有库的接口 --AdminController.java 添加了清除所有的接口 --RedisUtils.java 添加了清除redis所有key的方法,redisTemplate提供的清空方法已经被标记为弃用了,所有选择用执行lua脚本方式 * fix:修复使用本地配置时忘记附带提示词 * fix:修复意图识别插件名称格式bug * update:版本升级后强制刷新redis * update:增加单台设备每天最多聊天字数,防止被ddos --------- Co-authored-by: 剑雨 <2375294554@qq.com>
150 lines
4.1 KiB
Java
150 lines
4.1 KiB
Java
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.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;
|
|
|
|
/**
|
|
* Redis工具类
|
|
* Copyright (c) 人人开源 All rights reserved.
|
|
* Website: https://www.renren.io
|
|
*/
|
|
@Component
|
|
public class RedisUtils {
|
|
@Resource
|
|
private RedisTemplate<String, Object> redisTemplate;
|
|
|
|
/**
|
|
* 默认过期时长为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 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 ="redis.call('FLUSHALL')";
|
|
|
|
// 创建 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);
|
|
|
|
}
|
|
|
|
} |