update 优化 缓存注解支持关闭本地缓存

This commit is contained in:
疯狂的狮子Li 2025-04-14 10:09:03 +08:00
parent 142fb33d81
commit 33e1d34ce5
3 changed files with 22 additions and 11 deletions

View File

@ -3,13 +3,14 @@ package org.dromara.common.core.constant;
/** /**
* 缓存组名称常量 * 缓存组名称常量
* <p> * <p>
* key 格式为 cacheNames#ttl#maxIdleTime#maxSize * key 格式为 cacheNames#ttl#maxIdleTime#maxSize#local
* <p> * <p>
* ttl 过期时间 如果设置为0则不过期 默认为0 * ttl 过期时间 如果设置为0则不过期 默认为0
* maxIdleTime 最大空闲时间 根据LRU算法清理空闲数据 如果设置为0则不检测 默认为0 * maxIdleTime 最大空闲时间 根据LRU算法清理空闲数据 如果设置为0则不检测 默认为0
* maxSize 组最大长度 根据LRU算法清理溢出数据 如果设置为0则无限长 默认为0 * maxSize 组最大长度 根据LRU算法清理溢出数据 如果设置为0则无限长 默认为0
* local 默认开启本地缓存为1 关闭本地缓存为0
* <p> * <p>
* 例子: test#60stest#0#60stest#0#1m#1000test#1h#0#500 * 例子: test#60stest#0#60stest#0#1m#1000test#1h#0#500test#1h#0#500#0
* *
* @author Lion Li * @author Lion Li
*/ */

View File

@ -145,18 +145,25 @@ public class PlusSpringCacheManager implements CacheManager {
if (array.length > 3) { if (array.length > 3) {
config.setMaxSize(Integer.parseInt(array[3])); config.setMaxSize(Integer.parseInt(array[3]));
} }
int local = 1;
if (config.getMaxIdleTime() == 0 && config.getTTL() == 0 && config.getMaxSize() == 0) { if (array.length > 4) {
return createMap(name, config); local = Integer.parseInt(array[4]);
} }
return createMapCache(name, config); if (config.getMaxIdleTime() == 0 && config.getTTL() == 0 && config.getMaxSize() == 0) {
return createMap(name, config, local);
}
return createMapCache(name, config, local);
} }
private Cache createMap(String name, CacheConfig config) { private Cache createMap(String name, CacheConfig config, int local) {
RMap<Object, Object> map = RedisUtils.getClient().getMap(name); RMap<Object, Object> map = RedisUtils.getClient().getMap(name);
Cache cache = new CaffeineCacheDecorator(name, new RedissonCache(map, allowNullValues)); Cache cache = new RedissonCache(map, allowNullValues);
if (local == 1) {
cache = new CaffeineCacheDecorator(name, cache);
}
if (transactionAware) { if (transactionAware) {
cache = new TransactionAwareCacheDecorator(cache); cache = new TransactionAwareCacheDecorator(cache);
} }
@ -167,10 +174,13 @@ public class PlusSpringCacheManager implements CacheManager {
return cache; return cache;
} }
private Cache createMapCache(String name, CacheConfig config) { private Cache createMapCache(String name, CacheConfig config, int local) {
RMapCache<Object, Object> map = RedisUtils.getClient().getMapCache(name); RMapCache<Object, Object> map = RedisUtils.getClient().getMapCache(name);
Cache cache = new CaffeineCacheDecorator(name, new RedissonCache(map, config, allowNullValues)); Cache cache = new RedissonCache(map, config, allowNullValues);
if (local == 1) {
cache = new CaffeineCacheDecorator(name, cache);
}
if (transactionAware) { if (transactionAware) {
cache = new TransactionAwareCacheDecorator(cache); cache = new TransactionAwareCacheDecorator(cache);
} }

View File

@ -40,7 +40,7 @@ public class RedisCacheController {
* <p> * <p>
* cacheNames 命名规则 查看 {@link CacheNames} 注释 支持多参数 * cacheNames 命名规则 查看 {@link CacheNames} 注释 支持多参数
*/ */
@Cacheable(cacheNames = "demo:cache#60s#10m#20", key = "#key", condition = "#key != null") @Cacheable(cacheNames = "demo:cache#60s#10m#20#1", key = "#key", condition = "#key != null")
@GetMapping("/test1") @GetMapping("/test1")
public R<String> test1(String key, String value) { public R<String> test1(String key, String value) {
return R.ok("操作成功", value); return R.ok("操作成功", value);