mirror of https://gitee.com/stylefeng/roses
【7.1.1】【cache】优化用户组织机构的缓存
parent
cc7055fbbe
commit
9a463b4970
|
@ -52,4 +52,9 @@ public interface SystemCachesConstants {
|
|||
*/
|
||||
String ROLE_INFO_CACHE_PREFIX = "role:";
|
||||
|
||||
/**
|
||||
* 用户组织机构缓存的前缀
|
||||
*/
|
||||
String USER_ORG_CACHE_PREFIX = "user_org:";
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,49 @@
|
|||
/*
|
||||
* Copyright [2020-2030] [https://www.stylefeng.cn]
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Guns采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点:
|
||||
*
|
||||
* 1.请不要删除和修改根目录下的LICENSE文件。
|
||||
* 2.请不要删除和修改Guns源码头部的版权声明。
|
||||
* 3.请保留源码和相关描述文件的项目出处,作者声明等。
|
||||
* 4.分发源码时候,请注明软件出处 https://gitee.com/stylefeng/guns
|
||||
* 5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/stylefeng/guns
|
||||
* 6.若您的项目无法满足以上几点,可申请商业授权
|
||||
*/
|
||||
package cn.stylefeng.roses.kernel.system.modular.user.cache;
|
||||
|
||||
import cn.hutool.cache.impl.TimedCache;
|
||||
import cn.stylefeng.roses.kernel.cache.memory.AbstractMemoryCacheOperator;
|
||||
import cn.stylefeng.roses.kernel.system.api.constants.SystemCachesConstants;
|
||||
import cn.stylefeng.roses.kernel.system.api.pojo.user.SysUserOrgDTO;
|
||||
|
||||
/**
|
||||
* 用户组织机构缓存
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2021/7/30 23:07
|
||||
*/
|
||||
public class UserOrgMemoryCache extends AbstractMemoryCacheOperator<SysUserOrgDTO> {
|
||||
|
||||
public UserOrgMemoryCache(TimedCache<String, SysUserOrgDTO> timedCache) {
|
||||
super(timedCache);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCommonKeyPrefix() {
|
||||
return SystemCachesConstants.USER_ORG_CACHE_PREFIX;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
/*
|
||||
* Copyright [2020-2030] [https://www.stylefeng.cn]
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Guns采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点:
|
||||
*
|
||||
* 1.请不要删除和修改根目录下的LICENSE文件。
|
||||
* 2.请不要删除和修改Guns源码头部的版权声明。
|
||||
* 3.请保留源码和相关描述文件的项目出处,作者声明等。
|
||||
* 4.分发源码时候,请注明软件出处 https://gitee.com/stylefeng/guns
|
||||
* 5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/stylefeng/guns
|
||||
* 6.若您的项目无法满足以上几点,可申请商业授权
|
||||
*/
|
||||
package cn.stylefeng.roses.kernel.system.modular.user.cache;
|
||||
|
||||
import cn.stylefeng.roses.kernel.cache.redis.AbstractRedisCacheOperator;
|
||||
import cn.stylefeng.roses.kernel.system.api.constants.SystemCachesConstants;
|
||||
import cn.stylefeng.roses.kernel.system.api.pojo.user.SysUserDTO;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
|
||||
/**
|
||||
* 用户组织机构缓存
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2021/2/28 10:23
|
||||
*/
|
||||
public class UserOrgRedisCache extends AbstractRedisCacheOperator<SysUserDTO> {
|
||||
|
||||
public UserOrgRedisCache(RedisTemplate<String, SysUserDTO> redisTemplate) {
|
||||
super(redisTemplate);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCommonKeyPrefix() {
|
||||
return SystemCachesConstants.USER_ORG_CACHE_PREFIX;
|
||||
}
|
||||
|
||||
}
|
|
@ -26,6 +26,7 @@ package cn.stylefeng.roses.kernel.system.modular.user.service.impl;
|
|||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi;
|
||||
import cn.stylefeng.roses.kernel.system.api.exception.SystemModularException;
|
||||
import cn.stylefeng.roses.kernel.system.api.exception.enums.user.SysUserOrgExceptionEnum;
|
||||
import cn.stylefeng.roses.kernel.system.api.pojo.user.SysUserOrgDTO;
|
||||
|
@ -38,6 +39,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
|
@ -50,9 +52,19 @@ import java.util.List;
|
|||
@Service
|
||||
public class SysUserOrgServiceServiceImpl extends ServiceImpl<SysUserOrgMapper, SysUserOrg> implements SysUserOrgService {
|
||||
|
||||
@Resource(name = "userOrgCacheApi")
|
||||
private CacheOperatorApi<SysUserOrgDTO> userOrgCacheApi;
|
||||
|
||||
@Override
|
||||
public SysUserOrgDTO getUserOrgByUserId(Long userId) {
|
||||
|
||||
// 从缓存获取数据
|
||||
String key = String.valueOf(userId);
|
||||
SysUserOrgDTO sysUserDTOCache = userOrgCacheApi.get(key);
|
||||
if (sysUserDTOCache != null) {
|
||||
return sysUserDTOCache;
|
||||
}
|
||||
|
||||
UserOrgRequest userOrgRequest = new UserOrgRequest();
|
||||
userOrgRequest.setUserId(userId);
|
||||
SysUserOrg sysUserOrg = this.detail(userOrgRequest);
|
||||
|
@ -61,6 +73,10 @@ public class SysUserOrgServiceServiceImpl extends ServiceImpl<SysUserOrgMapper,
|
|||
}
|
||||
SysUserOrgDTO sysUserOrgDTO = new SysUserOrgDTO();
|
||||
BeanUtil.copyProperties(sysUserOrg, sysUserOrgDTO);
|
||||
|
||||
// 加入缓存
|
||||
userOrgCacheApi.put(key, sysUserOrgDTO);
|
||||
|
||||
return sysUserOrgDTO;
|
||||
}
|
||||
|
||||
|
@ -93,6 +109,9 @@ public class SysUserOrgServiceServiceImpl extends ServiceImpl<SysUserOrgMapper,
|
|||
public void del(UserOrgRequest userOrgResponse) {
|
||||
SysUserOrg sysUserOrg = this.querySysUserOrgById(userOrgResponse);
|
||||
this.removeById(sysUserOrg.getUserOrgId());
|
||||
|
||||
// 清除缓存
|
||||
userOrgCacheApi.remove(String.valueOf(sysUserOrg.getUserId()));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -100,6 +119,9 @@ public class SysUserOrgServiceServiceImpl extends ServiceImpl<SysUserOrgMapper,
|
|||
LambdaQueryWrapper<SysUserOrg> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(SysUserOrg::getUserId, userId);
|
||||
this.remove(queryWrapper);
|
||||
|
||||
// 清除缓存
|
||||
userOrgCacheApi.remove(String.valueOf(userId));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -107,6 +129,9 @@ public class SysUserOrgServiceServiceImpl extends ServiceImpl<SysUserOrgMapper,
|
|||
SysUserOrg sysUserOrg = this.querySysUserOrgById(userOrgResponse);
|
||||
BeanUtil.copyProperties(userOrgResponse, sysUserOrg);
|
||||
this.updateById(sysUserOrg);
|
||||
|
||||
// 清除缓存
|
||||
userOrgCacheApi.remove(String.valueOf(sysUserOrg.getUserId()));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -118,6 +143,9 @@ public class SysUserOrgServiceServiceImpl extends ServiceImpl<SysUserOrgMapper,
|
|||
|
||||
// 新增组织机构绑定
|
||||
this.add(userId, orgId);
|
||||
|
||||
// 清除缓存
|
||||
userOrgCacheApi.remove(String.valueOf(userId));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -129,6 +157,9 @@ public class SysUserOrgServiceServiceImpl extends ServiceImpl<SysUserOrgMapper,
|
|||
|
||||
// 新增组织机构绑定
|
||||
this.add(userId, orgId, positionId);
|
||||
|
||||
// 清除缓存
|
||||
userOrgCacheApi.remove(String.valueOf(userId));
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -29,9 +29,11 @@ import cn.hutool.cache.impl.TimedCache;
|
|||
import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi;
|
||||
import cn.stylefeng.roses.kernel.system.api.constants.SystemCachesConstants;
|
||||
import cn.stylefeng.roses.kernel.system.api.pojo.user.SysUserDTO;
|
||||
import cn.stylefeng.roses.kernel.system.api.pojo.user.SysUserOrgDTO;
|
||||
import cn.stylefeng.roses.kernel.system.modular.role.cache.RoleMemoryCache;
|
||||
import cn.stylefeng.roses.kernel.system.modular.role.entity.SysRole;
|
||||
import cn.stylefeng.roses.kernel.system.modular.user.cache.SysUserMemoryCache;
|
||||
import cn.stylefeng.roses.kernel.system.modular.user.cache.UserOrgMemoryCache;
|
||||
import cn.stylefeng.roses.kernel.system.modular.user.cache.UserRoleMemoryCache;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
@ -87,4 +89,17 @@ public class GunsSystemCacheAutoConfiguration {
|
|||
return new RoleMemoryCache(roleCache);
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户组织机构的缓存
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2021/7/30 23:09
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(name = "userOrgCacheApi")
|
||||
public CacheOperatorApi<SysUserOrgDTO> userOrgCacheApi() {
|
||||
TimedCache<String, SysUserOrgDTO> roleCache = CacheUtil.newTimedCache(SystemCachesConstants.USER_CACHE_TIMEOUT_SECONDS * 1000);
|
||||
return new UserOrgMemoryCache(roleCache);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue