优化缓存系统

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.common.annotation.RequiresPermissionsCus;
import org.opsli.common.annotation.hotdata.EnableHotData;
import org.opsli.common.constants.CacheConstants;
import org.opsli.common.exception.ServiceException;
import org.opsli.common.exception.TokenException;
import org.opsli.common.msg.CommonMsg;
@ -119,7 +120,8 @@ public abstract class BaseRestController <T extends BaseEntity, E extends ApiWra
// 如果开启缓存 先从缓存读
if(hotDataFlag){
model = WrapperUtil.transformInstance(
CacheUtil.getTimed(entityClazz, id), modelClazz);
CacheUtil.getTimed(entityClazz, CacheConstants.HOT_DATA_PREFIX +":"+ id)
, modelClazz);
if(model != null){
return model;
}
@ -147,13 +149,14 @@ public abstract class BaseRestController <T extends BaseEntity, E extends ApiWra
// 如果获得锁 则 再次检查缓存里有没有, 如果有则直接退出, 没有的话才发起数据库请求
model = WrapperUtil.transformInstance(
CacheUtil.getTimed(entityClazz, id),modelClazz);
CacheUtil.getTimed(entityClazz, CacheConstants.HOT_DATA_PREFIX +":"+ id)
, modelClazz);
if(model != null){
return model;
}
// 如果缓存没读到 则去数据库读
model = WrapperUtil.transformInstance(IService.get(id),modelClazz);
model = WrapperUtil.transformInstance(IService.get(id), modelClazz);
}catch (Exception e){
log.error(e.getMessage(), e);
}finally {

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

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

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

@ -17,7 +17,9 @@ package org.opsli.core.cache.pushsub.handler;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
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.MsgArgsType;
import org.opsli.core.cache.pushsub.enums.PushSubType;
@ -45,10 +47,16 @@ public class HotDataHandler implements RedisPushSubHandler{
@Override
public void handler(JSONObject msgJson) {
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());
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){
ehCachePlugin.put(CacheConstants.EHCACHE_SPACE, cacheName, value);
}

@ -50,12 +50,11 @@ public final class CacheDataMsgFactory extends BaseSubMessage{
// 数据
JSONObject jsonObj = new JSONObject();
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_TYPE.toString(), cacheHandleType.toString());
// 热点数据 - 系统数据
baseSubMessage.build(CHANNEL,PushSubType.HOT_DATA.toString(),jsonObj);
baseSubMessage.build(CHANNEL, PushSubType.HOT_DATA.toString(), jsonObj);
return baseSubMessage;
}

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

@ -65,7 +65,7 @@ public class CaptchaUtil{
//生成文字验证码
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){
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)){
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.wrapper.system.dict.DictDetailModel;
import org.opsli.api.wrapper.system.dict.DictWrapper;
import org.opsli.common.constants.CacheConstants;
import org.opsli.common.constants.DictConstants;
import org.opsli.common.enums.CacheType;
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.lock.RedisLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@ -71,7 +71,7 @@ public class SearchHisUtil {
// 获得当前用户
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);
}
@ -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];
// 记录

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

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

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

Loading…
Cancel
Save