【8.3.3】【system】【cache】增加用户姓名的缓存和刷新

pull/62/head
stylefeng 2025-01-10 11:37:17 +08:00
parent 0588fcfb4e
commit d0d53522da
9 changed files with 150 additions and 3 deletions

View File

@ -7,6 +7,8 @@ import cn.stylefeng.roses.kernel.sys.api.SysUserServiceApi;
/** /**
* *
* <p>
*
* *
* @author fengshuonan * @author fengshuonan
* @since 2023/6/16 22:26 * @since 2023/6/16 22:26

View File

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

View File

@ -0,0 +1,26 @@
package cn.stylefeng.roses.kernel.sys.modular.user.cache.username;
import cn.hutool.cache.impl.TimedCache;
import cn.stylefeng.roses.kernel.cache.memory.AbstractMemoryCacheOperator;
import cn.stylefeng.roses.kernel.sys.modular.user.constants.UserConstants;
/**
*
* <p>
* keyidvalue
*
* @author fengshuonan
* @since 2025/1/10 11:06
*/
public class UserNameMemoryCache extends AbstractMemoryCacheOperator<String> {
public UserNameMemoryCache(TimedCache<String, String> timedCache) {
super(timedCache);
}
@Override
public String getCommonKeyPrefix() {
return UserConstants.USER_NAME_CACHE_PREFIX;
}
}

View File

@ -0,0 +1,26 @@
package cn.stylefeng.roses.kernel.sys.modular.user.cache.username;
import cn.stylefeng.roses.kernel.cache.redis.AbstractRedisCacheOperator;
import cn.stylefeng.roses.kernel.sys.modular.user.constants.UserConstants;
import org.springframework.data.redis.core.RedisTemplate;
/**
*
* <p>
* keyidvalue
*
* @author fengshuonan
* @since 2025/1/10 11:05
*/
public class UserNameRedisCache extends AbstractRedisCacheOperator<String> {
public UserNameRedisCache(RedisTemplate<String, String> redisTemplate) {
super(redisTemplate);
}
@Override
public String getCommonKeyPrefix() {
return UserConstants.USER_NAME_CACHE_PREFIX;
}
}

View File

@ -0,0 +1,35 @@
package cn.stylefeng.roses.kernel.sys.modular.user.cache.username.clear;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi;
import cn.stylefeng.roses.kernel.event.api.annotation.BusinessListener;
import cn.stylefeng.roses.kernel.sys.modular.user.constants.UserConstants;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
/**
*
*
* @author fengshuonan
* @since 2025/1/10 11:10
*/
@Service
public class UserNameClearListener {
@Resource(name = "userNameCache")
private CacheOperatorApi<String> userNameCache;
/**
*
*
* @author fengshuonan
* @since 2025/1/10 11:11
*/
@BusinessListener(businessCode = UserConstants.UPDATE_USER_NAME_EVENT)
public void updateUserName(Long userId) {
if (ObjectUtil.isNotEmpty(userId)) {
userNameCache.remove(String.valueOf(userId));
}
}
}

View File

@ -18,4 +18,14 @@ public interface UserConstants {
*/ */
String UPDATE_USER_ROLE_EVENT = "UPDATE_USER_ROLE_EVENT"; String UPDATE_USER_ROLE_EVENT = "UPDATE_USER_ROLE_EVENT";
/**
*
*/
String USER_NAME_CACHE_PREFIX = "SYS:USER_NAME:";
/**
*
*/
String UPDATE_USER_NAME_EVENT = "UPDATE_USER_NAME_EVENT";
} }

View File

@ -20,6 +20,7 @@ import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.db.mp.tenant.holder.TenantIdHolder; import cn.stylefeng.roses.kernel.db.mp.tenant.holder.TenantIdHolder;
import cn.stylefeng.roses.kernel.db.mp.tenant.holder.TenantSwitchHolder; import cn.stylefeng.roses.kernel.db.mp.tenant.holder.TenantSwitchHolder;
import cn.stylefeng.roses.kernel.dsctn.api.context.DataSourceContext; import cn.stylefeng.roses.kernel.dsctn.api.context.DataSourceContext;
import cn.stylefeng.roses.kernel.event.sdk.publish.BusinessEventPublisher;
import cn.stylefeng.roses.kernel.file.api.FileInfoApi; import cn.stylefeng.roses.kernel.file.api.FileInfoApi;
import cn.stylefeng.roses.kernel.file.api.constants.FileConstants; import cn.stylefeng.roses.kernel.file.api.constants.FileConstants;
import cn.stylefeng.roses.kernel.log.api.util.BusinessLogUtil; import cn.stylefeng.roses.kernel.log.api.util.BusinessLogUtil;
@ -51,13 +52,14 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static cn.stylefeng.roses.kernel.sys.modular.user.constants.UserConstants.UPDATE_USER_NAME_EVENT;
/** /**
* *
* *
@ -92,6 +94,9 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
@Resource @Resource
private SysUserCertificateService sysUserCertificateService; private SysUserCertificateService sysUserCertificateService;
@Resource(name = "userNameCache")
private CacheOperatorApi<String> userNameCache;
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void add(SysUserRequest sysUserRequest) { public void add(SysUserRequest sysUserRequest) {
@ -206,6 +211,9 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
// 记录日志 // 记录日志
BusinessLogUtil.addContent("更新后用户信息如下:\n", sysUser); BusinessLogUtil.addContent("更新后用户信息如下:\n", sysUser);
// 发布修改的事件
BusinessEventPublisher.publishEvent(UPDATE_USER_NAME_EVENT, sysUser.getUserId());
} }
@Override @Override
@ -472,15 +480,29 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
} }
@Override @Override
@Cacheable(value = "users", key = "#userId", unless = "#result.length() == 0")
public String getUserRealName(Long userId) { public String getUserRealName(Long userId) {
if (userId == null) {
return "";
}
// 先从缓存获取
String userNameCached = userNameCache.get(userId.toString());
if (userNameCached != null) {
return userNameCached;
}
LambdaQueryWrapper<SysUser> sysUserLambdaQueryWrapper = new LambdaQueryWrapper<>(); LambdaQueryWrapper<SysUser> sysUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
sysUserLambdaQueryWrapper.select(SysUser::getRealName); sysUserLambdaQueryWrapper.select(SysUser::getRealName);
sysUserLambdaQueryWrapper.eq(SysUser::getUserId, userId); sysUserLambdaQueryWrapper.eq(SysUser::getUserId, userId);
SysUser sysUser = this.getOne(sysUserLambdaQueryWrapper); SysUser sysUser = this.getOne(sysUserLambdaQueryWrapper);
if (sysUser == null) { if (sysUser == null) {
// 查询结果记录到缓存
userNameCache.put(userId.toString(), "");
return ""; return "";
} }
// 查询结果记录到缓存
userNameCache.put(userId.toString(), sysUser.getRealName());
return sysUser.getRealName(); return sysUser.getRealName();
} }

View File

@ -28,6 +28,7 @@ import cn.hutool.cache.CacheUtil;
import cn.hutool.cache.impl.TimedCache; import cn.hutool.cache.impl.TimedCache;
import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi; import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi;
import cn.stylefeng.roses.kernel.sys.api.constants.SysConstants; import cn.stylefeng.roses.kernel.sys.api.constants.SysConstants;
import cn.stylefeng.roses.kernel.sys.modular.user.cache.username.UserNameMemoryCache;
import cn.stylefeng.roses.kernel.sys.modular.user.cache.userrole.UserRoleMemoryCache; import cn.stylefeng.roses.kernel.sys.modular.user.cache.userrole.UserRoleMemoryCache;
import cn.stylefeng.roses.kernel.sys.modular.user.entity.SysUserRole; import cn.stylefeng.roses.kernel.sys.modular.user.entity.SysUserRole;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
@ -59,4 +60,17 @@ public class UserMemoryCacheAutoConfiguration {
return new UserRoleMemoryCache(cache); return new UserRoleMemoryCache(cache);
} }
/**
*
*
* @author fengshuonan
* @since 2025/1/10 11:12
*/
@Bean
public CacheOperatorApi<String> userNameCache() {
// 1小时过期
TimedCache<String, String> cache = CacheUtil.newTimedCache(1000 * SysConstants.DEFAULT_SYS_CACHE_TIMEOUT_SECONDS);
return new UserNameMemoryCache(cache);
}
} }

View File

@ -26,6 +26,7 @@ package cn.stylefeng.roses.kernel.sys.starter.cache.user;
import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi; import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi;
import cn.stylefeng.roses.kernel.cache.redis.util.CreateRedisTemplateUtil; import cn.stylefeng.roses.kernel.cache.redis.util.CreateRedisTemplateUtil;
import cn.stylefeng.roses.kernel.sys.modular.user.cache.username.UserNameRedisCache;
import cn.stylefeng.roses.kernel.sys.modular.user.cache.userrole.UserRoleRedisCache; import cn.stylefeng.roses.kernel.sys.modular.user.cache.userrole.UserRoleRedisCache;
import cn.stylefeng.roses.kernel.sys.modular.user.entity.SysUserRole; import cn.stylefeng.roses.kernel.sys.modular.user.entity.SysUserRole;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
@ -58,4 +59,16 @@ public class UserRedisCacheAutoConfiguration {
return new UserRoleRedisCache(redisTemplate); return new UserRoleRedisCache(redisTemplate);
} }
/**
*
*
* @author fengshuonan
* @since 2025/1/10 11:12
*/
@Bean
public CacheOperatorApi<String> userNameCache(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, String> redisTemplate = CreateRedisTemplateUtil.createObject(redisConnectionFactory);
return new UserNameRedisCache(redisTemplate);
}
} }