refactor: 修改缓存处理逻辑.
This commit is contained in:
@@ -45,19 +45,9 @@ public class CodeGenerators {
|
||||
// .color("blue", "gray", "red", "green", "white")
|
||||
// .valueUseFields()
|
||||
// .build(),
|
||||
Template.create("data_group", "数据分组", "data")
|
||||
Template.create("data_permission", "数据权限", "data")
|
||||
.enableProviderApi()
|
||||
.disableUnitTest()
|
||||
.cache("data:group:{}", "数据分组 ${type}")
|
||||
.expire(1, TimeUnit.DAYS)
|
||||
.vue("system", "data-group")
|
||||
.build(),
|
||||
Template.create("data_group_rel", "数据分组关联", "data")
|
||||
.enableProviderApi()
|
||||
.disableUnitTest()
|
||||
.cache("data:group-rel:{}", "数据分组关联 ${groupId}")
|
||||
.expire(1, TimeUnit.DAYS)
|
||||
.vue("system", "data-group-rel")
|
||||
.build(),
|
||||
};
|
||||
// jdbc 配置 - 使用配置文件
|
||||
|
||||
@@ -13,6 +13,7 @@ import com.orion.ops.framework.common.utils.FileNames;
|
||||
import com.orion.ops.framework.common.utils.Valid;
|
||||
#if($meta.enableCache)
|
||||
import com.orion.ops.framework.redis.core.utils.RedisMaps;
|
||||
import com.orion.ops.framework.redis.core.utils.barrier.CacheBarriers;
|
||||
#end
|
||||
#foreach($pkg in ${customModuleFilePackages})
|
||||
import ${pkg}.*;
|
||||
@@ -138,13 +139,12 @@ public class ${table.serviceImplName} implements ${table.serviceName} {
|
||||
// 查询数据库
|
||||
list = ${typeLower}DAO.of().list(${type}Convert.MAPPER::toCache);
|
||||
// 设置屏障 防止穿透
|
||||
RedisMaps.checkBarrier(list, ${type}CacheDTO::new);
|
||||
CacheBarriers.checkBarrier(list, ${type}CacheDTO::new);
|
||||
// 设置缓存
|
||||
RedisMaps.putAllJson(${type}CacheKeyDefine.${typeConst}.getKey(), s -> s.getId().toString(), list);
|
||||
RedisMaps.setExpire(${type}CacheKeyDefine.${typeConst});
|
||||
RedisMaps.putAllJson(${type}CacheKeyDefine.${typeConst}, s -> s.getId().toString(), list);
|
||||
}
|
||||
// 删除屏障
|
||||
RedisMaps.removeBarrier(list);
|
||||
CacheBarriers.removeBarrier(list);
|
||||
// 转换
|
||||
return list.stream()
|
||||
.map(${type}Convert.MAPPER::to)
|
||||
|
||||
@@ -76,7 +76,7 @@
|
||||
#if($vue.enableCardView)
|
||||
const appStore = useAppStore();
|
||||
|
||||
// FIXME 这里需要修改一下字段名称 同时 appStore 的类型和 AppPreferenceModel 都需要定义该字段类型
|
||||
// FIXME 这里需要修改一下字段名称 并且在 appStore 定义该字段
|
||||
const renderTable = computed(() => appStore.${vue.featureEntityFirstLower}View === 'table');
|
||||
#end
|
||||
|
||||
|
||||
@@ -11,6 +11,8 @@ import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* redis list 工具类
|
||||
* <p>
|
||||
* 写操作会自动设置过期时间 如果有
|
||||
*
|
||||
* @author Jiahang Li
|
||||
* @version 1.0.0
|
||||
@@ -82,6 +84,92 @@ public class RedisLists extends RedisUtils {
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* list 添加元素
|
||||
*
|
||||
* @param key key
|
||||
* @param value value
|
||||
* @param <T> T
|
||||
*/
|
||||
public static <T> void push(CacheKeyDefine key, String value) {
|
||||
push(key.getKey(), key, value, Function.identity());
|
||||
}
|
||||
|
||||
/**
|
||||
* list 添加元素
|
||||
*
|
||||
* @param key key
|
||||
* @param value value
|
||||
* @param <T> T
|
||||
*/
|
||||
public static <T> void push(String key, String value) {
|
||||
push(key, null, value, Function.identity());
|
||||
}
|
||||
|
||||
/**
|
||||
* list 添加元素
|
||||
*
|
||||
* @param key key
|
||||
* @param define define
|
||||
* @param value value
|
||||
* @param <T> T
|
||||
*/
|
||||
public static <T> void push(String key, CacheKeyDefine define, String value) {
|
||||
push(key, define, value, Function.identity());
|
||||
}
|
||||
|
||||
/**
|
||||
* list 添加元素
|
||||
*
|
||||
* @param key key
|
||||
* @param value value
|
||||
* @param mapper mapper
|
||||
* @param <T> T
|
||||
*/
|
||||
public static <T> void push(CacheKeyDefine key, T value, Function<T, String> mapper) {
|
||||
push(key.getKey(), key, value, mapper);
|
||||
}
|
||||
|
||||
/**
|
||||
* list 添加元素
|
||||
*
|
||||
* @param key key
|
||||
* @param value value
|
||||
* @param mapper mapper
|
||||
* @param <T> T
|
||||
*/
|
||||
public static <T> void push(String key, T value, Function<T, String> mapper) {
|
||||
push(key, null, value, mapper);
|
||||
redisTemplate.opsForList().rightPush(key, mapper.apply(value));
|
||||
}
|
||||
|
||||
/**
|
||||
* list 添加元素
|
||||
*
|
||||
* @param key key
|
||||
* @param define define
|
||||
* @param value value
|
||||
* @param mapper mapper
|
||||
* @param <T> T
|
||||
*/
|
||||
public static <T> void push(String key, CacheKeyDefine define, T value, Function<T, String> mapper) {
|
||||
redisTemplate.opsForList().rightPush(key, mapper.apply(value));
|
||||
if (define != null) {
|
||||
setExpire(key, define);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* list 添加元素
|
||||
*
|
||||
* @param key key
|
||||
* @param value value
|
||||
* @param <T> T
|
||||
*/
|
||||
public static <T> void pushJson(String key, T value) {
|
||||
redisTemplate.opsForList().rightPush(key, JSON.toJSONString(value));
|
||||
}
|
||||
|
||||
/**
|
||||
* list 添加元素
|
||||
*
|
||||
@@ -129,45 +217,37 @@ public class RedisLists extends RedisUtils {
|
||||
* @param list list
|
||||
* @param <T> T
|
||||
*/
|
||||
public static <T> void pushAllJson(String key, List<T> list) {
|
||||
List<String> values = list.stream()
|
||||
.map(JSON::toJSONString)
|
||||
.collect(Collectors.toList());
|
||||
redisTemplate.opsForList().rightPushAll(key, values);
|
||||
public static <T> void pushAllJson(CacheKeyDefine key, List<T> list) {
|
||||
pushAllJson(key.getKey(), key, list);
|
||||
}
|
||||
|
||||
/**
|
||||
* list 添加元素
|
||||
*
|
||||
* @param key key
|
||||
* @param value value
|
||||
* @param <T> T
|
||||
* @param key key
|
||||
* @param list list
|
||||
* @param <T> T
|
||||
*/
|
||||
public static <T> void push(String key, String value) {
|
||||
redisTemplate.opsForList().rightPush(key, value);
|
||||
public static <T> void pushAllJson(String key, List<T> list) {
|
||||
pushAllJson(key, null, list);
|
||||
}
|
||||
|
||||
/**
|
||||
* list 添加元素
|
||||
*
|
||||
* @param key key
|
||||
* @param value value
|
||||
* @param mapper mapper
|
||||
* @param define define
|
||||
* @param list list
|
||||
* @param <T> T
|
||||
*/
|
||||
public static <T> void push(String key, T value, Function<T, String> mapper) {
|
||||
redisTemplate.opsForList().rightPush(key, mapper.apply(value));
|
||||
}
|
||||
|
||||
/**
|
||||
* list 添加元素
|
||||
*
|
||||
* @param key key
|
||||
* @param value value
|
||||
* @param <T> T
|
||||
*/
|
||||
public static <T> void pushJson(String key, T value) {
|
||||
redisTemplate.opsForList().rightPush(key, JSON.toJSONString(value));
|
||||
public static <T> void pushAllJson(String key, CacheKeyDefine define, List<T> list) {
|
||||
List<String> values = list.stream()
|
||||
.map(JSON::toJSONString)
|
||||
.collect(Collectors.toList());
|
||||
redisTemplate.opsForList().rightPushAll(key, values);
|
||||
if (define != null) {
|
||||
setExpire(key, define);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -15,6 +15,8 @@ import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* redis hash 工具类
|
||||
* <p>
|
||||
* 写操作会自动设置过期时间 如果有
|
||||
*
|
||||
* @author Jiahang Li
|
||||
* @version 1.0.0
|
||||
@@ -34,7 +36,7 @@ public class RedisMaps extends RedisUtils {
|
||||
* @param value value
|
||||
*/
|
||||
public static void put(CacheKeyDefine key, Object hashKey, Object value) {
|
||||
put(key.getKey(), hashKey, value);
|
||||
put(key.getKey(), key, hashKey, value);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -45,7 +47,22 @@ public class RedisMaps extends RedisUtils {
|
||||
* @param value value
|
||||
*/
|
||||
public static void put(String key, Object hashKey, Object value) {
|
||||
put(key, null, hashKey, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 插入数据
|
||||
*
|
||||
* @param key key
|
||||
* @param define define
|
||||
* @param hashKey hashKey
|
||||
* @param value value
|
||||
*/
|
||||
public static void put(String key, CacheKeyDefine define, Object hashKey, Object value) {
|
||||
redisTemplate.opsForHash().put(key, Objects1.toString(hashKey), Objects1.toString(value));
|
||||
if (define != null) {
|
||||
setExpire(key, define);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -56,7 +73,7 @@ public class RedisMaps extends RedisUtils {
|
||||
* @param value value
|
||||
*/
|
||||
public static void putJson(CacheKeyDefine key, Object hashKey, Object value) {
|
||||
put(key.getKey(), hashKey, JSON.toJSONString(value));
|
||||
put(key.getKey(), key, hashKey, JSON.toJSONString(value));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -67,7 +84,19 @@ public class RedisMaps extends RedisUtils {
|
||||
* @param value value
|
||||
*/
|
||||
public static void putJson(String key, Object hashKey, Object value) {
|
||||
put(key, hashKey, JSON.toJSONString(value));
|
||||
put(key, null, hashKey, JSON.toJSONString(value));
|
||||
}
|
||||
|
||||
/**
|
||||
* 插入数据 json
|
||||
*
|
||||
* @param key key
|
||||
* @param define define
|
||||
* @param hashKey hashKey
|
||||
* @param value value
|
||||
*/
|
||||
public static void putJson(String key, CacheKeyDefine define, Object hashKey, Object value) {
|
||||
put(key, define, hashKey, JSON.toJSONString(value));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -79,7 +108,7 @@ public class RedisMaps extends RedisUtils {
|
||||
* @param <V> V
|
||||
*/
|
||||
public static <V> void putJson(CacheKeyDefine key, Function<V, String> keyMapper, V value) {
|
||||
put(key.getKey(), keyMapper.apply(value), JSON.toJSONString(value));
|
||||
put(key.getKey(), key, keyMapper.apply(value), JSON.toJSONString(value));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -91,7 +120,20 @@ public class RedisMaps extends RedisUtils {
|
||||
* @param <V> V
|
||||
*/
|
||||
public static <V> void putJson(String key, Function<V, String> keyMapper, V value) {
|
||||
put(key, keyMapper.apply(value), JSON.toJSONString(value));
|
||||
put(key, null, keyMapper.apply(value), JSON.toJSONString(value));
|
||||
}
|
||||
|
||||
/**
|
||||
* 插入数据 json
|
||||
*
|
||||
* @param key key
|
||||
* @param define define
|
||||
* @param keyMapper keyMapper
|
||||
* @param value value
|
||||
* @param <V> V
|
||||
*/
|
||||
public static <V> void putJson(String key, CacheKeyDefine define, Function<V, String> keyMapper, V value) {
|
||||
put(key, define, keyMapper.apply(value), JSON.toJSONString(value));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -101,7 +143,7 @@ public class RedisMaps extends RedisUtils {
|
||||
* @param values values
|
||||
*/
|
||||
public static void putAll(CacheKeyDefine key, Map<?, ?> values) {
|
||||
putAll(key.getKey(), values);
|
||||
putAll(key.getKey(), key, values);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -111,19 +153,22 @@ public class RedisMaps extends RedisUtils {
|
||||
* @param values values
|
||||
*/
|
||||
public static void putAll(String key, Map<?, ?> values) {
|
||||
Map<String, String> map = Maps.map(values, Objects1::toString, Objects1::toString);
|
||||
redisTemplate.opsForHash().putAll(key, map);
|
||||
putAll(key, null, values);
|
||||
}
|
||||
|
||||
/**
|
||||
* 插入数据 json
|
||||
* 插入数据
|
||||
*
|
||||
* @param key key
|
||||
* @param define define
|
||||
* @param values values
|
||||
* @param <V> V
|
||||
*/
|
||||
public static <V> void putAllJson(CacheKeyDefine key, Map<?, V> values) {
|
||||
putAllJson(key.getKey(), values);
|
||||
public static void putAll(String key, CacheKeyDefine define, Map<?, ?> values) {
|
||||
Map<String, String> map = Maps.map(values, Objects1::toString, Objects1::toString);
|
||||
redisTemplate.opsForHash().putAll(key, map);
|
||||
if (define != null) {
|
||||
setExpire(key, define);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -134,20 +179,34 @@ public class RedisMaps extends RedisUtils {
|
||||
* @param <V> V
|
||||
*/
|
||||
public static <V> void putAllJson(String key, Map<?, V> values) {
|
||||
Map<String, String> map = Maps.map(values, Objects1::toString, JSON::toJSONString);
|
||||
redisTemplate.opsForHash().putAll(key, map);
|
||||
putAllJson(key, null, values);
|
||||
}
|
||||
|
||||
/**
|
||||
* 插入数据 json
|
||||
*
|
||||
* @param key key
|
||||
* @param keyMapper keyMapper
|
||||
* @param values values
|
||||
* @param <V> V
|
||||
* @param key key
|
||||
* @param values values
|
||||
* @param <V> V
|
||||
*/
|
||||
public static <V> void putAllJson(CacheKeyDefine key, Function<V, String> keyMapper, List<V> values) {
|
||||
putAllJson(key.getKey(), keyMapper, values);
|
||||
public static <V> void putAllJson(CacheKeyDefine key, Map<?, V> values) {
|
||||
putAllJson(key.getKey(), key, values);
|
||||
}
|
||||
|
||||
/**
|
||||
* 插入数据 json
|
||||
*
|
||||
* @param key key
|
||||
* @param define define
|
||||
* @param values values
|
||||
* @param <V> V
|
||||
*/
|
||||
public static <V> void putAllJson(String key, CacheKeyDefine define, Map<?, V> values) {
|
||||
Map<String, String> map = Maps.map(values, Objects1::toString, JSON::toJSONString);
|
||||
redisTemplate.opsForHash().putAll(key, map);
|
||||
if (define != null) {
|
||||
setExpire(key, define);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -159,9 +218,38 @@ public class RedisMaps extends RedisUtils {
|
||||
* @param <V> V
|
||||
*/
|
||||
public static <V> void putAllJson(String key, Function<V, String> keyMapper, List<V> values) {
|
||||
putAllJson(key, null, keyMapper, values);
|
||||
}
|
||||
|
||||
/**
|
||||
* 插入数据 json
|
||||
*
|
||||
* @param key key
|
||||
* @param keyMapper keyMapper
|
||||
* @param values values
|
||||
* @param <V> V
|
||||
*/
|
||||
public static <V> void putAllJson(CacheKeyDefine key, Function<V, String> keyMapper, List<V> values) {
|
||||
putAllJson(key.getKey(), key, keyMapper, values);
|
||||
}
|
||||
|
||||
/**
|
||||
* 插入数据 json
|
||||
*
|
||||
* @param key key
|
||||
* @param define define
|
||||
* @param keyMapper keyMapper
|
||||
* @param values values
|
||||
* @param <V> V
|
||||
*/
|
||||
public static <V> void putAllJson(String key, CacheKeyDefine define,
|
||||
Function<V, String> keyMapper, List<V> values) {
|
||||
Map<String, String> map = values.stream()
|
||||
.collect(Collectors.toMap(keyMapper, JSON::toJSONString, Functions.right()));
|
||||
redisTemplate.opsForHash().putAll(key, map);
|
||||
if (define != null) {
|
||||
setExpire(key, define);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -14,6 +14,8 @@ import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* redis string 工具类
|
||||
* <p>
|
||||
* 写操作会自动设置过期时间 如果有
|
||||
*
|
||||
* @author Jiahang Li
|
||||
* @version 1.0.0
|
||||
@@ -224,7 +226,17 @@ public class RedisStrings extends RedisUtils {
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置 json
|
||||
* 设置值
|
||||
*
|
||||
* @param define define
|
||||
* @param value value
|
||||
*/
|
||||
public static void set(CacheKeyDefine define, Object value) {
|
||||
set(define.getKey(), define, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置值
|
||||
*
|
||||
* @param key key
|
||||
* @param define define
|
||||
@@ -234,7 +246,7 @@ public class RedisStrings extends RedisUtils {
|
||||
if (value == null) {
|
||||
value = Strings.EMPTY;
|
||||
}
|
||||
if (define.getTimeout() == 0) {
|
||||
if (define == null || define.getTimeout() == 0) {
|
||||
// 不过期
|
||||
redisTemplate.opsForValue().set(key, value.toString());
|
||||
} else {
|
||||
@@ -245,6 +257,16 @@ public class RedisStrings extends RedisUtils {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置 json
|
||||
*
|
||||
* @param define define
|
||||
* @param value value
|
||||
*/
|
||||
public static void setJson(CacheKeyDefine define, Object value) {
|
||||
setJson(define.getKey(), define, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置 json
|
||||
*
|
||||
@@ -253,7 +275,7 @@ public class RedisStrings extends RedisUtils {
|
||||
* @param value value
|
||||
*/
|
||||
public static void setJson(String key, CacheKeyDefine define, Object value) {
|
||||
if (define.getTimeout() == 0) {
|
||||
if (define == null || define.getTimeout() == 0) {
|
||||
// 不过期
|
||||
redisTemplate.opsForValue().set(key, JSON.toJSONString(value));
|
||||
} else {
|
||||
|
||||
@@ -20,7 +20,7 @@ import java.util.Set;
|
||||
* @version 1.0.0
|
||||
* @since 2021/11/6 11:09
|
||||
*/
|
||||
public class RedisUtils extends CacheUtils {
|
||||
public class RedisUtils {
|
||||
|
||||
protected static RedisTemplate<String, String> redisTemplate;
|
||||
|
||||
@@ -112,7 +112,7 @@ public class RedisUtils extends CacheUtils {
|
||||
* @param define define
|
||||
*/
|
||||
public static void setExpire(String key, CacheKeyDefine define) {
|
||||
if (define.getTimeout() != 0) {
|
||||
if (define != null && define.getTimeout() != 0) {
|
||||
// 设置过期时间
|
||||
redisTemplate.expire(key, define.getTimeout(), define.getUnit());
|
||||
}
|
||||
|
||||
@@ -1,25 +1,26 @@
|
||||
package com.orion.ops.framework.redis.core.utils;
|
||||
package com.orion.ops.framework.redis.core.utils.barrier;
|
||||
|
||||
import com.orion.lang.define.cache.key.model.LongCacheIdModel;
|
||||
import com.orion.lang.utils.collect.Lists;
|
||||
import com.orion.ops.framework.common.constant.Const;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* 缓存工具类
|
||||
* 缓存屏障工具类
|
||||
*
|
||||
* @author Jiahang Li
|
||||
* @version 1.0.0
|
||||
* @since 2023/11/15 1:22
|
||||
*/
|
||||
public class CacheUtils {
|
||||
public class CacheBarriers {
|
||||
|
||||
protected CacheUtils() {
|
||||
private CacheBarriers() {
|
||||
}
|
||||
|
||||
public static final GenericsListBarrier<Long> LONG = GenericsListBarrier.create(Const.NONE_ID);
|
||||
|
||||
/**
|
||||
* 创建屏障对象 防止穿透
|
||||
*
|
||||
@@ -33,15 +34,14 @@ public class CacheUtils {
|
||||
return val;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 检测是否需要 创建屏障对象 防止穿透
|
||||
* 检测是否需要添加屏障对象 防止穿透
|
||||
*
|
||||
* @param list list
|
||||
* @param supplier supplier
|
||||
* @param <T> T
|
||||
*/
|
||||
public static <T extends LongCacheIdModel> void checkBarrier(List<T> list, Supplier<T> supplier) {
|
||||
public static <T extends LongCacheIdModel> void checkBarrier(Collection<T> list, Supplier<T> supplier) {
|
||||
if (list != null && list.isEmpty()) {
|
||||
// 添加屏障对象
|
||||
list.add(createBarrier(supplier));
|
||||
@@ -0,0 +1,57 @@
|
||||
package com.orion.ops.framework.redis.core.utils.barrier;
|
||||
|
||||
import com.orion.lang.utils.collect.Lists;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* 标准集合屏障
|
||||
*
|
||||
* @author Jiahang Li
|
||||
* @version 1.0.0
|
||||
* @since 2023/11/21 11:46
|
||||
*/
|
||||
public class GenericsListBarrier<T> {
|
||||
|
||||
private final T barrierValue;
|
||||
|
||||
public GenericsListBarrier(T barrierValue) {
|
||||
this.barrierValue = barrierValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建屏障
|
||||
*
|
||||
* @param barrierValue barrierValue
|
||||
* @param <T> T
|
||||
* @return barrier
|
||||
*/
|
||||
public static <T> GenericsListBarrier<T> create(T barrierValue) {
|
||||
return new GenericsListBarrier<>(barrierValue);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 检测是否需要添加屏障对象 防止穿透
|
||||
*
|
||||
* @param list list
|
||||
*/
|
||||
public void check(Collection<T> list) {
|
||||
if (list != null && list.isEmpty()) {
|
||||
// 添加屏障对象
|
||||
list.add(barrierValue);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除屏障对象
|
||||
*
|
||||
* @param list list
|
||||
*/
|
||||
public void remove(Collection<T> list) {
|
||||
if (!Lists.isEmpty(list)) {
|
||||
list.removeIf(s -> s.equals(barrierValue));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user