重构缓存架构

v1.4.1
Parker 4 years ago
parent 08efd7d8b0
commit a1e5291d53

@ -2,6 +2,7 @@ package org.opsli.core.cache.local;
import cn.hutool.core.util.XmlUtil;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
@ -16,6 +17,8 @@ import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import java.util.List;
/**
* @BelongsProject: opsli-boot
@ -36,6 +39,9 @@ import org.w3c.dom.NodeList;
@Component
public class CacheUtil {
/** 空状态 key 前缀 */
private static final String NIL_FLAG_PREFIX = "opsli:nil:";
/** 热点数据缓存时间 秒 */
private static int ttlHotData;
/** Redis插件 */
@ -73,19 +79,198 @@ public class CacheUtil {
public static <V> V getByKeyOriginal(String key, Class<V> vClass){
return CacheUtil.get(CacheConstants.HOT_DATA,key,vClass,false);
}
/**
* -
* Key
* jksahdjh1j1hjk1
*
* @param key
* @param vClass
* @return V
*/
public static <V> V getEden(String key, Class<V> vClass){
return CacheUtil.get(CacheConstants.EDEN_DATA,key,vClass,true);
}
/**
* -
* Key
* Key opsli:hotData:ahdjksahjkd1
*
* @param key
* @param vClass
* @return V
*/
public static <V> V getEdenByKeyOriginal(String key, Class<V> vClass){
return CacheUtil.get(CacheConstants.EDEN_DATA,key,vClass,false);
}
/**
* Hash
* Key
* jksahdjh1j1hjk1
*
* @param key
* @param field
* @param vClass
* @return V
*/
public static <V> V getHash(String key, String field, Class<V> vClass){
return CacheUtil.getHash(CacheConstants.EDEN_HASH_DATA,key,field,vClass,true);
}
/**
* Hash
* Key
* Key opsli:hotData:ahdjksahjkd1
*
* @param key
* @param field
* @param vClass
* @return V
*/
public static <V> V getHashByKeyOriginal(String key, String field, Class<V> vClass){
return CacheUtil.getHash(CacheConstants.EDEN_HASH_DATA,key,field,vClass,false);
}
/**
* -
* Key
* jksahdjh1j1hjk1
*
* @param keys -
* @param vClass
* @return V
*/
public static <V> List<V> getAll(List<String> keys, Class<V> vClass){
return CacheUtil.getAll(CacheConstants.HOT_DATA,keys,vClass,true);
}
/**
* -
* Key
* Key opsli:hotData:ahdjksahjkd1
*
* @param keys -
* @param vClass
* @return V
*/
public static <V> List<V> getAllByKeyOriginal(List<String> keys, Class<V> vClass){
return CacheUtil.getAll(CacheConstants.HOT_DATA,keys,vClass,false);
}
/**
* - -
* Key
* jksahdjh1j1hjk1
*
* @param keys -
* @param vClass
* @return V
*/
public static <V> List<V> getEdenAll(List<String> keys, Class<V> vClass){
return CacheUtil.getAll(CacheConstants.EDEN_DATA,keys,vClass,true);
}
/**
* - -
* Key
* Key opsli:hotData:ahdjksahjkd1
*
* @param keys -
* @param vClass
* @return V
*/
public static <V> List<V> getEdenAllByKeyOriginal(List<String> keys, Class<V> vClass){
return CacheUtil.getAll(CacheConstants.EDEN_DATA,keys,vClass,false);
}
/**
* Hash
* Key
* jksahdjh1j1hjk1
*
* @param key
* @param fields
* @param vClass
* @return V
*/
public static <V> List<V> getHashAll(String key, List<String> fields, Class<V> vClass){
return CacheUtil.getHashAll(CacheConstants.EDEN_HASH_DATA,key,fields,vClass,true);
}
/**
* Hash
* Key
* Key opsli:hotData:ahdjksahjkd1
*
* @param key
* @param fields
* @param vClass
* @return V
*/
public static <V> List<V> getHashAllByKeyOriginal(String key, List<String> fields, Class<V> vClass){
return CacheUtil.getHashAll(CacheConstants.EDEN_HASH_DATA,key,fields,vClass,false);
}
/**
* Hash
* @param cacheName
* @param key
* @param field
* @param vClass Clazz
* @param keyFlag key
* @param <V>
* @return
*/
private static <V> V getHash(String cacheName, String key, String field, Class<V> vClass,boolean keyFlag){
// 自动处理 key
if(keyFlag){
key = CacheUtil.handleKey(cacheName, key);
}
V v = null;
try {
JSONObject jsonObject;
jsonObject = ehCachePlugin.get(CacheConstants.HOT_DATA, key+":"+field, JSONObject.class);
// 如果本地缓存为空 则去Redis中 再去取一次
if(jsonObject != null){
v = jsonObject.toJavaObject(vClass);
}else{
jsonObject = (JSONObject) redisPlugin.hGet(key,field);
if(jsonObject != null){
// 存入EhCache
ehCachePlugin.put(CacheConstants.HOT_DATA, key+":"+field, jsonObject);
v = jsonObject.toJavaObject(vClass);
}
}
}catch (Exception e){
log.error(e.getMessage(),e);
}
return v;
}
/**
* Hash
* @param cacheName
* @param key
* @param vClass Clazz
* @param keyFlag key
* @param <V>
* @return
*/
private static <V> V get(String cacheName, String key, Class<V> vClass,boolean keyFlag){
String KeyOriginal = key;
// 自动处理 key
if(keyFlag){
StringBuilder keyBuf = new StringBuilder(CacheDataAop.PREFIX_NAME);
keyBuf.append(cacheName).append(":");
keyBuf.append(key);
key = keyBuf.toString();
key = CacheUtil.handleKey(cacheName, key);
}
V v = null;
try {
JSONObject jsonObject;
jsonObject = ehCachePlugin.get(cacheName, key, JSONObject.class);
jsonObject = ehCachePlugin.get(CacheConstants.HOT_DATA, key, JSONObject.class);
// 如果本地缓存为空 则去Redis中 再去取一次
if(jsonObject != null){
v = jsonObject.toJavaObject(vClass);
@ -93,7 +278,7 @@ public class CacheUtil {
jsonObject = (JSONObject) redisPlugin.get(key);
if(jsonObject != null){
// 存入EhCache
ehCachePlugin.put(CacheConstants.HOT_DATA, KeyOriginal, jsonObject);
ehCachePlugin.put(CacheConstants.HOT_DATA, key, jsonObject);
v = jsonObject.toJavaObject(vClass);
}
}
@ -103,7 +288,91 @@ public class CacheUtil {
return v;
}
/**
* -
* @param cacheName
* @param keys
* @param vClass Clazz
* @param keyFlag key
* @param <V>
* @return
*/
private static <V> List<V> getAll(String cacheName, List<String> keys, Class<V> vClass, boolean keyFlag){
List<V> vs = Lists.newArrayList();
try {
List<String> nokeys = Lists.newArrayList();
for (String key : keys) {
// 自动处理 key
if(keyFlag){
key = CacheUtil.handleKey(cacheName, key);
}
JSONObject jsonObject;
jsonObject = ehCachePlugin.get(CacheConstants.HOT_DATA, key, JSONObject.class);
if(jsonObject != null){
vs.add(jsonObject.toJavaObject(vClass));
} else {
nokeys.add(key);
}
}
// 如果本地缓存为空 则去Redis中 再去取一次
if(nokeys.size() > 0){
List<Object> objs = redisPlugin.getAll(nokeys);
for (Object obj : objs) {
JSONObject jsonObject = (JSONObject) obj;
if(jsonObject != null){
vs.add(jsonObject.toJavaObject(vClass));
}
}
}
}catch (Exception e){
log.error(e.getMessage(),e);
}
return vs;
}
/**
* Hash -
* @param cacheName
* @param key
* @param fields
* @param vClass Clazz
* @param keyFlag key
* @param <V>
* @return
*/
private static <V> List<V> getHashAll(String cacheName,String key,List<String> fields, Class<V> vClass, boolean keyFlag){
// 自动处理 key
if(keyFlag){
key = CacheUtil.handleKey(cacheName, key);
}
List<V> vs = Lists.newArrayList();
try {
List<Object> nofields = Lists.newArrayList();
for (String field : fields) {
JSONObject jsonObject;
jsonObject = ehCachePlugin.get(CacheConstants.HOT_DATA, key+":"+field, JSONObject.class);
if(jsonObject != null){
vs.add(jsonObject.toJavaObject(vClass));
} else {
nofields.add(field);
}
}
// 如果本地缓存为空 则去Redis中 再去取一次
if(nofields.size() > 0){
List<Object> objs = redisPlugin.hMultiGet(key, nofields);
for (Object obj : objs) {
JSONObject jsonObject = (JSONObject) obj;
if(jsonObject != null){
vs.add(jsonObject.toJavaObject(vClass));
}
}
}
}catch (Exception e){
log.error(e.getMessage(),e);
}
return vs;
}
// ========================= PUT - 热点区 =========================
@ -164,15 +433,90 @@ public class CacheUtil {
return CacheUtil.put(CacheConstants.EDEN_DATA, key, value, null, false, false);
}
// ========================= PUT - 永久代 =========================
/**
* Hash -
* RedisEhCache
* Redis
*
* @param key
* @param value
* @return boolean
*/
@Deprecated
public static boolean putEdenHash(String key, String field, Object value) {
return CacheUtil.putEdenHash(CacheConstants.EDEN_HASH_DATA, key, field, value, true);
}
/**
* Hash -
* RedisEhCache
* Redis
*
* @param key
* @param value
* @return boolean
*/
@Deprecated
public static boolean putEdenHashByKeyOriginal(String key, String field, Object value) {
return CacheUtil.putEdenHash(CacheConstants.EDEN_HASH_DATA, key, field, value, false);
}
/**
* Hash
* @param cacheName
* @param key
* @param field
* @param value
* @param keyFlag key
* @return
*/
private static boolean putEdenHash(String cacheName, String key, String field, Object value, boolean keyFlag) {
boolean ret = false;
try {
// 自动处理 key
if(keyFlag){
key = CacheUtil.handleKey(cacheName, key);
}
// 如果不是 JSONObject 则统一转换为 JSONObject
if(!(value instanceof JSONObject)){
String jsonStr = JSONObject.toJSONString(value);
value = JSONObject.parseObject(jsonStr);
}
// 存入EhCache
ehCachePlugin.put(CacheConstants.HOT_DATA, key+":"+field, value);
// 存入Redis
redisPlugin.hPut(key, field, value);
ret = true;
}catch (Exception e){
log.error(e.getMessage(),e);
}
return ret;
}
/**
*
* @param cacheName
* @param key
* @param value
* @param timeout
* @param timeFlag
* @param keyFlag key
* @return
*/
private static boolean put(String cacheName, String key, Object value,Integer timeout, boolean timeFlag, boolean keyFlag) {
boolean ret = false;
try {
// 自动处理 key
if(keyFlag){
StringBuilder keyBuf = new StringBuilder(CacheDataAop.PREFIX_NAME);
keyBuf.append(cacheName).append(":");
keyBuf.append(key);
key = keyBuf.toString();
key = CacheUtil.handleKey(cacheName, key);
}
// 如果不是 JSONObject 则统一转换为 JSONObject
@ -212,15 +556,100 @@ public class CacheUtil {
// ========================= DEL - 删除 =========================
/**
*
*
*
* Key
* Key opsli:hotData:ahdjksahjkd1
*
* @param key
* @return boolean
*/
public static boolean del(String key) {
return CacheUtil.del(CacheConstants.HOT_DATA, key, true);
}
/**
*
*
* Key
* Key opsli:hotData:ahdjksahjkd1
*
* @param key
* @return boolean
*/
public static boolean delByKeyOriginal(String key) {
return CacheUtil.del(CacheConstants.HOT_DATA, key, false);
}
/**
* Hash
*
* Key
* Key opsli:hotData:ahdjksahjkd1
*
* @param key
* @param field
* @return boolean
*/
public static boolean delEdenHash(String key, String field) {
return CacheUtil.delEdenHash(CacheConstants.EDEN_HASH_DATA, key, field, true);
}
/**
* Hash
*
* Key
* Key opsli:hotData:ahdjksahjkd1
*
* @param key
* @param field
* @return boolean
*/
public static boolean delEdenHashByKeyOriginal(String key, String field) {
return CacheUtil.delEdenHash(CacheConstants.EDEN_HASH_DATA, key, field, false);
}
/**
* Hash
* @param cacheName
* @param key
* @param field
* @param keyFlag Key
* @return boolean
*/
private static boolean delEdenHash(String cacheName, String key, String field, boolean keyFlag) {
boolean ret = false;
try {
// 自动处理 key
if(keyFlag){
key = CacheUtil.handleKey(cacheName, key);
}
// 删除 EhCache 不论是什么 本地都按照热数据处理
ehCachePlugin.delete(CacheConstants.HOT_DATA,key+":"+field);
// 删除 Redis
redisPlugin.hDelete(key, field);
ret = true;
}catch (Exception e){
log.error(e.getMessage(),e);
}
return ret;
}
/**
*
* @param cacheName
* @param key
* @param keyFlag Key
* @return boolean
*/
private static boolean del(String cacheName, String key, boolean keyFlag) {
boolean ret = false;
try {
// 删除 EhCache
// 自动处理 key
if(keyFlag){
key = CacheUtil.handleKey(cacheName, key);
}
// 删除 EhCache 不论是什么 本地都按照热数据处理
ehCachePlugin.delete(CacheConstants.HOT_DATA,key);
// 删除 Redis
redisPlugin.del(key);
@ -231,16 +660,64 @@ public class CacheUtil {
return ret;
}
// ====================================================================
/**
* 5
* 穿
*
* @param key
* @return boolean
*/
public static boolean putNilFlag(String key) {
boolean ret = false;
try {
// 存入Redis
redisPlugin.put(NIL_FLAG_PREFIX + key, 1, 300);
ret = true;
}catch (Exception e){
log.error(e.getMessage(),e);
}
return ret;
}
/**
* 5
* 穿
*
* @param key
* @return boolean
*/
public static boolean hasNilFlag(String key) {
try {
// 存入Redis
Object o = redisPlugin.get(NIL_FLAG_PREFIX + key);
if(o != null){
return true;
}
return false;
}catch (Exception e){
log.error(e.getMessage(),e);
}
return false;
}
// ====================================================================
/**
* key
* @param cacheName
* @param key
* @return
*/
public static String handleKey(String cacheName, String key){
StringBuilder keyBuf = new StringBuilder(CacheDataAop.PREFIX_NAME);
keyBuf.append(cacheName).append(":");
keyBuf.append(key);
return keyBuf.toString();
}
/**
*
*/

Loading…
Cancel
Save