【8.3.3】【system】【cache】增加组织机构的缓存

master^2
stylefeng 2025-01-10 13:33:37 +08:00
parent 6afa0bf670
commit 243be73ea5
8 changed files with 145 additions and 2 deletions

View File

@ -0,0 +1,27 @@
package cn.stylefeng.roses.kernel.sys.modular.org.cache.orginfo;
import cn.hutool.cache.impl.TimedCache;
import cn.stylefeng.roses.kernel.cache.memory.AbstractMemoryCacheOperator;
import cn.stylefeng.roses.kernel.sys.api.pojo.org.HrOrganizationDTO;
import cn.stylefeng.roses.kernel.sys.modular.org.constants.OrgConstants;
/**
*
* <p>
* keyidvalue
*
* @author fengshuonan
* @since 2025/1/10 11:47
*/
public class SysOrgInfoMemoryCache extends AbstractMemoryCacheOperator<HrOrganizationDTO> {
public SysOrgInfoMemoryCache(TimedCache<String, HrOrganizationDTO> timedCache) {
super(timedCache);
}
@Override
public String getCommonKeyPrefix() {
return OrgConstants.ORG_INFO_CACHE_PREFIX;
}
}

View File

@ -0,0 +1,27 @@
package cn.stylefeng.roses.kernel.sys.modular.org.cache.orginfo;
import cn.stylefeng.roses.kernel.cache.redis.AbstractRedisCacheOperator;
import cn.stylefeng.roses.kernel.sys.api.pojo.org.HrOrganizationDTO;
import cn.stylefeng.roses.kernel.sys.modular.org.constants.OrgConstants;
import org.springframework.data.redis.core.RedisTemplate;
/**
*
* <p>
* keyidvalue
*
* @author fengshuonan
* @since 2023/7/14 1:06
*/
public class SysOrgInfoRedisCache extends AbstractRedisCacheOperator<HrOrganizationDTO> {
public SysOrgInfoRedisCache(RedisTemplate<String, HrOrganizationDTO> redisTemplate) {
super(redisTemplate);
}
@Override
public String getCommonKeyPrefix() {
return OrgConstants.ORG_INFO_CACHE_PREFIX;
}
}

View File

@ -0,0 +1,33 @@
package cn.stylefeng.roses.kernel.sys.modular.org.cache.orginfo.clear;
import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi;
import cn.stylefeng.roses.kernel.event.api.annotation.BusinessListener;
import cn.stylefeng.roses.kernel.sys.api.pojo.org.HrOrganizationDTO;
import cn.stylefeng.roses.kernel.sys.modular.org.constants.OrgConstants;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
/**
*
*
* @author fengshuonan
* @since 2025/1/10 11:48
*/
@Service
public class OrgInfoClearListener {
@Resource(name = "sysOrgInfoCache")
private CacheOperatorApi<HrOrganizationDTO> sysOrgInfoCache;
/**
*
*
* @author fengshuonan
* @since 2025/1/10 12:32
*/
@BusinessListener(businessCode = OrgConstants.UPDATE_ORG_INFO_EVENT)
public void updateOrgCallback(Long orgId) {
sysOrgInfoCache.remove(orgId.toString());
}
}

View File

@ -1 +0,0 @@
package cn.stylefeng.roses.kernel.sys.modular.org.cache;

View File

@ -57,4 +57,16 @@ public interface OrgConstants {
*/
String DELETE_ORG_EVENT = "DELETE_ORG_EVENT";
//-------------------------------组织机构详情缓存-------------------------------
/**
*
*/
String ORG_INFO_CACHE_PREFIX = "SYS:ORG:INFO:";
/**
*
*/
String UPDATE_ORG_INFO_EVENT = "UPDATE_ORG_INFO_EVENT";
}

View File

@ -79,6 +79,9 @@ public class HrOrganizationServiceImpl extends ServiceImpl<HrOrganizationMapper,
@Resource(name = "sysOrgSubFlagCache")
private CacheOperatorApi<Boolean> sysOrgSubFlagCache;
@Resource(name = "sysOrgInfoCache")
private CacheOperatorApi<HrOrganizationDTO> sysOrgInfoCache;
@Override
public void add(HrOrganizationRequest hrOrganizationRequest) {
HrOrganization hrOrganization = new HrOrganization();
@ -157,6 +160,7 @@ public class HrOrganizationServiceImpl extends ServiceImpl<HrOrganizationMapper,
// 发布编辑机构事件
BusinessEventPublisher.publishEvent(OrgConstants.EDIT_ORG_EVENT, null);
BusinessEventPublisher.publishEvent(OrgConstants.UPDATE_ORG_INFO_EVENT, hrOrganization.getOrgId());
// 记录日志
BusinessLogUtil.addContent("更新后的机构信息为:\n", hrOrganization);
@ -367,6 +371,15 @@ public class HrOrganizationServiceImpl extends ServiceImpl<HrOrganizationMapper,
@Override
public HrOrganizationDTO getOrgInfo(Long orgId) {
if (orgId == null) {
return new HrOrganizationDTO();
}
// 先从缓存中获取
HrOrganizationDTO hrOrganizationDTO = sysOrgInfoCache.get(String.valueOf(orgId));
if (hrOrganizationDTO != null) {
return hrOrganizationDTO;
}
// 查询组织机构对应的信息
LambdaQueryWrapper<HrOrganization> hrOrganizationLambdaQueryWrapper = new LambdaQueryWrapper<>();
@ -376,11 +389,14 @@ public class HrOrganizationServiceImpl extends ServiceImpl<HrOrganizationMapper,
HrOrganization hrOrganization = this.getOne(hrOrganizationLambdaQueryWrapper, false);
if (ObjectUtil.isEmpty(hrOrganization)) {
sysOrgInfoCache.put(String.valueOf(orgId), new HrOrganizationDTO());
return new HrOrganizationDTO();
}
HrOrganizationDTO hrOrganizationDTO = new HrOrganizationDTO();
hrOrganizationDTO = new HrOrganizationDTO();
BeanUtil.copyProperties(hrOrganization, hrOrganizationDTO);
sysOrgInfoCache.put(String.valueOf(orgId), hrOrganizationDTO);
return hrOrganizationDTO;
}

View File

@ -28,6 +28,8 @@ import cn.hutool.cache.CacheUtil;
import cn.hutool.cache.impl.TimedCache;
import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi;
import cn.stylefeng.roses.kernel.sys.api.constants.SysConstants;
import cn.stylefeng.roses.kernel.sys.api.pojo.org.HrOrganizationDTO;
import cn.stylefeng.roses.kernel.sys.modular.org.cache.orginfo.SysOrgInfoMemoryCache;
import cn.stylefeng.roses.kernel.sys.modular.org.cache.subflag.SysOrgSubFlagMemoryCache;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.context.annotation.Bean;
@ -56,4 +58,17 @@ public class OrgMemoryCacheAutoConfiguration {
return new SysOrgSubFlagMemoryCache(themeCache);
}
/**
*
*
* @author fengshuonan
* @since 2025/1/10 11:48
*/
@Bean
public CacheOperatorApi<HrOrganizationDTO> sysOrgInfoCache() {
// 1小时过期
TimedCache<String, HrOrganizationDTO> themeCache = CacheUtil.newTimedCache(1000 * SysConstants.DEFAULT_SYS_CACHE_TIMEOUT_SECONDS);
return new SysOrgInfoMemoryCache(themeCache);
}
}

View File

@ -26,6 +26,8 @@ package cn.stylefeng.roses.kernel.sys.starter.cache.org;
import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi;
import cn.stylefeng.roses.kernel.cache.redis.util.CreateRedisTemplateUtil;
import cn.stylefeng.roses.kernel.sys.api.pojo.org.HrOrganizationDTO;
import cn.stylefeng.roses.kernel.sys.modular.org.cache.orginfo.SysOrgInfoRedisCache;
import cn.stylefeng.roses.kernel.sys.modular.org.cache.subflag.SysOrgSubFlagRedisCache;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
@ -55,4 +57,16 @@ public class OrgRedisCacheAutoConfiguration {
return new SysOrgSubFlagRedisCache(redisTemplate);
}
/**
*
*
* @author fengshuonan
* @since 2025/1/10 11:49
*/
@Bean
public CacheOperatorApi<HrOrganizationDTO> sysOrgInfoCache(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, HrOrganizationDTO> redisTemplate = CreateRedisTemplateUtil.createObject(redisConnectionFactory);
return new SysOrgInfoRedisCache(redisTemplate);
}
}