缓存系统刷新优化

v1.4.1
Parker 4 years ago
parent 708e10d0a3
commit 1a397e94ca

@ -55,6 +55,7 @@ public enum CoreMsg implements BaseMsg {
*
*/
CACHE_PUNCTURE_EXCEPTION(10405, "当期服务繁忙,客官请稍微再次尝试!"),
CACHE_DEL_EXCEPTION(10406, "删除失败,无法清除缓存,请稍后再试"),
/**

@ -15,6 +15,7 @@
*/
package org.opsli.core.utils;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
@ -396,13 +397,13 @@ public class DictUtil {
* @return
*/
public static void put(DictWrapper model){
// 清除缓存
DictUtil.del(model);
CacheUtil.putEdenHash(DictConstants.CACHE_PREFIX_NAME + model.getTypeCode(),
model.getDictName(), model.getModel());
CacheUtil.putEdenHash(DictConstants.CACHE_PREFIX_VALUE + model.getTypeCode(),
model.getDictValue(), model.getModel());
// 删除 空属性 拦截
CacheUtil.delNilFlag(DictConstants.CACHE_PREFIX_NAME + model.getTypeCode() + ":" + model.getDictName());
CacheUtil.delNilFlag(DictConstants.CACHE_PREFIX_VALUE + model.getTypeCode() + ":" + model.getTypeCode());
}
/**
@ -410,12 +411,64 @@ public class DictUtil {
* @param model
* @return
*/
public static void del(DictWrapper model){
CacheUtil.delEdenHash(DictConstants.CACHE_PREFIX_NAME + model.getTypeCode(), model.getDictName());
CacheUtil.delEdenHash(DictConstants.CACHE_PREFIX_VALUE + model.getTypeCode(), model.getDictValue());
// 删除 空属性 拦截
CacheUtil.delNilFlag(DictConstants.CACHE_PREFIX_NAME + model.getTypeCode() + ":" + model.getDictName());
CacheUtil.delNilFlag(DictConstants.CACHE_PREFIX_VALUE + model.getTypeCode() + ":" + model.getTypeCode());
public static boolean del(DictWrapper model){
if(model == null){
return true;
}
boolean hasNilFlagByName = CacheUtil.hasNilFlag(DictConstants.CACHE_PREFIX_NAME +
model.getTypeCode() + ":" + model.getDictName());
boolean hasNilFlagByValue = CacheUtil.hasNilFlag(DictConstants.CACHE_PREFIX_VALUE +
model.getTypeCode() + ":" + model.getDictValue());
DictWrapper dictByName = CacheUtil.get(DictConstants.CACHE_PREFIX_NAME + model.getTypeCode(),
DictWrapper.class);
DictWrapper dictByValue = CacheUtil.get(DictConstants.CACHE_PREFIX_VALUE + model.getTypeCode(),
DictWrapper.class);
// 计数器
int count = 0;
if (hasNilFlagByName){
count++;
// 清除空拦截
boolean tmp = CacheUtil.delNilFlag(DictConstants.CACHE_PREFIX_NAME +
model.getTypeCode() + ":" + model.getDictName());
if(tmp){
count--;
}
}
if (hasNilFlagByValue){
count++;
// 清除空拦截
boolean tmp = CacheUtil.delNilFlag(DictConstants.CACHE_PREFIX_VALUE +
model.getTypeCode() + ":" + model.getDictValue());
if(tmp){
count--;
}
}
if (dictByName != null){
count++;
// 清除空拦截
boolean tmp = CacheUtil.delEdenHash(DictConstants.CACHE_PREFIX_NAME +
model.getTypeCode(), model.getDictName());
if(tmp){
count--;
}
}
if (dictByValue != null){
count++;
// 清除空拦截
boolean tmp = CacheUtil.delEdenHash(DictConstants.CACHE_PREFIX_VALUE +
model.getTypeCode(), model.getDictValue());
if(tmp){
count--;
}
}
return count == 0;
}
/**
@ -423,11 +476,21 @@ public class DictUtil {
* @param typeCode
* @return
*/
public static void delAll(String typeCode){
public static boolean delAll(String typeCode){
List<DictWrapper> dictWrapperList = DictUtil.getDictList(typeCode);
if(CollUtil.isEmpty(dictWrapperList)){
return true;
}
// 计数器
int count = dictWrapperList.size();
for (DictWrapper dictWrapperModel : dictWrapperList) {
DictUtil.del(dictWrapperModel);
boolean tmp = DictUtil.del(dictWrapperModel);
if(tmp){
count--;
}
}
return count == 0;
}

@ -133,29 +133,42 @@ public class MenuUtil {
* @param menu
* @return
*/
public static void refreshMenu(MenuModel menu){
public static boolean refreshMenu(MenuModel menu){
if(menu == null || StringUtils.isEmpty(menu.getMenuCode())){
return;
return true;
}
// 计数器
int count = 0;
MenuModel menuModel = CacheUtil.get(PREFIX_CODE + menu.getMenuCode(), MenuModel.class);
boolean hasNilFlag = CacheUtil.hasNilFlag(PREFIX_CODE + menu.getMenuCode());
// 只要不为空 则执行刷新
if (hasNilFlag){
count++;
// 清除空拦截
CacheUtil.delNilFlag(PREFIX_CODE + menu.getMenuCode());
boolean tmp = CacheUtil.delNilFlag(PREFIX_CODE + menu.getMenuCode());
if(tmp){
count--;
}
}
if(menuModel != null){
count++;
// 先删除
CacheUtil.del(PREFIX_CODE + menu.getMenuCode());
boolean tmp = CacheUtil.del(PREFIX_CODE + menu.getMenuCode());
if(tmp){
count--;
}
// 发送通知消息
redisPlugin.sendMessage(
MenuMsgFactory.createMenuMsg(menu)
);
}
return count == 0;
}

@ -134,29 +134,41 @@ public class OrgUtil {
* @param userId
* @return
*/
public static void refreshMenu(String userId){
public static boolean refreshOrg(String userId){
if(StringUtils.isEmpty(userId)){
return;
return true;
}
// 计数器
int count = 0;
UserOrgRefModel orgRefModel = CacheUtil.get(PREFIX_CODE + userId, UserOrgRefModel.class);
boolean hasNilFlag = CacheUtil.hasNilFlag(PREFIX_CODE + userId);
// 只要不为空 则执行刷新
if (hasNilFlag){
count++;
// 清除空拦截
CacheUtil.delNilFlag(PREFIX_CODE + userId);
boolean tmp = CacheUtil.delNilFlag(PREFIX_CODE + userId);
if(tmp){
count--;
}
}
if(orgRefModel != null){
count++;
// 先删除
CacheUtil.del(PREFIX_CODE + userId);
boolean tmp = CacheUtil.del(PREFIX_CODE + userId);
if(tmp){
count--;
}
// 发送通知消息
redisPlugin.sendMessage(
OrgMsgFactory.createOrgMsg(orgRefModel)
);
}
return count == 0;
}

@ -138,29 +138,42 @@ public class TenantUtil {
* @param tenantId
* @return
*/
public static void refreshTenant(String tenantId){
public static boolean refreshTenant(String tenantId){
if(StringUtils.isEmpty(tenantId)){
return;
return true;
}
// 计数器
int count = 0;
TenantModel tenantModel = CacheUtil.get(PREFIX_CODE + tenantId, TenantModel.class);
boolean hasNilFlag = CacheUtil.hasNilFlag(PREFIX_CODE + tenantId);
// 只要不为空 则执行刷新
if (hasNilFlag){
count++;
// 清除空拦截
CacheUtil.delNilFlag(PREFIX_CODE + tenantId);
boolean tmp = CacheUtil.delNilFlag(PREFIX_CODE + tenantId);
if(tmp){
count--;
}
}
if(tenantModel != null){
count++;
// 先删除
CacheUtil.del(PREFIX_CODE + tenantId);
boolean tmp = CacheUtil.del(PREFIX_CODE + tenantId);
if(tmp){
count--;
}
// 发送通知消息
redisPlugin.sendMessage(
TenantMsgFactory.createTenantMsg(tenantModel)
);
}
return count == 0;
}

@ -524,9 +524,9 @@ public class UserUtil {
* @param user
* @return
*/
public static void refreshUser(UserModel user){
public static boolean refreshUser(UserModel user){
if(user == null || StringUtils.isEmpty(user.getId())){
return;
return true;
}
UserModel userModelById = CacheUtil.get(PREFIX_ID + user.getId(), UserModel.class);
@ -536,27 +536,52 @@ public class UserUtil {
boolean hasNilFlagById = CacheUtil.hasNilFlag(PREFIX_ID + user.getId());
boolean hasNilFlagByName = CacheUtil.hasNilFlag(PREFIX_USERNAME + user.getUsername());
// 只要有一个不为空 则执行刷新
if (hasNilFlagById || hasNilFlagByName){
// 计数器
int count = 0;
if (hasNilFlagById){
count++;
// 清除空拦截
CacheUtil.delNilFlag(PREFIX_ID + user.getId());
CacheUtil.delNilFlag(PREFIX_USERNAME + user.getUsername());
boolean tmp = CacheUtil.delNilFlag(PREFIX_ID + user.getId());
if(tmp){
count--;
}
}
if (hasNilFlagByName){
count++;
// 清除空拦截
boolean tmp = CacheUtil.delNilFlag(PREFIX_USERNAME + user.getUsername());
if(tmp){
count--;
}
}
// 只要有一个不为空 则执行刷新
if (userModelById != null || userModelByUsername != null){
if (userModelById != null){
count++;
// 先删除
CacheUtil.del(PREFIX_ID + user.getId());
CacheUtil.del(PREFIX_USERNAME + user.getUsername());
// 清除空拦截
CacheUtil.delNilFlag(PREFIX_ID + user.getId());
CacheUtil.delNilFlag(PREFIX_USERNAME + user.getUsername());
boolean tmp = CacheUtil.del(PREFIX_ID + user.getId());
if(tmp){
count--;
}
}
// 发送通知消息
redisPlugin.sendMessage(
UserMsgFactory.createUserMsg(user)
);
if (userModelByUsername != null){
count++;
// 先删除
boolean tmp = CacheUtil.del(PREFIX_USERNAME + user.getUsername());
if(tmp){
count--;
}
}
// 发送通知消息
redisPlugin.sendMessage(
UserMsgFactory.createUserMsg(user)
);
return count == 0;
}
@ -565,30 +590,38 @@ public class UserUtil {
* @param userId
* @return
*/
public static void refreshUserRoles(String userId){
try {
Object obj = CacheUtil.get(PREFIX_ID_ROLES + userId);
boolean hasNilFlag = CacheUtil.hasNilFlag(PREFIX_ID_ROLES + userId);
// 只要不为空 则执行刷新
if (hasNilFlag){
// 清除空拦截
CacheUtil.delNilFlag(PREFIX_ID_ROLES + userId);
}
public static boolean refreshUserRoles(String userId){
Object obj = CacheUtil.get(PREFIX_ID_ROLES + userId);
boolean hasNilFlag = CacheUtil.hasNilFlag(PREFIX_ID_ROLES + userId);
if(obj != null){
// 先删除
CacheUtil.del(PREFIX_ID_ROLES + userId);
// 计数器
int count = 0;
// 发送通知消息
redisPlugin.sendMessage(
UserMsgFactory.createUserRolesMsg(userId, null)
);
// 只要不为空 则执行刷新
if (hasNilFlag){
count++;
// 清除空拦截
boolean tmp = CacheUtil.delNilFlag(PREFIX_ID_ROLES + userId);
if(tmp){
count--;
}
}
}catch (Exception e){
log.error(e.getMessage(), e);
if(obj != null){
count++;
// 先删除
boolean tmp = CacheUtil.del(PREFIX_ID_ROLES + userId);
if(tmp){
count--;
}
}
// 发送通知消息
redisPlugin.sendMessage(
UserMsgFactory.createUserRolesMsg(userId, null)
);
return count == 0;
}
/**
@ -596,29 +629,38 @@ public class UserUtil {
* @param userId
* @return
*/
public static void refreshUserAllPerms(String userId){
try {
Object obj = CacheUtil.get(PREFIX_ID_PERMISSIONS + userId);
boolean hasNilFlag = CacheUtil.hasNilFlag(PREFIX_ID_PERMISSIONS + userId);
public static boolean refreshUserAllPerms(String userId){
Object obj = CacheUtil.get(PREFIX_ID_PERMISSIONS + userId);
boolean hasNilFlag = CacheUtil.hasNilFlag(PREFIX_ID_PERMISSIONS + userId);
// 只要不为空 则执行刷新
if (hasNilFlag){
// 清除空拦截
CacheUtil.delNilFlag(PREFIX_ID_PERMISSIONS + userId);
}
// 计数器
int count = 0;
if(obj != null){
// 先删除
CacheUtil.del(PREFIX_ID_PERMISSIONS + userId);
// 只要不为空 则执行刷新
if (hasNilFlag){
count++;
// 清除空拦截
boolean tmp = CacheUtil.delNilFlag(PREFIX_ID_PERMISSIONS + userId);
if(tmp){
count--;
}
}
// 发送通知消息
redisPlugin.sendMessage(
UserMsgFactory.createUserPermsMsg(userId, null)
);
if(obj != null){
count++;
// 先删除
boolean tmp = CacheUtil.del(PREFIX_ID_PERMISSIONS + userId);
if(tmp){
count--;
}
}catch (Exception e){
log.error(e.getMessage(), e);
}
// 发送通知消息
redisPlugin.sendMessage(
UserMsgFactory.createUserPermsMsg(userId, null)
);
return count == 0;
}
/**
@ -626,30 +668,38 @@ public class UserUtil {
* @param userId
* @return
*/
public static void refreshUserMenus(String userId){
try {
Object obj = CacheUtil.get(PREFIX_ID_MENUS + userId);
boolean hasNilFlag = CacheUtil.hasNilFlag(PREFIX_ID_MENUS + userId);
// 只要不为空 则执行刷新
if (hasNilFlag){
// 清除空拦截
CacheUtil.delNilFlag(PREFIX_ID_MENUS + userId);
}
public static boolean refreshUserMenus(String userId){
Object obj = CacheUtil.get(PREFIX_ID_MENUS + userId);
boolean hasNilFlag = CacheUtil.hasNilFlag(PREFIX_ID_MENUS + userId);
if(obj != null){
// 先删除
CacheUtil.del(PREFIX_ID_MENUS + userId);
// 计数器
int count = 0;
// 发送通知消息
redisPlugin.sendMessage(
UserMsgFactory.createUserMenusMsg(userId, null)
);
// 只要不为空 则执行刷新
if (hasNilFlag){
count++;
// 清除空拦截
boolean tmp = CacheUtil.delNilFlag(PREFIX_ID_MENUS + userId);
if(tmp){
count--;
}
}
}catch (Exception e){
log.error(e.getMessage(), e);
if(obj != null){
count++;
// 先删除
boolean tmp = CacheUtil.del(PREFIX_ID_MENUS + userId);
if(tmp){
count--;
}
}
// 发送通知消息
redisPlugin.sendMessage(
UserMsgFactory.createUserMenusMsg(userId, null)
);
return count == 0;
}
/**

@ -15,6 +15,7 @@
*/
package org.opsli.modulars.system.dict.service.impl;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
@ -28,6 +29,7 @@ import org.opsli.common.utils.HumpUtil;
import org.opsli.core.base.service.impl.CrudServiceImpl;
import org.opsli.core.cache.pushsub.enums.CacheType;
import org.opsli.core.cache.pushsub.msgs.DictMsgFactory;
import org.opsli.core.msg.CoreMsg;
import org.opsli.core.persistence.querybuilder.GenQueryBuilder;
import org.opsli.core.persistence.querybuilder.QueryBuilder;
import org.opsli.core.utils.DictUtil;
@ -41,10 +43,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.*;
/**
@ -94,7 +93,10 @@ public class DictDetailServiceImpl extends CrudServiceImpl<DictDetailMapper, Sys
dictWrapperList.add(dictWrapperModel);
}
// 删除缓存
DictUtil.delAll(ret.getTypeCode());
this.clearCache(Collections.singletonList(
ret.getTypeCode()
));
// 广播缓存数据 - 通知其他服务器同步数据
redisPlugin.sendMessage(
DictMsgFactory.createMsg(dictWrapperList, CacheType.DELETE)
@ -138,7 +140,10 @@ public class DictDetailServiceImpl extends CrudServiceImpl<DictDetailMapper, Sys
dictWrapperList.add(dictWrapperModel);
}
// 删除缓存
DictUtil.delAll(oldModel.getTypeCode());
this.clearCache(Collections.singletonList(
oldModel.getTypeCode()
));
// 广播缓存数据 - 通知其他服务器同步数据
redisPlugin.sendMessage(
DictMsgFactory.createMsg(dictWrapperList, CacheType.DELETE)
@ -172,7 +177,9 @@ public class DictDetailServiceImpl extends CrudServiceImpl<DictDetailMapper, Sys
dictWrapperList.add(dictWrapperModel);
}
// 删除缓存
DictUtil.delAll(dictModel.getTypeCode());
this.clearCache(Collections.singletonList(
dictModel.getTypeCode()
));
// 广播缓存数据 - 通知其他服务器同步数据
redisPlugin.sendMessage(
DictMsgFactory.createMsg(dictWrapperList, CacheType.DELETE)
@ -193,6 +200,11 @@ public class DictDetailServiceImpl extends CrudServiceImpl<DictDetailMapper, Sys
DictDetailModel dictModel = this.get(model);
boolean ret = super.delete(model);
if(ret){
// 删除缓存
this.clearCache(Collections.singletonList(
dictModel.getTypeCode()
));
List<DictDetailModel> listByTypeCode = this.findListByTypeCode(dictModel.getTypeCode());
if(listByTypeCode != null && listByTypeCode.size() > 0){
List<DictWrapper> dictWrapperList = Lists.newArrayListWithCapacity(listByTypeCode.size());
@ -204,7 +216,9 @@ public class DictDetailServiceImpl extends CrudServiceImpl<DictDetailMapper, Sys
dictWrapperList.add(dictWrapperModel);
}
// 删除缓存
DictUtil.delAll(dictModel.getTypeCode());
this.clearCache(Collections.singletonList(
dictModel.getTypeCode()
));
// 广播缓存数据 - 通知其他服务器同步数据
redisPlugin.sendMessage(
DictMsgFactory.createMsg(dictWrapperList, CacheType.DELETE)
@ -245,10 +259,11 @@ public class DictDetailServiceImpl extends CrudServiceImpl<DictDetailMapper, Sys
typeCodes.add(dictWrapperModel.getTypeCode());
}
List<String> typeCodeList = Lists.newArrayListWithCapacity(typeCodes.size());
typeCodeList.addAll(typeCodes);
// 删除缓存
for (String typeCode : typeCodes) {
DictUtil.delAll(typeCode);
}
this.clearCache(typeCodeList);
// 广播缓存数据 - 通知其他服务器同步数据
redisPlugin.sendMessage(
@ -298,10 +313,11 @@ public class DictDetailServiceImpl extends CrudServiceImpl<DictDetailMapper, Sys
typeCodes.add(dictWrapperModel.getTypeCode());
}
List<String> typeCodeList = Lists.newArrayListWithCapacity(typeCodes.size());
typeCodeList.addAll(typeCodes);
// 删除缓存
for (String typeCode : typeCodes) {
DictUtil.delAll(typeCode);
}
this.clearCache(typeCodeList);
// 广播缓存数据 - 通知其他服务器同步数据
redisPlugin.sendMessage(
@ -343,7 +359,9 @@ public class DictDetailServiceImpl extends CrudServiceImpl<DictDetailMapper, Sys
dictWrapperList.add(dictWrapperModel);
}
// 删除缓存
DictUtil.delAll(dictModel.getTypeCode());
this.clearCache(Collections.singletonList(
dictModel.getTypeCode()
));
// 广播缓存数据 - 通知其他服务器同步数据
redisPlugin.sendMessage(
DictMsgFactory.createMsg(dictWrapperList, CacheType.DELETE)
@ -377,6 +395,32 @@ public class DictDetailServiceImpl extends CrudServiceImpl<DictDetailMapper, Sys
// 转化对象
return super.transformTs2Ms(list);
}
// ================
/**
*
* @param typeCodeList
*/
private void clearCache(List<String> typeCodeList) {
// 删除缓存
if (CollUtil.isNotEmpty(typeCodeList)) {
int cacheCount = 0;
for (String typeCode : typeCodeList) {
cacheCount++;
boolean tmp = DictUtil.delAll(typeCode);
if(tmp){
cacheCount--;
}
}
// 判断删除状态
if(cacheCount != 0){
// 删除缓存失败
throw new ServiceException(CoreMsg.CACHE_DEL_EXCEPTION);
}
}
}
}

@ -15,6 +15,7 @@
*/
package org.opsli.modulars.system.menu.service.impl;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
@ -22,8 +23,8 @@ import org.opsli.api.wrapper.system.menu.MenuModel;
import org.opsli.common.constants.MyBatisConstants;
import org.opsli.common.exception.ServiceException;
import org.opsli.common.utils.HumpUtil;
import org.opsli.common.utils.WrapperUtil;
import org.opsli.core.base.service.impl.CrudServiceImpl;
import org.opsli.core.msg.CoreMsg;
import org.opsli.core.persistence.querybuilder.GenQueryBuilder;
import org.opsli.core.persistence.querybuilder.QueryBuilder;
import org.opsli.core.utils.MenuUtil;
@ -107,18 +108,8 @@ public class MenuServiceImpl extends CrudServiceImpl<MenuMapper, SysMenu, MenuMo
MenuModel menuModel = super.update(model);
if(menuModel != null){
// 清空编号缓存
MenuUtil.refreshMenu(menuModel);
// 清空该菜单下 用户缓存
List<String> userIdList = iUserRoleRefService.getUserIdListByMenuId(menuModel.getId());
if(userIdList != null && !userIdList.isEmpty()){
for (String userId : userIdList) {
// 清空当期用户缓存角色、权限、菜单
UserUtil.refreshUserRoles(userId);
UserUtil.refreshUserAllPerms(userId);
UserUtil.refreshUserMenus(userId);
}
}
// 清除缓存
this.clearCache(model);
}
return menuModel;
@ -134,18 +125,8 @@ public class MenuServiceImpl extends CrudServiceImpl<MenuMapper, SysMenu, MenuMo
if(ret){
// 清空编号缓存
MenuUtil.refreshMenu(menuModel);
// 清空该菜单下 用户缓存
List<String> userIdList = iUserRoleRefService.getUserIdListByMenuId(id);
if(userIdList != null && !userIdList.isEmpty()){
for (String userId : userIdList) {
// 清空当期用户缓存角色、权限、菜单
UserUtil.refreshUserRoles(userId);
UserUtil.refreshUserAllPerms(userId);
UserUtil.refreshUserMenus(userId);
}
}
// 清除缓存
this.clearCache(menuModel);
}
return ret;
}
@ -156,7 +137,15 @@ public class MenuServiceImpl extends CrudServiceImpl<MenuMapper, SysMenu, MenuMo
QueryBuilder<SysMenu> queryBuilder = new GenQueryBuilder<>();
QueryWrapper<SysMenu> queryWrapper = queryBuilder.build();
queryWrapper.in(MyBatisConstants.FIELD_ID, Convert.toList(String.class, ids));
List<SysMenu> menuList = super.findList(queryWrapper);
List<MenuModel> menuList = super.transformTs2Ms(
super.findList(queryWrapper)
);
// 清除缓存
for (MenuModel menuModel : menuList) {
this.clearCache(menuModel);
}
boolean ret = super.deleteAll(ids);
// 删除子数据
@ -164,27 +153,6 @@ public class MenuServiceImpl extends CrudServiceImpl<MenuMapper, SysMenu, MenuMo
this.deleteByParentId(id);
}
if(ret){
// 清空编号缓存
for (SysMenu sysMenu : menuList) {
MenuUtil.refreshMenu(
WrapperUtil.transformInstance(sysMenu, MenuModel.class)
);
}
for (String id : ids) {
// 清空该菜单下 用户缓存
List<String> userIdList = iUserRoleRefService.getUserIdListByMenuId(id);
if(userIdList != null && !userIdList.isEmpty()){
for (String userId : userIdList) {
// 清空当期用户缓存角色、权限、菜单
UserUtil.refreshUserRoles(userId);
UserUtil.refreshUserAllPerms(userId);
UserUtil.refreshUserMenus(userId);
}
}
}
}
return ret;
}
@ -208,6 +176,49 @@ public class MenuServiceImpl extends CrudServiceImpl<MenuMapper, SysMenu, MenuMo
}
return ret;
}
// ============
/**
*
* @param menuModel
*/
private void clearCache(MenuModel menuModel){
boolean cacheRet;
// 计数器
int cacheCount = 1;
// 先清除缓存
// 清空编号缓存
cacheRet = MenuUtil.refreshMenu(menuModel);
if(cacheRet){
cacheCount--;
}
// 清空该菜单下 用户缓存
List<String> userIdList = iUserRoleRefService.getUserIdListByMenuId(menuModel.getId());
if(CollUtil.isNotEmpty(userIdList)){
for (String userId : userIdList) {
cacheCount += 3;
// 清空当期用户缓存角色、权限、菜单
cacheRet = UserUtil.refreshUserRoles(userId);
if(cacheRet){
cacheCount--;
}
cacheRet = UserUtil.refreshUserAllPerms(userId);
if(cacheRet){
cacheCount--;
}
cacheRet = UserUtil.refreshUserMenus(userId);
if(cacheRet){
cacheCount--;
}
}
}
// 判断删除状态
if(cacheCount != 0){
// 删除缓存失败
throw new ServiceException(CoreMsg.CACHE_DEL_EXCEPTION);
}
}
}

@ -15,11 +15,13 @@
*/
package org.opsli.modulars.system.role.service.impl;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.opsli.common.exception.ServiceException;
import org.opsli.core.msg.CoreMsg;
import org.opsli.core.utils.UserUtil;
import org.opsli.modulars.system.SystemMsg;
import org.opsli.modulars.system.menu.entity.SysMenu;
@ -75,21 +77,51 @@ public class RoleMenuRefServiceImpl extends ServiceImpl<RoleMenuRefMapper,SysRol
entity.setMenuId(permsId);
list.add(entity);
}
super.saveBatch(list);
boolean ret = super.saveBatch(list);
if(ret){
// 清除缓存
this.clearCache(roleId);
}
return ret;
}
return true;
}
// =========================
/**
*
* @param roleId
*/
private void clearCache(String roleId){
// 清空该角色下 用户缓存
List<String> userIdList = iUserRoleRefService.getUserIdListByRoleId(roleId);
if(userIdList != null && !userIdList.isEmpty()){
if(CollUtil.isNotEmpty(userIdList)){
int cacheCount = 0;
for (String userId : userIdList) {
cacheCount += 3;
boolean tmp;
// 清空当期用户缓存角色、权限、菜单
UserUtil.refreshUserRoles(userId);
UserUtil.refreshUserAllPerms(userId);
UserUtil.refreshUserMenus(userId);
tmp = UserUtil.refreshUserRoles(userId);
if(tmp){
cacheCount--;
}
tmp = UserUtil.refreshUserAllPerms(userId);
if(tmp){
cacheCount--;
}
tmp = UserUtil.refreshUserMenus(userId);
if(tmp){
cacheCount--;
}
}
// 判断删除状态
if(cacheCount != 0){
// 删除缓存失败
throw new ServiceException(CoreMsg.CACHE_DEL_EXCEPTION);
}
}
return true;
}
}

@ -17,15 +17,11 @@ package org.opsli.modulars.system.tenant.service.impl;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import org.opsli.api.wrapper.system.tenant.TenantModel;
import org.opsli.common.constants.MyBatisConstants;
import org.opsli.common.exception.ServiceException;
import org.opsli.common.utils.HumpUtil;
import org.opsli.core.base.service.impl.CrudServiceImpl;
import org.opsli.core.persistence.querybuilder.GenQueryBuilder;
import org.opsli.core.persistence.querybuilder.QueryBuilder;
import org.opsli.core.msg.CoreMsg;
import org.opsli.core.utils.TenantUtil;
import org.opsli.modulars.system.SystemMsg;
import org.opsli.modulars.system.tenant.entity.SysTenant;
@ -36,6 +32,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
@ -87,8 +84,8 @@ public class TenantServiceImpl extends CrudServiceImpl<TenantMapper, SysTenant,
TenantModel tenantModel = super.update(model);
if(tenantModel != null){
// 刷新缓存
TenantUtil.refreshTenant(tenantModel.getId());
// 清除缓存
this.clearCache(Collections.singletonList(model.getId()));
}
return tenantModel;
}
@ -103,11 +100,17 @@ public class TenantServiceImpl extends CrudServiceImpl<TenantMapper, SysTenant,
@Transactional(rollbackFor = Exception.class)
public boolean delete(String id) {
TenantModel tenantModel = this.get(id);
if(tenantModel == null){
return false;
}
boolean ret = super.delete(id);
if(ret){
// 刷新缓存
TenantUtil.refreshTenant(tenantModel.getId());
// 清除缓存
this.clearCache(Collections.singletonList(tenantModel.getId()));
}
return ret;
}
@ -120,11 +123,17 @@ public class TenantServiceImpl extends CrudServiceImpl<TenantMapper, SysTenant,
@Transactional(rollbackFor = Exception.class)
public boolean delete(TenantModel model) {
TenantModel tenantModel = this.get(model);
if(tenantModel == null){
return false;
}
boolean ret = super.delete(model);
if(ret){
// 刷新缓存
TenantUtil.refreshTenant(tenantModel.getId());
// 清除缓存
this.clearCache(Collections.singletonList(tenantModel.getId()));
}
return ret;
}
@ -138,20 +147,10 @@ public class TenantServiceImpl extends CrudServiceImpl<TenantMapper, SysTenant,
public boolean deleteAll(String[] ids) {
List<String> idList = Convert.toList(String.class, ids);
QueryBuilder<SysTenant> queryBuilder = new GenQueryBuilder<>();
QueryWrapper<SysTenant> queryWrapper = queryBuilder.build();
queryWrapper.in(HumpUtil.humpToUnderline(MyBatisConstants.FIELD_ID), idList);
List<SysTenant> list = this.findList(queryWrapper);
boolean ret = super.deleteAll(ids);
if(ret){
if(CollUtil.isNotEmpty(list)){
for (SysTenant sysTenant : list) {
// 刷新缓存
TenantUtil.refreshTenant(sysTenant.getId());
}
}
// 清除缓存
this.clearCache(idList);
}
return ret;
}
@ -170,25 +169,38 @@ public class TenantServiceImpl extends CrudServiceImpl<TenantMapper, SysTenant,
idList.add(model.getId());
}
QueryBuilder<SysTenant> queryBuilder = new GenQueryBuilder<>();
QueryWrapper<SysTenant> queryWrapper = queryBuilder.build();
queryWrapper.in(HumpUtil.humpToUnderline(MyBatisConstants.FIELD_ID), idList);
List<SysTenant> list = this.findList(queryWrapper);
boolean ret = super.deleteAll(models);
if(ret){
if(CollUtil.isNotEmpty(list)){
for (SysTenant sysTenant : list) {
// 刷新缓存
TenantUtil.refreshTenant(sysTenant.getId());
}
}
// 清除缓存
this.clearCache(idList);
}
return ret;
}
// ============
/**
*
* @param tenantIds
*/
private void clearCache(List<String> tenantIds){
// 清空缓存
if(CollUtil.isNotEmpty(tenantIds)){
int cacheCount = 0;
for (String tenantId : tenantIds) {
cacheCount++;
boolean tmp = TenantUtil.refreshTenant(tenantId);
if(tmp){
cacheCount--;
}
}
// 判断删除状态
if(cacheCount != 0){
// 删除缓存失败
throw new ServiceException(CoreMsg.CACHE_DEL_EXCEPTION);
}
}
}
}

@ -15,27 +15,24 @@
*/
package org.opsli.modulars.system.user.service.impl;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.opsli.api.wrapper.system.user.UserOrgRefModel;
import org.opsli.common.exception.ServiceException;
import org.opsli.core.msg.CoreMsg;
import org.opsli.core.utils.OrgUtil;
import org.opsli.core.utils.UserUtil;
import org.opsli.modulars.system.SystemMsg;
import org.opsli.modulars.system.user.entity.SysUserOrgRef;
import org.opsli.modulars.system.user.entity.SysUserRoleRef;
import org.opsli.modulars.system.user.mapper.UserOrgRefMapper;
import org.opsli.modulars.system.user.mapper.UserRoleRefMapper;
import org.opsli.modulars.system.user.service.IUserOrgRefService;
import org.opsli.modulars.system.user.service.IUserRoleRefService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.Collections;
import java.util.List;
@ -99,12 +96,38 @@ public class UserOrgRefServiceImpl extends ServiceImpl<UserOrgRefMapper, SysUser
// 清空缓存
if(saveBatchFlag){
OrgUtil.refreshMenu(model.getUserId());
// 刷新用户缓存
this.clearCache(Collections.singletonList(model.getUserId()));
}
return true;
}
// ============
/**
*
* @param userIds
*/
private void clearCache(List<String> userIds){
// 清空缓存
if(CollUtil.isNotEmpty(userIds)){
int cacheCount = 0;
for (String userId : userIds) {
cacheCount++;
boolean tmp = OrgUtil.refreshOrg(userId);
if(tmp){
cacheCount--;
}
}
// 判断删除状态
if(cacheCount != 0){
// 删除缓存失败
throw new ServiceException(CoreMsg.CACHE_DEL_EXCEPTION);
}
}
}
}

@ -20,6 +20,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.opsli.common.exception.ServiceException;
import org.opsli.core.msg.CoreMsg;
import org.opsli.core.utils.UserUtil;
import org.opsli.modulars.system.SystemMsg;
import org.opsli.modulars.system.user.entity.SysUserRoleRef;
@ -82,15 +83,45 @@ public class UserRoleRefServiceImpl extends ServiceImpl<UserRoleRefMapper, SysUs
entity.setRoleId(roleId);
list.add(entity);
}
super.saveBatch(list);
boolean ret = super.saveBatch(list);
if(ret){
// 清除缓存
this.clearCache(userId);
}
}
return true;
}
// ===========
/**
*
* @param userId
*/
private void clearCache(String userId) {
int cacheCount = 3;
boolean tmp;
// 清空当期用户缓存角色、权限、菜单
UserUtil.refreshUserRoles(userId);
UserUtil.refreshUserAllPerms(userId);
UserUtil.refreshUserMenus(userId);
tmp = UserUtil.refreshUserRoles(userId);
if(tmp){
cacheCount--;
}
tmp = UserUtil.refreshUserAllPerms(userId);
if(tmp){
cacheCount--;
}
tmp = UserUtil.refreshUserMenus(userId);
if(tmp){
cacheCount--;
}
return true;
// 判断删除状态
if(cacheCount != 0){
// 删除缓存失败
throw new ServiceException(CoreMsg.CACHE_DEL_EXCEPTION);
}
}
}

@ -15,9 +15,12 @@
*/
package org.opsli.modulars.system.user.service.impl;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.opsli.api.wrapper.system.menu.MenuModel;
@ -29,6 +32,7 @@ import org.opsli.common.exception.ServiceException;
import org.opsli.common.utils.HumpUtil;
import org.opsli.common.utils.WrapperUtil;
import org.opsli.core.base.service.impl.CrudServiceImpl;
import org.opsli.core.msg.CoreMsg;
import org.opsli.core.persistence.Page;
import org.opsli.core.persistence.querybuilder.GenQueryBuilder;
import org.opsli.core.persistence.querybuilder.QueryBuilder;
@ -44,9 +48,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.*;
/**
@ -134,12 +136,86 @@ public class UserServiceImpl extends CrudServiceImpl<UserMapper, SysUser, UserMo
UserModel update = super.update(model);
if(update != null){
// 刷新用户缓存
UserUtil.refreshUser(update);
this.clearCache(Collections.singletonList(update));
}
return update;
}
@Override
public boolean delete(String id) {
UserModel userModel = super.get(id);
boolean ret = super.delete(id);
if(ret){
// 刷新用户缓存
this.clearCache(Collections.singletonList(userModel));
}
return ret;
}
@Override
public boolean delete(UserModel model) {
UserModel userModel = null;
if(model != null){
userModel = this.get(model.getId());
}
boolean ret = super.delete(model);
if(ret){
if(userModel != null){
// 刷新用户缓存
this.clearCache(Collections.singletonList(userModel));
}
}
return ret;
}
@Override
public boolean deleteAll(String[] ids) {
QueryBuilder<SysUser> queryBuilder = new GenQueryBuilder<>();
QueryWrapper<SysUser> queryWrapper = queryBuilder.build();
List<String> idList = Convert.toList(String.class, ids);
queryWrapper.in(HumpUtil.humpToUnderline(MyBatisConstants.FIELD_ID),idList);
List<UserModel> modelList = super.transformTs2Ms(
this.findList(queryWrapper)
);
boolean ret = super.deleteAll(ids);
if(ret){
// 刷新用户缓存
this.clearCache(modelList);
}
return ret;
}
@Override
public boolean deleteAll(Collection<UserModel> models) {
if(CollUtil.isEmpty(models)){
return false;
}
List<String> idList = Lists.newArrayListWithCapacity(models.size());
for (UserModel model : models) {
idList.add(model.getId());
}
QueryBuilder<SysUser> queryBuilder = new GenQueryBuilder<>();
QueryWrapper<SysUser> queryWrapper = queryBuilder.build();
queryWrapper.in(HumpUtil.humpToUnderline(MyBatisConstants.FIELD_ID),idList);
List<UserModel> modelList = super.transformTs2Ms(
this.findList(queryWrapper)
);
boolean ret = super.deleteAll(models);
if(ret){
// 刷新用户缓存
this.clearCache(modelList);
}
return ret;
}
@Override
public UserModel queryByUserName(String username) {
String key = HumpUtil.humpToUnderline("username");
@ -224,7 +300,7 @@ public class UserServiceImpl extends CrudServiceImpl<UserMapper, SysUser, UserMo
if(ret){
// 刷新用户缓存
UserUtil.refreshUser(userModel);
this.clearCache(Collections.singletonList(userModel));
}
return ret;
@ -255,7 +331,7 @@ public class UserServiceImpl extends CrudServiceImpl<UserMapper, SysUser, UserMo
if(ret){
// 刷新用户缓存
UserUtil.refreshUser(userModel);
this.clearCache(Collections.singletonList(userModel));
}
return ret;
@ -324,6 +400,33 @@ public class UserServiceImpl extends CrudServiceImpl<UserMapper, SysUser, UserMo
return page;
}
// ==================
/**
*
* @param list
*/
private void clearCache(List<UserModel> list){
if(CollUtil.isNotEmpty(list)){
int cacheCount = 0;
for (UserModel userModel : list) {
cacheCount++;
// 刷新用户缓存
boolean tmp = UserUtil.refreshUser(userModel);
if(tmp){
cacheCount--;
}
}
// 判断删除状态
if(cacheCount != 0){
// 删除缓存失败
throw new ServiceException(CoreMsg.CACHE_DEL_EXCEPTION);
}
}
}
}

@ -385,11 +385,6 @@ public class UserRestController extends BaseRestController<SysUser, UserModel, I
IService.delete(id);
// 清除缓存信息
UserModel userModel = new UserModel();
userModel.setId(id);
UserUtil.refreshUser(userModel);
return ResultVo.success("删除用户信息成功");
}
@ -410,13 +405,6 @@ public class UserRestController extends BaseRestController<SysUser, UserModel, I
String[] idArray = Convert.toStrArray(ids);
IService.deleteAll(idArray);
for (String id : idArray) {
// 清除缓存信息
UserModel userModel = new UserModel();
userModel.setId(id);
UserUtil.refreshUser(userModel);
}
return ResultVo.success("批量删除用户信息成功");
}

Loading…
Cancel
Save