修改缓存逻辑.

This commit is contained in:
lijiahang
2023-09-22 18:40:07 +08:00
parent fdc3bc6147
commit 6a3affdbd3
9 changed files with 571 additions and 34 deletions

View File

@@ -0,0 +1,509 @@
package com.orion.ops.framework.redis.core.utils;
import com.alibaba.fastjson.JSON;
import com.orion.lang.define.cache.CacheKeyDefine;
import com.orion.lang.function.Functions;
import com.orion.lang.utils.Objects1;
import com.orion.lang.utils.collect.Lists;
import com.orion.lang.utils.collect.Maps;
import org.springframework.data.redis.core.HashOperations;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* redis hash 工具类
*
* @author Jiahang Li
* @version 1.0.0
* @since 2023/9/22 13:44
*/
@SuppressWarnings("unchecked")
public class RedisMaps extends RedisUtils {
private RedisMaps() {
}
/**
* 插入数据
*
* @param key key
* @param hashKey hashKey
* @param value value
*/
public static void put(CacheKeyDefine key, Object hashKey, Object value) {
put(key.getKey(), hashKey, value);
}
/**
* 插入数据
*
* @param key key
* @param hashKey hashKey
* @param value value
*/
public static void put(String key, Object hashKey, Object value) {
redisTemplate.opsForHash().put(key, Objects1.toString(hashKey), Objects1.toString(value));
}
/**
* 插入数据 json
*
* @param key key
* @param hashKey hashKey
* @param value value
*/
public static void putJson(CacheKeyDefine key, Object hashKey, Object value) {
put(key.getKey(), hashKey, JSON.toJSONString(value));
}
/**
* 插入数据 json
*
* @param key key
* @param hashKey hashKey
* @param value value
*/
public static void putJson(String key, Object hashKey, Object value) {
put(key, hashKey, JSON.toJSONString(value));
}
/**
* 插入数据 json
*
* @param key key
* @param keyMapper keyMapper
* @param value value
* @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));
}
/**
* 插入数据 json
*
* @param key key
* @param keyMapper keyMapper
* @param value value
* @param <V> V
*/
public static <V> void putJson(String key, Function<V, String> keyMapper, V value) {
put(key, keyMapper.apply(value), JSON.toJSONString(value));
}
/**
* 插入数据
*
* @param key key
* @param values values
*/
public static void putAll(CacheKeyDefine key, Map<?, ?> values) {
putAll(key.getKey(), values);
}
/**
* 插入数据
*
* @param key key
* @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);
}
/**
* 插入数据 json
*
* @param key key
* @param values values
* @param <V> V
*/
public static <V> void putAllJson(CacheKeyDefine key, Map<?, V> values) {
putAllJson(key.getKey(), values);
}
/**
* 插入数据 json
*
* @param key key
* @param values values
* @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);
}
/**
* 插入数据 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(), keyMapper, values);
}
/**
* 插入数据 json
*
* @param key key
* @param keyMapper keyMapper
* @param values values
* @param <V> V
*/
public static <V> void putAllJson(String key, 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);
}
/**
* 获取值
*
* @param key key
* @param hashKey hashKey
* @return value
*/
public static String get(CacheKeyDefine key, Object hashKey) {
return get(key.getKey(), hashKey);
}
/**
* 获取值
*
* @param key key
* @param hashKey hashKey
* @return value
*/
public static String get(String key, Object hashKey) {
Object value = redisTemplate.opsForHash().get(key, Objects1.toString(hashKey));
if (value == null) {
return null;
}
return value.toString();
}
/**
* 获取值 json
*
* @param key key
* @param hashKey hashKey
* @param <V> V
* @return value
*/
public static <V> V getJson(CacheKeyDefine key, Object hashKey) {
return getJson(key.getKey(), hashKey, (Class<V>) key.getType());
}
/**
* 获取值 json
*
* @param key key
* @param hashKey hashKey
* @param clazz clazz
* @param <V> V
* @return value
*/
public static <V> V getJson(String key, Object hashKey, Class<V> clazz) {
Object value = redisTemplate.opsForHash().get(key, Objects1.toString(hashKey));
if (value == null) {
return null;
}
return JSON.parseObject(Objects1.toString(value), clazz);
}
/**
* 获取值
*
* @param key key
* @param hashKeys hashKeys
* @return values
*/
public static List<String> multiGet(CacheKeyDefine key, List<?> hashKeys) {
return multiGet(key.getKey(), hashKeys);
}
/**
* 获取值
*
* @param key key
* @param hashKeys hashKeys
* @return values
*/
public static List<String> multiGet(String key, List<?> hashKeys) {
HashOperations<String, String, String> op = redisTemplate.opsForHash();
return op.multiGet(key, Lists.map(hashKeys, Objects1::toString));
}
/**
* 获取值 json
*
* @param key key
* @param hashKeys hashKeys
* @param keyMapper keyMapper
* @param <K> K
* @param <V> V
* @return values
*/
public static <V> List<V> multiGetJson(CacheKeyDefine key, List<?> hashKeys) {
return multiGetJson(key.getKey(), hashKeys, (Class<V>) key.getType());
}
/**
* 获取值 json
*
* @param key key
* @param hashKeys hashKeys
* @param keyMapper keyMapper
* @param clazz clazz
* @param <K> K
* @param <V> V
* @return values
*/
public static <V> List<V> multiGetJson(String key, List<?> hashKeys, Class<V> clazz) {
HashOperations<String, String, String> op = redisTemplate.opsForHash();
List<String> multiKeys = hashKeys.stream()
.map(Objects1::toString)
.collect(Collectors.toList());
return op.multiGet(key, multiKeys)
.stream()
.map(s -> JSON.parseObject(s, clazz))
.collect(Collectors.toList());
}
/**
* 获取所有 key
*
* @param key key
* @return keys
*/
public static List<String> keys(CacheKeyDefine key) {
return keys(key.getKey(), Function.identity());
}
/**
* 获取所有 key
*
* @param key key
* @return keys
*/
public static List<String> keys(String key) {
return keys(key, Function.identity());
}
/**
* 获取所有 key
*
* @param key key
* @param keyMapper keyMapper
* @param <K> K
* @return keys
*/
public static <K> List<K> keys(CacheKeyDefine key, Function<String, K> keyMapper) {
return keys(key.getKey(), keyMapper);
}
/**
* 获取所有 key
*
* @param key key
* @param keyMapper keyMapper
* @param <K> K
* @return keys
*/
public static <K> List<K> keys(String key, Function<String, K> keyMapper) {
HashOperations<String, String, String> op = redisTemplate.opsForHash();
return op.keys(key)
.stream()
.map(keyMapper)
.collect(Collectors.toList());
}
/**
* 删除
*
* @param key key
* @param hashKey hashKey
*/
public static void delete(CacheKeyDefine key, Object hashKey) {
delete(key.getKey(), hashKey);
}
/**
* 删除
*
* @param key key
* @param hashKey hashKey
*/
public static void delete(String key, Object hashKey) {
redisTemplate.opsForHash().delete(key, Objects1.toString(hashKey));
}
/**
* 删除
*
* @param key key
* @param hashKeys hashKeys
*/
public static void delete(CacheKeyDefine key, List<?> hashKeys) {
delete(key.getKey(), hashKeys);
}
/**
* 删除
*
* @param key key
* @param hashKeys hashKeys
*/
public static void delete(String key, List<?> hashKeys) {
Object[] deleteKeys = hashKeys.stream()
.map(Objects1::toString)
.toArray(Object[]::new);
redisTemplate.opsForHash().delete(key, deleteKeys);
}
/**
* 获取数量
*
* @param key key
* @return size
*/
public static Long size(CacheKeyDefine key) {
return size(key.getKey());
}
/**
* 获取数量
*
* @param key key
* @return size
*/
public static Long size(String key) {
return redisTemplate.opsForHash().size(key);
}
/**
* 获取所有键值对
*
* @param key key
* @return entity
*/
public static Map<String, String> entities(CacheKeyDefine key) {
return entities(key.getKey());
}
/**
* 获取所有键值对
*
* @param key key
* @return entity
*/
public static Map<String, String> entities(String key) {
HashOperations<String, String, String> op = redisTemplate.opsForHash();
return op.entries(key);
}
/**
* 获取所有键值对 json
*
* @param key key
* @param <V> V
* @return entity
*/
public static <V> Map<String, V> entitiesJson(CacheKeyDefine key) {
return entitiesJson(key.getKey(), Function.identity(), (Class<V>) key.getType());
}
/**
* 获取所有键值对 json
*
* @param key key
* @param clazz clazz
* @param <V> V
* @return entity
*/
public static <K, V> Map<String, V> entitiesJson(String key, Class<V> clazz) {
return entitiesJson(key, Function.identity(), clazz);
}
/**
* 获取所有键值对 json
*
* @param key key
* @param keyMapper keyMapper
* @param <K> K
* @param <V> V
* @return entity
*/
public static <K, V> Map<K, V> entitiesJson(CacheKeyDefine key, Function<String, K> keyMapper) {
return entitiesJson(key.getKey(), keyMapper, (Class<V>) key.getType());
}
/**
* 获取所有键值对 json
*
* @param key key
* @param keyMapper keyMapper
* @param clazz clazz
* @param <K> K
* @param <V> V
* @return entity
*/
public static <K, V> Map<K, V> entitiesJson(String key, Function<String, K> keyMapper, Class<V> clazz) {
Map<String, String> entities = entities(key);
return Maps.map(entities, keyMapper, v -> (V) JSON.parseObject(v, clazz));
}
/**
* 获取所有值
*
* @param key key
* @return values
*/
public static List<String> values(CacheKeyDefine key) {
return values(key.getKey());
}
/**
* 获取所有值
*
* @param key key
* @return values
*/
public static List<String> values(String key) {
HashOperations<String, String, String> op = redisTemplate.opsForHash();
return op.values(key);
}
/**
* 获取所有值 json
*
* @param key key
* @param <V> V
* @return values
*/
public static <V> List<V> valuesJson(CacheKeyDefine key) {
return valuesJson(key.getKey(), (Class<V>) key.getType());
}
/**
* 获取所有值 json
*
* @param key key
* @param <V> V
* @return values
*/
public static <V> List<V> valuesJson(String key, Class<V> clazz) {
return values(key).stream()
.map(s -> JSON.parseObject(s, clazz))
.collect(Collectors.toList());
}
}

View File

@@ -8,6 +8,7 @@ import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ScanOptions;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
@@ -54,6 +55,33 @@ public class RedisUtils {
});
}
/**
* 删除 key
*
* @param define define
*/
public static void delete(CacheKeyDefine define) {
delete(define.getKey());
}
/**
* 删除 key
*
* @param key key
*/
public static void delete(String key) {
redisTemplate.delete(key);
}
/**
* 删除 key
*
* @param keys keys
*/
public static void delete(List<String> keys) {
redisTemplate.delete(keys);
}
/**
* 设置过期时间
*

View File

@@ -48,13 +48,14 @@ public class ${table.serviceImplName} implements ${table.serviceName} {
this.check${type}Present(record);
// 插入
int effect = ${typeLower}DAO.insert(record);
log.info("${type}Service-create${type} effect: {}", effect);
return record.getId();
Long id = record.getId();
log.info("${type}Service-create${type} id: {}, effect: {}", id, effect);
return id;
}
@Override
public Integer update${type}ById(${type}UpdateRequest request) {
log.info("${type}Service-update${type}ById request: {}", JSON.toJSONString(request));
log.info("${type}Service-update${type}ById id: {}, request: {}", request.getId(), JSON.toJSONString(request));
// 查询
Long id = Valid.notNull(request.getId(), ErrorMessage.ID_MISSING);
${type}DO record = ${typeLower}DAO.selectById(id);

View File

@@ -20,14 +20,14 @@ public interface HostCacheKeyDefine {
.key("host:key:list")
.desc("主机秘钥列表")
.type(HostKeyCacheDTO.class)
.timeout(3, TimeUnit.DAYS)
.timeout(1, TimeUnit.HOURS)
.build();
CacheKeyDefine HOST_IDENTITY = new CacheKeyBuilder()
.key("host:identity:list")
.desc("主机身份列表")
.type(HostIdentityCacheDTO.class)
.timeout(3, TimeUnit.DAYS)
.timeout(1, TimeUnit.HOURS)
.build();
}

View File

@@ -28,4 +28,7 @@ public class HostIdentityCacheDTO implements Serializable {
@Schema(description = "名称")
private String name;
@Schema(description = "用户名")
private String username;
}

View File

@@ -9,7 +9,7 @@ import com.orion.ops.framework.common.constant.Const;
import com.orion.ops.framework.common.constant.ErrorMessage;
import com.orion.ops.framework.common.security.PasswordModifier;
import com.orion.ops.framework.common.utils.Valid;
import com.orion.ops.framework.redis.core.utils.RedisLists;
import com.orion.ops.framework.redis.core.utils.RedisMaps;
import com.orion.ops.module.asset.convert.HostIdentityConvert;
import com.orion.ops.module.asset.dao.HostIdentityDAO;
import com.orion.ops.module.asset.dao.HostKeyDAO;
@@ -60,11 +60,9 @@ public class HostIdentityServiceImpl implements HostIdentityService {
// 插入
int effect = hostIdentityDAO.insert(record);
log.info("HostIdentityService-createHostIdentity effect: {}", effect);
Long id = record.getId();
// 设置缓存
RedisLists.pushJson(HostCacheKeyDefine.HOST_IDENTITY.getKey(), HostIdentityConvert.MAPPER.toCache(record));
RedisLists.setExpire(HostCacheKeyDefine.HOST_IDENTITY);
return id;
// 删除缓存
RedisMaps.delete(HostCacheKeyDefine.HOST_IDENTITY);
return record.getId();
}
@Override
@@ -88,12 +86,12 @@ public class HostIdentityServiceImpl implements HostIdentityService {
.set(HostIdentityDO::getKeyId, request.getKeyId())
.eq(HostIdentityDO::getId, id);
int effect = hostIdentityDAO.update(updateRecord, wrapper);
// 设置缓存
if (!record.getName().equals(updateRecord.getName())) {
RedisLists.removeJson(HostCacheKeyDefine.HOST_IDENTITY.getKey(), HostIdentityConvert.MAPPER.toCache(record));
RedisLists.pushJson(HostCacheKeyDefine.HOST_IDENTITY.getKey(), HostIdentityConvert.MAPPER.toCache(updateRecord));
}
log.info("HostIdentityService-updateHostIdentityById effect: {}", effect);
// 删除缓存
if (!record.getName().equals(updateRecord.getName()) ||
!record.getUsername().equals(updateRecord.getUsername())) {
RedisMaps.delete(HostCacheKeyDefine.HOST_IDENTITY);
}
return effect;
}
@@ -109,7 +107,7 @@ public class HostIdentityServiceImpl implements HostIdentityService {
@Override
public List<HostIdentityVO> getHostIdentityList() {
// 查询缓存
List<HostIdentityCacheDTO> list = RedisLists.rangeJson(HostCacheKeyDefine.HOST_IDENTITY);
List<HostIdentityCacheDTO> list = RedisMaps.valuesJson(HostCacheKeyDefine.HOST_IDENTITY);
if (list.isEmpty()) {
// 查询数据库
list = hostIdentityDAO.of().list(HostIdentityConvert.MAPPER::toCache);
@@ -120,8 +118,8 @@ public class HostIdentityServiceImpl implements HostIdentityService {
.build());
}
// 设置缓存
RedisLists.pushAllJson(HostCacheKeyDefine.HOST_IDENTITY.getKey(), list);
RedisLists.setExpire(HostCacheKeyDefine.HOST_IDENTITY);
RedisMaps.putAllJson(HostCacheKeyDefine.HOST_IDENTITY.getKey(), s -> s.getId().toString(), list);
RedisMaps.setExpire(HostCacheKeyDefine.HOST_IDENTITY);
}
// 删除默认值
return list.stream()
@@ -173,7 +171,7 @@ public class HostIdentityServiceImpl implements HostIdentityService {
// TODO config
// 删除缓存
RedisLists.removeJson(HostCacheKeyDefine.HOST_IDENTITY.getKey(), HostIdentityConvert.MAPPER.toCache(record));
RedisMaps.delete(HostCacheKeyDefine.HOST_IDENTITY.getKey(), record.getId());
log.info("HostIdentityService-deleteHostIdentityById effect: {}", effect);
return effect;
}

View File

@@ -9,7 +9,7 @@ import com.orion.ops.framework.common.constant.ErrorMessage;
import com.orion.ops.framework.common.security.PasswordModifier;
import com.orion.ops.framework.common.utils.CryptoUtils;
import com.orion.ops.framework.common.utils.Valid;
import com.orion.ops.framework.redis.core.utils.RedisLists;
import com.orion.ops.framework.redis.core.utils.RedisMaps;
import com.orion.ops.module.asset.convert.HostKeyConvert;
import com.orion.ops.module.asset.dao.HostIdentityDAO;
import com.orion.ops.module.asset.dao.HostKeyDAO;
@@ -63,9 +63,8 @@ public class HostKeyServiceImpl implements HostKeyService {
int effect = hostKeyDAO.insert(record);
log.info("HostKeyService-createHostKey effect: {}", effect);
Long id = record.getId();
// 设置缓存
RedisLists.pushJson(HostCacheKeyDefine.HOST_KEY.getKey(), HostKeyConvert.MAPPER.toCache(record));
RedisLists.setExpire(HostCacheKeyDefine.HOST_KEY);
// 删除缓存
RedisMaps.delete(HostCacheKeyDefine.HOST_KEY);
return id;
}
@@ -87,10 +86,9 @@ public class HostKeyServiceImpl implements HostKeyService {
updateRecord.setPassword(newPassword);
// 更新
int effect = hostKeyDAO.updateById(updateRecord);
// 设置缓存
// 删除缓存
if (!record.getName().equals(updateRecord.getName())) {
RedisLists.removeJson(HostCacheKeyDefine.HOST_KEY.getKey(), HostKeyConvert.MAPPER.toCache(record));
RedisLists.pushJson(HostCacheKeyDefine.HOST_KEY.getKey(), HostKeyConvert.MAPPER.toCache(updateRecord));
RedisMaps.delete(HostCacheKeyDefine.HOST_KEY);
}
log.info("HostKeyService-updateHostKeyById effect: {}", effect);
return effect;
@@ -124,7 +122,7 @@ public class HostKeyServiceImpl implements HostKeyService {
@Override
public List<HostKeyVO> getHostKeyList() {
// 查询缓存
List<HostKeyCacheDTO> list = RedisLists.rangeJson(HostCacheKeyDefine.HOST_KEY);
List<HostKeyCacheDTO> list = RedisMaps.valuesJson(HostCacheKeyDefine.HOST_KEY);
if (list.isEmpty()) {
// 查询数据库
list = hostKeyDAO.of().list(HostKeyConvert.MAPPER::toCache);
@@ -135,8 +133,8 @@ public class HostKeyServiceImpl implements HostKeyService {
.build());
}
// 设置缓存
RedisLists.pushAllJson(HostCacheKeyDefine.HOST_KEY.getKey(), list);
RedisLists.setExpire(HostCacheKeyDefine.HOST_KEY);
RedisMaps.putAllJson(HostCacheKeyDefine.HOST_KEY.getKey(), s -> s.getId().toString(), list);
RedisMaps.setExpire(HostCacheKeyDefine.HOST_KEY);
}
// 删除默认值
return list.stream()
@@ -171,7 +169,7 @@ public class HostKeyServiceImpl implements HostKeyService {
// TODO config
// 删除缓存
RedisLists.removeJson(HostCacheKeyDefine.HOST_KEY.getKey(), HostKeyConvert.MAPPER.toCache(record));
RedisMaps.delete(HostCacheKeyDefine.HOST_KEY.getKey(), record.getId());
log.info("HostKeyService-deleteHostKeyById effect: {}", effect);
return effect;
}

View File

@@ -18,7 +18,7 @@ public interface FavoriteCacheKeyDefine {
.key("favorite:{}:{}")
.desc("收藏信息 ${type} ${userId}")
.type(Long.class)
.timeout(3, TimeUnit.DAYS)
.timeout(8, TimeUnit.HOURS)
.build();
}

View File

@@ -19,14 +19,14 @@ public interface TagCacheKeyDefine {
.key("tag:name:{}")
.desc("tag 名称 ${type}")
.type(TagCacheDTO.class)
.timeout(3, TimeUnit.DAYS)
.timeout(8, TimeUnit.HOURS)
.build();
CacheKeyDefine TAG_REL = new CacheKeyBuilder()
.key("tag:rel:{}:{}")
.desc("tag 引用 ${type} ${relId}")
.type(TagCacheDTO.class)
.timeout(3, TimeUnit.DAYS)
.timeout(8, TimeUnit.HOURS)
.build();
}