优化缓存系统

v1.4.1
Parker 5 years ago
parent bea683f578
commit d1a95d6a76

@ -32,6 +32,7 @@ import org.opsli.api.base.warpper.ApiWrapper;
import org.opsli.api.wrapper.system.user.UserModel; import org.opsli.api.wrapper.system.user.UserModel;
import org.opsli.common.annotation.RequiresPermissionsCus; import org.opsli.common.annotation.RequiresPermissionsCus;
import org.opsli.common.annotation.hotdata.EnableHotData; import org.opsli.common.annotation.hotdata.EnableHotData;
import org.opsli.common.constants.CacheConstants;
import org.opsli.common.exception.ServiceException; import org.opsli.common.exception.ServiceException;
import org.opsli.common.exception.TokenException; import org.opsli.common.exception.TokenException;
import org.opsli.common.msg.CommonMsg; import org.opsli.common.msg.CommonMsg;
@ -119,7 +120,8 @@ public abstract class BaseRestController <T extends BaseEntity, E extends ApiWra
// 如果开启缓存 先从缓存读 // 如果开启缓存 先从缓存读
if(hotDataFlag){ if(hotDataFlag){
model = WrapperUtil.transformInstance( model = WrapperUtil.transformInstance(
CacheUtil.getTimed(entityClazz, id), modelClazz); CacheUtil.getTimed(entityClazz, CacheConstants.HOT_DATA_PREFIX +":"+ id)
, modelClazz);
if(model != null){ if(model != null){
return model; return model;
} }
@ -147,7 +149,8 @@ public abstract class BaseRestController <T extends BaseEntity, E extends ApiWra
// 如果获得锁 则 再次检查缓存里有没有, 如果有则直接退出, 没有的话才发起数据库请求 // 如果获得锁 则 再次检查缓存里有没有, 如果有则直接退出, 没有的话才发起数据库请求
model = WrapperUtil.transformInstance( model = WrapperUtil.transformInstance(
CacheUtil.getTimed(entityClazz, id),modelClazz); CacheUtil.getTimed(entityClazz, CacheConstants.HOT_DATA_PREFIX +":"+ id)
, modelClazz);
if(model != null){ if(model != null){
return model; return model;
} }

@ -74,7 +74,7 @@ public class CacheUtil {
private static final String NIL_FLAG_PREFIX = "nil"; private static final String NIL_FLAG_PREFIX = "nil";
/** 热点数据前缀 */ /** 热点数据前缀 */
public static String PREFIX_NAME; private static String PREFIX_NAME;
static { static {
try { try {
@ -83,6 +83,13 @@ public class CacheUtil {
}catch (Exception ignored){} }catch (Exception ignored){}
} }
/***
*
* @return
*/
public static String getPrefixName() {
return PREFIX_NAME;
}
// ========================= GET ========================= // ========================= GET =========================
@ -408,19 +415,30 @@ public class CacheUtil {
public static boolean del(final String key) { public static boolean del(final String key) {
try { try {
// 计数器 // 计数器
int count = 4; int count = 0;
Object timed = CacheUtil.getTimed(key);
Object eden = CacheUtil.getEden(key);
// 删除key 集合 // 删除key 集合
List<String> cacheKeys = Lists.newArrayListWithCapacity(2); List<String> cacheKeys = Lists.newArrayList();
if(timed != null){
count+=2;
// 处理 key - 时控数据 // 处理 key - 时控数据
cacheKeys.add( cacheKeys.add(
CacheUtil.handleKey(CacheType.TIMED, key)); CacheUtil.handleKey(CacheType.TIMED, key)
);
}
if(eden != null){
count+=2;
// 处理 key - 永久数据 // 处理 key - 永久数据
cacheKeys.add( cacheKeys.add(
CacheUtil.handleKey(CacheType.TIMED, key)); CacheUtil.handleKey(CacheType.EDEN, key));
}
// 循环删除缓存数据 // 循环删除缓存数据
for (String cacheKey : cacheKeys) { for (String cacheKey : cacheKeys) {
// 删除 EhCache // 删除 EhCache
boolean ehcacheRet = ehCachePlugin.delete(CacheConstants.EHCACHE_SPACE, cacheKey); boolean ehcacheRet = ehCachePlugin.delete(CacheConstants.EHCACHE_SPACE, cacheKey);
if(ehcacheRet){ if(ehcacheRet){
@ -571,8 +589,6 @@ public class CacheUtil {
} }
/** /**
* TODO XML
*
* *
*/ */
private static void readPropertyXML() throws IOException { private static void readPropertyXML() throws IOException {

@ -36,12 +36,9 @@ public class CacheDataEntity {
/** key */ /** key */
private String key; private String key;
/** 缓存名称 */
private String cacheName;
public static void main(String[] args) { public static void main(String[] args) {
CacheDataEntity ret = new CacheDataEntity("123", "12aaaa"); CacheDataEntity ret = new CacheDataEntity("123");
System.out.println(ToStringBuilder.reflectionToString(ret)); System.out.println(ToStringBuilder.reflectionToString(ret));
} }
} }

@ -59,8 +59,6 @@ public enum MsgArgsType {
/** 缓存数据Key */ /** 缓存数据Key */
CACHE_DATA_KEY, CACHE_DATA_KEY,
/** 缓存数据Key */
CACHE_DATA_NAME,
/** 缓存数据Value */ /** 缓存数据Value */
CACHE_DATA_VALUE, CACHE_DATA_VALUE,
/** 缓存数据Type */ /** 缓存数据Type */

@ -17,7 +17,9 @@ package org.opsli.core.cache.pushsub.handler;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.opsli.common.constants.CacheConstants; import org.opsli.common.constants.CacheConstants;
import org.opsli.core.cache.local.CacheUtil;
import org.opsli.core.cache.pushsub.enums.CacheHandleType; import org.opsli.core.cache.pushsub.enums.CacheHandleType;
import org.opsli.core.cache.pushsub.enums.MsgArgsType; import org.opsli.core.cache.pushsub.enums.MsgArgsType;
import org.opsli.core.cache.pushsub.enums.PushSubType; import org.opsli.core.cache.pushsub.enums.PushSubType;
@ -45,10 +47,16 @@ public class HotDataHandler implements RedisPushSubHandler{
@Override @Override
public void handler(JSONObject msgJson) { public void handler(JSONObject msgJson) {
String key = (String) msgJson.get(MsgArgsType.CACHE_DATA_KEY.toString()); String key = (String) msgJson.get(MsgArgsType.CACHE_DATA_KEY.toString());
String cacheName = (String) msgJson.get(MsgArgsType.CACHE_DATA_NAME.toString());
Object value = msgJson.get(MsgArgsType.CACHE_DATA_VALUE.toString()); Object value = msgJson.get(MsgArgsType.CACHE_DATA_VALUE.toString());
CacheHandleType type = CacheHandleType.valueOf((String )msgJson.get(MsgArgsType.CACHE_DATA_TYPE.toString())); CacheHandleType type = CacheHandleType.valueOf((String )msgJson.get(MsgArgsType.CACHE_DATA_TYPE.toString()));
if(StringUtils.isEmpty(key)){
return;
}
// 拼装缓存key
String cacheName = CacheUtil.handleKey(CacheConstants.HOT_DATA_PREFIX +":"+ key);
if(CacheHandleType.UPDATE == type){ if(CacheHandleType.UPDATE == type){
ehCachePlugin.put(CacheConstants.EHCACHE_SPACE, cacheName, value); ehCachePlugin.put(CacheConstants.EHCACHE_SPACE, cacheName, value);
} }

@ -50,7 +50,6 @@ public final class CacheDataMsgFactory extends BaseSubMessage{
// 数据 // 数据
JSONObject jsonObj = new JSONObject(); JSONObject jsonObj = new JSONObject();
jsonObj.put(MsgArgsType.CACHE_DATA_KEY.toString(),cacheDataEntity.getKey()); jsonObj.put(MsgArgsType.CACHE_DATA_KEY.toString(),cacheDataEntity.getKey());
jsonObj.put(MsgArgsType.CACHE_DATA_NAME.toString(),cacheDataEntity.getCacheName());
jsonObj.put(MsgArgsType.CACHE_DATA_VALUE.toString(),value); jsonObj.put(MsgArgsType.CACHE_DATA_VALUE.toString(),value);
jsonObj.put(MsgArgsType.CACHE_DATA_TYPE.toString(), cacheHandleType.toString()); jsonObj.put(MsgArgsType.CACHE_DATA_TYPE.toString(), cacheHandleType.toString());

@ -28,6 +28,7 @@ import org.opsli.api.base.warpper.ApiWrapper;
import org.opsli.common.annotation.hotdata.EnableHotData; import org.opsli.common.annotation.hotdata.EnableHotData;
import org.opsli.common.annotation.hotdata.HotDataDel; import org.opsli.common.annotation.hotdata.HotDataDel;
import org.opsli.common.annotation.hotdata.HotDataPut; import org.opsli.common.annotation.hotdata.HotDataPut;
import org.opsli.common.constants.CacheConstants;
import org.opsli.core.cache.local.CacheUtil; import org.opsli.core.cache.local.CacheUtil;
import org.opsli.core.cache.pushsub.entity.CacheDataEntity; import org.opsli.core.cache.pushsub.entity.CacheDataEntity;
import org.opsli.core.cache.pushsub.enums.CacheHandleType; import org.opsli.core.cache.pushsub.enums.CacheHandleType;
@ -83,9 +84,7 @@ public class CacheDataAop {
return returnValue; return returnValue;
} }
String simpleName = point.getTarget().getClass().getSimpleName(); List<CacheDataEntity> cacheDataEntityList = this.putHandlerData(point, returnValue);
List<CacheDataEntity> cacheDataEntityList = this.putHandlerData(point, returnValue, simpleName);
// 非法判断 // 非法判断
if(CollUtil.isEmpty(cacheDataEntityList)){ if(CollUtil.isEmpty(cacheDataEntityList)){
return returnValue; return returnValue;
@ -94,14 +93,16 @@ public class CacheDataAop {
for (CacheDataEntity cacheDataEntity : cacheDataEntityList) { for (CacheDataEntity cacheDataEntity : cacheDataEntityList) {
// 更新缓存数据 // 更新缓存数据
// 热点数据 // 热点数据
CacheUtil.put(cacheDataEntity.getKey(), returnValue); boolean putRet = CacheUtil.put(CacheConstants.HOT_DATA_PREFIX +":"+ cacheDataEntity.getKey(),
returnValue);
if(putRet){
// 广播缓存数据 - 通知其他服务器同步数据 // 广播缓存数据 - 通知其他服务器同步数据
redisPlugin.sendMessage( redisPlugin.sendMessage(
CacheDataMsgFactory.createMsg( CacheDataMsgFactory.createMsg(
cacheDataEntity, returnValue, CacheHandleType.UPDATE) cacheDataEntity, returnValue, CacheHandleType.UPDATE)
); );
} }
}
return returnValue; return returnValue;
} }
@ -123,8 +124,6 @@ public class CacheDataAop {
return returnValue; return returnValue;
} }
String simpleName = point.getTarget().getClass().getSimpleName();
// 删除状态判断 // 删除状态判断
try { try {
Boolean ret = (Boolean) returnValue; Boolean ret = (Boolean) returnValue;
@ -136,7 +135,7 @@ public class CacheDataAop {
return returnValue; return returnValue;
} }
List<CacheDataEntity> cacheDataEntityList = this.delHandlerData(point, args, simpleName); List<CacheDataEntity> cacheDataEntityList = this.delHandlerData(point, args);
// 非法判断 // 非法判断
if(CollUtil.isEmpty(cacheDataEntityList)){ if(CollUtil.isEmpty(cacheDataEntityList)){
return returnValue; return returnValue;
@ -144,14 +143,15 @@ public class CacheDataAop {
for (CacheDataEntity cacheDataEntity : cacheDataEntityList) { for (CacheDataEntity cacheDataEntity : cacheDataEntityList) {
// 更新缓存数据 - 删除缓存 // 更新缓存数据 - 删除缓存
CacheUtil.del(cacheDataEntity.getKey()); boolean delRet = CacheUtil.del(CacheConstants.HOT_DATA_PREFIX +":"+ cacheDataEntity.getKey());
if(delRet){
// 广播缓存数据 - 通知其他服务器同步数据 // 广播缓存数据 - 通知其他服务器同步数据
redisPlugin.sendMessage( redisPlugin.sendMessage(
CacheDataMsgFactory.createMsg( CacheDataMsgFactory.createMsg(
cacheDataEntity, returnValue, CacheHandleType.DELETE) cacheDataEntity, null, CacheHandleType.DELETE)
); );
} }
}
return returnValue; return returnValue;
} }
@ -164,7 +164,7 @@ public class CacheDataAop {
* PUT * PUT
* @param point * @param point
*/ */
private List<CacheDataEntity> putHandlerData(ProceedingJoinPoint point, Object returnValue, String simpleName){ private List<CacheDataEntity> putHandlerData(ProceedingJoinPoint point, Object returnValue){
// 这里 只对 继承了 ApiWrapper 的类做处理 // 这里 只对 继承了 ApiWrapper 的类做处理
if(!(returnValue instanceof ApiWrapper)){ if(!(returnValue instanceof ApiWrapper)){
return null; return null;
@ -187,10 +187,7 @@ public class CacheDataAop {
// 这里 只对 继承了 BaseEntity 的类做处理 // 这里 只对 继承了 BaseEntity 的类做处理
ApiWrapper apiWrapper = (ApiWrapper) returnValue; ApiWrapper apiWrapper = (ApiWrapper) returnValue;
// 热数据 前缀增加 方法类名称 减小ID 冲撞 CacheDataEntity ret = new CacheDataEntity(apiWrapper.getId());
String cacheKey = CacheUtil.handleKey(simpleName +":"+ apiWrapper.getId());
CacheDataEntity ret = new CacheDataEntity(apiWrapper.getId() , cacheKey);
// 存放数据 // 存放数据
this.putCacheData(cacheDataEntities, ret); this.putCacheData(cacheDataEntities, ret);
@ -207,7 +204,7 @@ public class CacheDataAop {
* DEL * DEL
* @param point * @param point
*/ */
private List<CacheDataEntity> delHandlerData(ProceedingJoinPoint point, Object[] args, String simpleName){ private List<CacheDataEntity> delHandlerData(ProceedingJoinPoint point, Object[] args){
if(args == null || args.length == 0){ if(args == null || args.length == 0){
return null; return null;
} }
@ -250,12 +247,9 @@ public class CacheDataAop {
} }
} }
if(keyList != null && CollUtil.isNotEmpty(keyList)){ if(keyList != null && CollUtil.isNotEmpty(keyList)){
for (String key : keyList) { for (String key : keyList) {
// 热数据 前缀增加 方法类名称 减小ID 冲撞 CacheDataEntity ret = new CacheDataEntity(key);
String cacheKey = CacheUtil.handleKey(simpleName +":"+ key);
CacheDataEntity ret = new CacheDataEntity(key , cacheKey);
// 存放数据 // 存放数据
this.putCacheData(cacheDataEntities, ret); this.putCacheData(cacheDataEntities, ret);
} }
@ -295,7 +289,7 @@ public class CacheDataAop {
*/ */
private void putCacheData(List<CacheDataEntity> cacheDataList, CacheDataEntity cacheData){ private void putCacheData(List<CacheDataEntity> cacheDataList, CacheDataEntity cacheData){
// 非法判断 // 非法判断
if(CollUtil.isEmpty(cacheDataList)){ if(cacheDataList == null){
return; return;
} }
cacheDataList.add(cacheData); cacheDataList.add(cacheData);

@ -65,7 +65,7 @@ public class CaptchaUtil{
//生成文字验证码 //生成文字验证码
String code = producer.createText(); String code = producer.createText();
boolean ret = redisPlugin.put(CacheUtil.PREFIX_NAME + PREFIX + uuid, code, TIME_OUT); boolean ret = redisPlugin.put(CacheUtil.getPrefixName() + PREFIX + uuid, code, TIME_OUT);
if(ret){ if(ret){
return producer.createImage(code); return producer.createImage(code);
@ -90,7 +90,7 @@ public class CaptchaUtil{
} }
// 验证码 // 验证码
String codeTemp = (String) redisPlugin.get(CacheUtil.PREFIX_NAME + PREFIX + uuid); String codeTemp = (String) redisPlugin.get(CacheUtil.getPrefixName() + PREFIX + uuid);
if(StringUtils.isEmpty(codeTemp)){ if(StringUtils.isEmpty(codeTemp)){
throw new TokenException(TokenMsg.EXCEPTION_CAPTCHA_NULL); throw new TokenException(TokenMsg.EXCEPTION_CAPTCHA_NULL);
} }
@ -114,7 +114,7 @@ public class CaptchaUtil{
} }
//删除验证码 //删除验证码
return redisPlugin.del(CacheUtil.PREFIX_NAME + PREFIX + uuid); return redisPlugin.del(CacheUtil.getPrefixName() + PREFIX + uuid);
} }

@ -25,7 +25,6 @@ import org.opsli.api.base.result.ResultVo;
import org.opsli.api.web.system.dict.DictDetailApi; import org.opsli.api.web.system.dict.DictDetailApi;
import org.opsli.api.wrapper.system.dict.DictDetailModel; import org.opsli.api.wrapper.system.dict.DictDetailModel;
import org.opsli.api.wrapper.system.dict.DictWrapper; import org.opsli.api.wrapper.system.dict.DictWrapper;
import org.opsli.common.constants.CacheConstants;
import org.opsli.common.constants.DictConstants; import org.opsli.common.constants.DictConstants;
import org.opsli.common.enums.CacheType; import org.opsli.common.enums.CacheType;
import org.opsli.core.cache.local.CacheUtil; import org.opsli.core.cache.local.CacheUtil;

@ -26,7 +26,6 @@ import org.opsli.plugins.redis.RedisLockPlugins;
import org.opsli.plugins.redis.RedisPlugin; import org.opsli.plugins.redis.RedisPlugin;
import org.opsli.plugins.redis.lock.RedisLock; import org.opsli.plugins.redis.lock.RedisLock;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Lazy; import org.springframework.context.annotation.Lazy;
import org.springframework.core.annotation.Order; import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;

@ -71,7 +71,7 @@ public class SearchHisUtil {
// 获得当前用户 // 获得当前用户
UserModel user = UserUtil.getUser(); UserModel user = UserUtil.getUser();
String cacheKey = CacheUtil.PREFIX_NAME + CACHE_PREFIX + user.getUsername() + ":" + key; String cacheKey = CacheUtil.getPrefixName() + CACHE_PREFIX + user.getUsername() + ":" + key;
return redisPlugin.zReverseRange(cacheKey, 0, count - 1); return redisPlugin.zReverseRange(cacheKey, 0, count - 1);
} }
@ -97,7 +97,7 @@ public class SearchHisUtil {
} }
String cacheKey = CacheUtil.PREFIX_NAME + CACHE_PREFIX + user.getUsername() + ":" + key; String cacheKey = CacheUtil.getPrefixName() + CACHE_PREFIX + user.getUsername() + ":" + key;
String val = values[0]; String val = values[0];
// 记录 // 记录

@ -26,7 +26,6 @@ import org.opsli.plugins.redis.RedisLockPlugins;
import org.opsli.plugins.redis.RedisPlugin; import org.opsli.plugins.redis.RedisPlugin;
import org.opsli.plugins.redis.lock.RedisLock; import org.opsli.plugins.redis.lock.RedisLock;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Lazy; import org.springframework.context.annotation.Lazy;
import org.springframework.core.annotation.Order; import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;

@ -26,12 +26,10 @@ import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.crypto.hash.Md5Hash; import org.apache.shiro.crypto.hash.Md5Hash;
import org.opsli.api.base.result.ResultVo; import org.opsli.api.base.result.ResultVo;
import org.opsli.api.wrapper.system.user.UserModel; import org.opsli.api.wrapper.system.user.UserModel;
import org.opsli.common.constants.CacheConstants;
import org.opsli.common.constants.SignConstants; import org.opsli.common.constants.SignConstants;
import org.opsli.common.constants.TokenConstants; import org.opsli.common.constants.TokenConstants;
import org.opsli.common.constants.TokenTypeConstants; import org.opsli.common.constants.TokenTypeConstants;
import org.opsli.common.exception.TokenException; import org.opsli.common.exception.TokenException;
import org.opsli.common.utils.Props;
import org.opsli.core.autoconfigure.properties.GlobalProperties; import org.opsli.core.autoconfigure.properties.GlobalProperties;
import org.opsli.core.cache.local.CacheUtil; import org.opsli.core.cache.local.CacheUtil;
import org.opsli.core.msg.TokenMsg; import org.opsli.core.msg.TokenMsg;
@ -80,9 +78,9 @@ public class UserTokenUtil {
static{ static{
// 缓存前缀 // 缓存前缀
TICKET_PREFIX = CacheUtil.PREFIX_NAME + "ticket:"; TICKET_PREFIX = CacheUtil.getPrefixName() + "ticket:";
ACCOUNT_SLIP_COUNT_PREFIX = CacheUtil.PREFIX_NAME + "account:slip:count:"; ACCOUNT_SLIP_COUNT_PREFIX = CacheUtil.getPrefixName() + "account:slip:count:";
ACCOUNT_SLIP_LOCK_PREFIX = CacheUtil.PREFIX_NAME + "account:slip:lock:"; ACCOUNT_SLIP_LOCK_PREFIX = CacheUtil.getPrefixName() + "account:slip:lock:";
} }

@ -8,7 +8,7 @@ spring:
#redis 配置 #redis 配置
redis: redis:
database: 0 database: 0
host: 10.0.0.254 host: 127.0.0.1
password: '123456' password: '123456'
port: 6379 port: 6379

Loading…
Cancel
Save