Skip to content

Commit 2eabbe2

Browse files
committed
feat: 添加 RedisCacheManager 配置,修改 KeyPrefix
1 parent defff12 commit 2eabbe2

3 files changed

Lines changed: 44 additions & 6 deletions

File tree

common/plugin/cache/src/main/java/top/cadecode/uniboot/common/plugin/cache/config/CacheConfig.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@
77
import org.springframework.cache.caffeine.CaffeineCacheManager;
88
import org.springframework.context.annotation.Bean;
99
import org.springframework.context.annotation.Configuration;
10+
import org.springframework.context.annotation.Primary;
11+
import org.springframework.data.redis.cache.RedisCacheConfiguration;
12+
import org.springframework.data.redis.cache.RedisCacheManager;
13+
import org.springframework.data.redis.core.RedisTemplate;
14+
import org.springframework.data.redis.serializer.RedisSerializationContext.SerializationPair;
1015

16+
import java.time.Duration;
1117
import java.util.concurrent.TimeUnit;
1218

1319
/**
@@ -36,4 +42,38 @@ public CacheManager caffeine5s() {
3642
caffeineCacheManager.setAllowNullValues(true);
3743
return caffeineCacheManager;
3844
}
45+
46+
/**
47+
* Redis 缓存
48+
* 过期时间 5 分钟
49+
*
50+
* @return RedisCacheManager 实例
51+
*/
52+
@Bean
53+
public CacheManager redis5m(RedisTemplate<String, ?> jsonRedisTemplate) {
54+
return geneRedisCacheManager(jsonRedisTemplate, 5);
55+
}
56+
57+
/**
58+
* Redis 缓存
59+
* 过期时间 30 分钟
60+
*
61+
* @return RedisCacheManager 实例
62+
*/
63+
@Primary
64+
@Bean
65+
public CacheManager redis30m(RedisTemplate<String, ?> jsonRedisTemplate) {
66+
return geneRedisCacheManager(jsonRedisTemplate, 30);
67+
}
68+
69+
private static RedisCacheManager geneRedisCacheManager(RedisTemplate<String, ?> jsonRedisTemplate, long minutes) {
70+
RedisCacheConfiguration cacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
71+
.serializeKeysWith(SerializationPair.fromSerializer(jsonRedisTemplate.getStringSerializer()))
72+
.serializeValuesWith(SerializationPair.fromSerializer(jsonRedisTemplate.getValueSerializer()))
73+
.entryTtl(Duration.ofMinutes(minutes));
74+
return RedisCacheManager.builder(jsonRedisTemplate.getConnectionFactory())
75+
.cacheDefaults(cacheConfiguration)
76+
.transactionAware()
77+
.build();
78+
}
3979
}

framework/src/main/java/top/cadecode/uniboot/framework/consts/KeyPrefix.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ public interface KeyPrefix {
1111
/**
1212
* 登录用户信息
1313
*/
14-
String LOGIN_USER = "login:user";
14+
String LOGIN_USER = "frame:loginUser";
1515

1616
/**
1717
* Api Roles 映射关系
1818
*/
19-
String API_ROLES = "api:roles";
19+
String API_ROLES = "frame:apiRoles";
2020
}

framework/src/main/java/top/cadecode/uniboot/framework/serviceimpl/SysApiServiceImpl.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,12 @@ public class SysApiServiceImpl extends ServiceImpl<SysApiMapper, SysApi> impleme
3434
@Cacheable(cacheNames = KeyPrefix.API_ROLES, cacheManager = "caffeine5s")
3535
@Override
3636
public List<SysApiRolesVo> listRolesVo() {
37-
List<SysApiRolesVo> sysApiRolesVos = RedisUtil.get(KeyPrefix.API_ROLES, new TypeReference<List<SysApiRolesVo>>() {
38-
});
37+
List<SysApiRolesVo> sysApiRolesVos = RedisUtil.get(KeyPrefix.API_ROLES, new TypeReference<List<SysApiRolesVo>>() {});
3938
if (ObjectUtil.isNotNull(sysApiRolesVos)) {
4039
return sysApiRolesVos;
4140
}
4241
synchronized (this) {
43-
sysApiRolesVos = RedisUtil.get(KeyPrefix.API_ROLES, new TypeReference<List<SysApiRolesVo>>() {
44-
});
42+
sysApiRolesVos = RedisUtil.get(KeyPrefix.API_ROLES, new TypeReference<List<SysApiRolesVo>>() {});
4543
if (ObjectUtil.isNotNull(sysApiRolesVos)) {
4644
return sysApiRolesVos;
4745
}

0 commit comments

Comments
 (0)