【7.1.1】【cache】优化用户组织机构的缓存

pull/22/head
fengshuonan 2021-07-30 23:14:48 +08:00
parent cc7055fbbe
commit 9a463b4970
5 changed files with 149 additions and 0 deletions

View File

@ -52,4 +52,9 @@ public interface SystemCachesConstants {
*/
String ROLE_INFO_CACHE_PREFIX = "role:";
/**
*
*/
String USER_ORG_CACHE_PREFIX = "user_org:";
}

View File

@ -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.
*
* GunsAPACHE 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;
}
}

View File

@ -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.
*
* GunsAPACHE 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;
}
}

View File

@ -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));
}

View File

@ -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);
}
}