完善缓存架构

v1.4.1
Parker 5 years ago
parent cfe4f7d3d2
commit c622fa278d

@ -131,6 +131,14 @@ public abstract class BaseRestController <T extends BaseEntity, E extends ApiWra
if(redisLock == null){
throw new ServiceException(CoreMsg.CACHE_PUNCTURE_EXCEPTION);
}
// 如果获得锁 则 再次检查缓存里有没有, 如果有则直接退出, 没有的话才发起数据库请求
model = WrapperUtil.transformInstance(
CacheUtil.get(id, entityClazz),modelClazz);
if(model != null){
return model;
}
// 如果缓存没读到 则去数据库读
model = WrapperUtil.transformInstance(IService.get(id),modelClazz);
}catch (Exception e){

@ -106,6 +106,14 @@ public class DictUtil {
return defaultVal;
}
// 如果获得锁 则 再次检查缓存里有没有, 如果有则直接退出, 没有的话才发起数据库请求
cacheModel = CacheUtil.getHash(DictConstants.CACHE_PREFIX_VALUE + typeCode,
dictValue, DictDetailModel.class);
if (cacheModel != null){
dictName = cacheModel.getDictName();
}
if (StringUtils.isNotEmpty(dictName)) return dictName;
// 查询数据库 并保存到缓存内
ResultVo<List<DictDetailModel>> resultVo = dictDetailApi.findListByTypeCode(typeCode);
if(resultVo.isSuccess()){
@ -182,6 +190,14 @@ public class DictUtil {
return defaultVal;
}
// 如果获得锁 则 再次检查缓存里有没有, 如果有则直接退出, 没有的话才发起数据库请求
cacheModel = CacheUtil.getHash(DictConstants.CACHE_PREFIX_NAME + typeCode,
dictName, DictDetailModel.class);
if (cacheModel != null){
dictValue = cacheModel.getDictValue();
}
if (StringUtils.isNotEmpty(dictValue)) return dictValue;
// 查询数据库 并保存到缓存内
ResultVo<List<DictDetailModel>> resultVo = dictDetailApi.findListByTypeCode(typeCode);
if(resultVo.isSuccess()){
@ -250,62 +266,89 @@ public class DictUtil {
dictWrapperModel.setDictValue(model.getDictValue());
dictWrapperModels.add(dictWrapperModel);
}
if(!dictWrapperModels.isEmpty()){
return dictWrapperModels;
}
if(dictWrapperModels.isEmpty()){
// 防止缓存穿透判断
boolean hasNilFlag = CacheUtil.hasNilFlag(DictConstants.CACHE_PREFIX_LIST + typeCode);
if(hasNilFlag){
// 防止缓存穿透判断
boolean hasNilFlag = CacheUtil.hasNilFlag(DictConstants.CACHE_PREFIX_LIST + typeCode);
if(hasNilFlag){
return dictWrapperModels;
}
// 锁凭证 redisLock 贯穿全程
RedisLock redisLock = new RedisLock();
redisLock.setLockName(DictConstants.CACHE_PREFIX_LIST + typeCode)
.setAcquireTimeOut(3000L)
.setLockTimeOut(10000L);
try {
// 这里增加分布式锁 防止缓存击穿
// ============ 尝试加锁
redisLock = redisLockPlugins.tryLock(redisLock);
if(redisLock == null){
return dictWrapperModels;
}
// 锁凭证 redisLock 贯穿全程
RedisLock redisLock = new RedisLock();
redisLock.setLockName(DictConstants.CACHE_PREFIX_LIST + typeCode)
.setAcquireTimeOut(3000L)
.setLockTimeOut(10000L);
try {
// 这里增加分布式锁 防止缓存击穿
// ============ 尝试加锁
redisLock = redisLockPlugins.tryLock(redisLock);
if(redisLock == null){
return dictWrapperModels;
// 如果获得锁 则 再次检查缓存里有没有, 如果有则直接退出, 没有的话才发起数据库请求
key = CacheUtil.handleKey(CacheConstants.EDEN_HASH_DATA, DictConstants.CACHE_PREFIX_VALUE + typeCode);
dictMap = redisPlugin.hGetAll(key);
entries = dictMap.entrySet();
for (Map.Entry<Object, Object> entry : entries) {
// 赋值
JSONObject jsonObject = (JSONObject) entry.getValue();
if(jsonObject == null){
continue;
}
// 查询数据库 并保存到缓存内
ResultVo<List<DictDetailModel>> resultVo = dictDetailApi.findListByTypeCode(typeCode);
if(resultVo.isSuccess()){
List<DictDetailModel> dictDetailModels = resultVo.getData();
for (DictDetailModel model : dictDetailModels) {
DictWrapper dictWrapperModel = new DictWrapper();
dictWrapperModel.setTypeCode(model.getTypeCode());
dictWrapperModel.setDictName(model.getDictName());
dictWrapperModel.setDictValue(model.getDictValue());
dictWrapperModel.setModel(model);
dictWrapperModels.add(dictWrapperModel);
// 保存至缓存
DictUtil.put(dictWrapperModel);
}
JSONObject dataJson = jsonObject.getJSONObject(CacheUtil.JSON_KEY);
if(dataJson == null){
continue;
}
}catch (Exception e){
log.error(e.getMessage(),e);
DictDetailModel model = dataJson.toJavaObject(DictDetailModel.class);
DictWrapper dictWrapperModel = new DictWrapper();
dictWrapperModel.setTypeCode(typeCode);
dictWrapperModel.setDictName(model.getDictName());
dictWrapperModel.setDictValue(model.getDictValue());
dictWrapperModels.add(dictWrapperModel);
}
if(!dictWrapperModels.isEmpty()){
return dictWrapperModels;
}finally {
// ============ 释放锁
redisLockPlugins.unLock(redisLock);
redisLock = null;
}
// 如果值还是 为空 则赋默认值
if(dictWrapperModels.isEmpty()){
// 加入缓存防穿透
// 设置空变量 用于防止穿透判断
CacheUtil.putNilFlag(DictConstants.CACHE_PREFIX_LIST + typeCode );
// 查询数据库 并保存到缓存内
ResultVo<List<DictDetailModel>> resultVo = dictDetailApi.findListByTypeCode(typeCode);
if(resultVo.isSuccess()){
List<DictDetailModel> dictDetailModels = resultVo.getData();
for (DictDetailModel model : dictDetailModels) {
DictWrapper dictWrapperModel = new DictWrapper();
dictWrapperModel.setTypeCode(model.getTypeCode());
dictWrapperModel.setDictName(model.getDictName());
dictWrapperModel.setDictValue(model.getDictValue());
dictWrapperModel.setModel(model);
dictWrapperModels.add(dictWrapperModel);
// 保存至缓存
DictUtil.put(dictWrapperModel);
}
}
}catch (Exception e){
log.error(e.getMessage(),e);
return dictWrapperModels;
}finally {
// ============ 释放锁
redisLockPlugins.unLock(redisLock);
redisLock = null;
}
// 如果值还是 为空 则赋默认值
if(dictWrapperModels.isEmpty()){
// 加入缓存防穿透
// 设置空变量 用于防止穿透判断
CacheUtil.putNilFlag(DictConstants.CACHE_PREFIX_LIST + typeCode );
}
}catch (Exception e){
log.error(e.getMessage(),e);
dictWrapperModels = Lists.newArrayList();

@ -95,6 +95,12 @@ public class MenuUtil {
return null;
}
// 如果获得锁 则 再次检查缓存里有没有, 如果有则直接退出, 没有的话才发起数据库请求
menuModel = CacheUtil.get(PREFIX_CODE + menuCode, MenuModel.class);
if (menuModel != null){
return menuModel;
}
// 查询数据库
ResultVo<MenuModel> resultVo = menuApi.getByCode(menuCode);
if(resultVo.isSuccess()){

@ -139,6 +139,12 @@ public class UserUtil {
return null;
}
// 如果获得锁 则 再次检查缓存里有没有, 如果有则直接退出, 没有的话才发起数据库请求
userModel = CacheUtil.get(PREFIX_ID + userId, UserModel.class);
if (userModel != null){
return userModel;
}
// 查询数据库
UserModel userModelTemp = new UserModel();
userModelTemp.setId(userId);
@ -201,6 +207,12 @@ public class UserUtil {
return null;
}
// 如果获得锁 则 再次检查缓存里有没有, 如果有则直接退出, 没有的话才发起数据库请求
userModel = CacheUtil.get(PREFIX_USERNAME + userName, UserModel.class);
if (userModel != null){
return userModel;
}
// 查询数据库
ResultVo<UserModel> resultVo = userApi.getUserByUsername(userName);
if(resultVo.isSuccess()){
@ -272,6 +284,22 @@ public class UserUtil {
return null;
}
// 如果获得锁 则 再次检查缓存里有没有, 如果有则直接退出, 没有的话才发起数据库请求
try {
Object obj = CacheUtil.get(PREFIX_ID_ROLES + userId);
if(obj instanceof List){
List<String> list = (List<String>) obj;
if (!list.isEmpty()) {
return list;
}
}else {
JSONArray jsonArray = (JSONArray) obj;
if (jsonArray != null && !jsonArray.isEmpty()) {
return jsonArray.toJavaList(String.class);
}
}
}catch (Exception ignored){}
// 查询数据库
ResultVo<List<String>> resultVo = userApi.getRolesByUserId(userId);
if(resultVo.isSuccess()){
@ -345,6 +373,22 @@ public class UserUtil {
return null;
}
// 如果获得锁 则 再次检查缓存里有没有, 如果有则直接退出, 没有的话才发起数据库请求
try {
Object obj = CacheUtil.get(PREFIX_ID_PERMISSIONS + userId);
if(obj instanceof List){
List<String> list = (List<String>) obj;
if (!list.isEmpty()) {
return list;
}
}else {
JSONArray jsonArray = (JSONArray) obj;
if (jsonArray != null && !jsonArray.isEmpty()) {
return jsonArray.toJavaList(String.class);
}
}
}catch (Exception ignored){}
// 查询数据库
ResultVo<List<String>> resultVo = userApi.getAllPerms(userId);
if(resultVo.isSuccess()){
@ -427,6 +471,32 @@ public class UserUtil {
return null;
}
// 如果获得锁 则 再次检查缓存里有没有, 如果有则直接退出, 没有的话才发起数据库请求
try {
Object obj = CacheUtil.get(PREFIX_ID_MENUS + userId);
if(obj instanceof List){
List<Object> list = (List<Object>) obj;
if (!list.isEmpty()) {
List<MenuModel> menuModels = Lists.newArrayListWithCapacity(list.size());
for (Object menuObj : list) {
if(menuObj instanceof MenuModel){
menuModels.add((MenuModel) menuObj);
}else if(menuObj instanceof JSONObject){
JSONObject jsonObject = (JSONObject) menuObj;
MenuModel t = JSONObject.toJavaObject(jsonObject, MenuModel.class);
menuModels.add(t);
}
}
return menuModels;
}
}else {
JSONArray jsonArray = (JSONArray) obj;
if (jsonArray != null && !jsonArray.isEmpty()) {
return jsonArray.toJavaList(MenuModel.class);
}
}
}catch (Exception ignored){}
// 查询数据库
ResultVo<List<MenuModel>> resultVo = userApi.getMenuListByUserId(userId);
if(resultVo.isSuccess()){

Loading…
Cancel
Save