package com.link.constants;
/**
* 表名枚举
*
* @date: 2023/03/28
* @author: xkc
*/
public enum TableNameEnum {
//列举四个数据库
OP_TOKEN("token", "id"),
OP_VIEW("view", "id"),
DCE_FILE("file", "id"),
OP_UPLOAD("upload", "id");
private String table;
private String tableId;
//枚举的操作
TableNameEnum(String table, String tableId) {
this.table = table;
this.tableId = tableId;
}
public String getTable() {
return table;
}
public void setTable(String table) {
this.table = table;
}
public String getTableId() {
return tableId;
}
public void setTableId(String tableId) {
this.tableId = tableId;
}
}
redis工具类及说明,可以适配字符串及map
package com.link.config;
import com.alibaba.fastjson.JSONObject;
import com.link.util.PageData;
import org.apache.commons.lang.StringUtils;
import java.util.Map;
/**
* 数据缓存(redis)处理
* 缓存方案:需要以 分号 结尾
* 前缀:cache_db
* 中间部分:表名
* 自定义部分:key
*
* @date: 2023/03/28
* @author: xkc
*/
public class test {
/**
* 缓存前缀
*/
public static final String CACHE_DB_PREFIX = "cache_db";
/**
* 条件查询前缀
*/
public static final String CRITERIA_QUERY_PREFIX = "criteria_query";
/**
* 默认缓存时间:3600s 秒
*/
public static final Long CACHE_TM = 3600L;
/**
* 缓存,根据表名和id设置缓存中的map
*
* @param table 表名
* @param cusKey 自定义key
* @param map 缓存value
*/
public static void set(String table, String cusKey, Map map) {
String cacheDbKey = getCacheDbKey(table, cusKey);
set(cacheDbKey, map, CACHE_TM);
}
/**
* 缓存(条件查询),根据表名和id获取缓存中的map
*
* @param table 表名
* @param cusKey 自定义key
* @param dbId 缓存value db 主键id
*/
public static void setQuery(String table, String cusKey, String dbId) {
setQuery(table, cusKey, dbId, CACHE_TM);
}
/**
* 缓存(条件查询)有效期,根据表名和id设置缓存中的map和有效期
*
* @param table 表名
* @param cusKey 自定义key
* @param dbId 缓存value db 主键id
*/
public static void setQuery(String table, String cusKey, String dbId, Long tm) {
String cacheDbKey = getCacheDbKey(table, cusKey, Boolean.TRUE);
CacheUtil.set(cacheDbKey, dbId, tm);
}
/**
* 缓存(有效期),设置key有效期
*
* @param cacheDbKey 缓存key
* @param map 缓存value
* @param tm 有效期
*/
public static void set(String cacheDbKey, Map map, Long tm) {
String value = mapToJsonStr(map);
CacheUtil.set(cacheDbKey, value, tm);
}
/**
* 缓存(有效期),根据表名和id设置map和有效期
*
* @param table 表名
* @param cusKey 自定义key
* @param map 缓存value
* @param tm 有效期
*/
public static void set(String table, String cusKey, Map map, Long tm) {
String cacheDbKey = getCacheDbKey(table, cusKey);
String value = mapToJsonStr(map);
CacheUtil.set(cacheDbKey, value, tm);
}
/**
* 根据表名和id获取缓存
*
* @param table 表名
* @param cusKey 自定义key
* @return db json字符串
*/
public static String getStr(String table, String cusKey) {
String cacheDbKey = getCacheDbKey(table, cusKey);
return (String) CacheUtil.get(cacheDbKey);
}
/**
* 获取缓存 Map
*
* @param table 表名
* @param cusKey 自定义key
* @return map对象
*/
public static Map getMap(String table, String cusKey) {
String value = getStr(table, cusKey);
return jsonStrToMap(value);
}
/**
* 删除缓存
*
* @param table 表名
* @param cusKey 自定义key
*/
public static void del(String table, String cusKey) {
String cacheDbKey = getCacheDbKey(table, cusKey);
CacheUtil.del(cacheDbKey);
}
/**
* 获取缓存前缀table
*
* @param table 表名
* @return
*/
public static StringBuilder getCacheDbPreTable(String table) {
StringBuilder sb = new StringBuilder();
sb.append(CACHE_DB_PREFIX).append(":");
if (StringUtils.isNotBlank(table)) {
sb.append(table).append(":");
}
return sb;
}
/**
* 获取缓存key
*
* @param table 表名
* @param cusKey 自定义key
* @return 缓存key
*/
public static String getCacheDbKey(String table, String cusKey) {
StringBuilder sb = getCacheDbPreTable(table);
sb.append(cusKey);
return sb.toString();
}
/**
* 获取缓存key
*
* @param table 表名
* @param cusKey 自定义key
* @param isQuery 标识条件查询
* @return 缓存key
*/
public static String getCacheDbKey(String table, String cusKey, Boolean isQuery) {
StringBuilder sb = getCacheDbPreTable(table);
if (Boolean.TRUE == isQuery) {
sb.append(CRITERIA_QUERY_PREFIX).append(":");
}
sb.append(cusKey);
return sb.toString();
}
/**
* 自定义map 对象 转json字符串
*
* @param map Map 对象
* @return json字符串
*/
public static String mapToJsonStr(Map map) {
if (null == map ){
return "";
}
return JSONObject.toJSONString(map);
}
/**
* json字符串 转 自定义map 对象
*
* @param jsonStr json字符串
* @return PageData对象
*/
public static Map jsonStrToMap(String jsonStr) {
if (StringUtils.isBlank(jsonStr)) {
return null;
}
return JSONObject.parseObject(jsonStr, Map.class);
}
}
//模糊查询的最佳业务代码实现redis查询
public static List<LocalEditorFile> getKeys(String patternKey){
//需要匹配的key
//String patternKey = "pay:*";
//patternKey = patternKey+ "*";
ScanOptions options = ScanOptions.scanOptions()
//这里指定每次扫描key的数量
.count(10000)
.match(patternKey+ "*").build();
RedisSerializer<String> redisSerializer = (RedisSerializer<String>) redisTemplate.getKeySerializer();
Cursor cursor = (Cursor) redisTemplate.executeWithStickyConnection(redisConnection -> new ConvertingCursor<>(redisConnection.scan(options), redisSerializer::deserialize));
List<LocalEditorFile> result = new ArrayList<>();
LocalEditorFile lef = null;
//切记这里一定要关闭,否则会耗尽连接数。
try {
String str = "";
String localPath = "";
while(cursor.hasNext()){
lef =new LocalEditorFile();
str = cursor.next().toString();
localPath = str.replace(patternKey,"");
localPath = localPath.replaceAll("-xKc%"," ");
//str = str.replaceAll(" ","-xKc%");
if(str!=null){
lef.setLocalPath(localPath);
lef.setFileDownloadUrl((String)get(str));
result.add(lef);
}
}
cursor.close();
}catch (Exception e){
System.out.println(e);
try{
cursor.close();
}catch (IOException ioe){
}
}
return result;
}
项目的使用:
Map map= new Map();
map.put("xkc","test");
//设置表中的map,用于代替数据库的缓存
CacheDbUtil.set(TableNameEnum.FILE.getTable(), d,map);
//获取redis中的map数据
CacheDbUtil.getMap(TableNameEnum.FILE.getTable(),id);