初始化

This commit is contained in:
2022-06-21 20:47:46 +08:00
parent b666a24a98
commit 59da3376c1
1246 changed files with 129600 additions and 0 deletions

View File

@@ -0,0 +1,52 @@
package com.njcn.redis.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* @author hongawen
* @version 1.0.0
* @date 2021年12月08日 17:53
*/
@Configuration
public class RedisConfig {
/**
* 修改RedisTemplate序列化由JdkSerializationRedisSerializer二进制调整为JSON格式
*/
@Bean("redisTemplate")
public RedisTemplate<String,Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
// 为了开发方便,一般直接使用<String,object>
RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(lettuceConnectionFactory);
// 用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
ObjectMapper objectMapper = new ObjectMapper();
// 指定要序列化的域(field,get,set),访问修饰符(public,private,protected)
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL);
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
// key采用string的序列化方式
redisTemplate.setKeySerializer(stringRedisSerializer);
// value序列化方式采用jackson
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
// hash的key采用string的序列化方式
redisTemplate.setHashKeySerializer(stringRedisSerializer);
// hash的value序列化方式采用jackson
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}

View File

@@ -0,0 +1,28 @@
package com.njcn.redis.config;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
/**
* @author hongawen
* @version 1.0.0
* @date 2022年04月02日 14:28
*/
@Configuration
@RequiredArgsConstructor
public class RedisListenerConfig {
@Bean
RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
return container;
}
}

View File

@@ -0,0 +1,36 @@
package com.njcn.redis.pojo.enums;
import lombok.Getter;
/**
* @author hongawen
* @version 1.0.0
* @createTime 2021年05月11日 17:37
*/
@Getter
public enum RedisKeyEnum {
/**
* 角色与资源的对应关系缓存,每次启动用户服务、角色与资源的关系发生变动时,进行清理并重新缓存
*/
ROLE_FUNCTION_KEY("ROLES_FUNCTIONS",-1L),
PUBLIC_FUNCTIONS_KEY("PUBLIC_FUNCTIONS",-1L),
/**
* 终端信息查询缓存的公共key前缀
*/
DEVICE_INFO_KEY("DEVICE_INFO_PUBLIC:",-1L);
private final String key;
/**
* redis存储的时间单位分钟
*/
private final Long time;
RedisKeyEnum(String key, Long time){
this.key=key;
this.time=time;
}
}

View File

@@ -0,0 +1,234 @@
package com.njcn.redis.utils;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
* @author hongawen
* @version 1.0.0
* @date 2021年04月30日 09:38
*/
@Component
@RequiredArgsConstructor
public class RedisUtil {
private final RedisTemplate<String, Object> redisTemplate;
/**
* 指定key的失效时间
* 秒级别的过期时间
*/
public void expire(String key, long time) {
redisTemplate.expire(key, time, TimeUnit.SECONDS);
}
/**
* 根据key获取过期时间
*/
public long getExpire(String key) {
Long expireTime = redisTemplate.getExpire(key, TimeUnit.SECONDS);
return Objects.isNull(expireTime) ? 0 : expireTime;
}
/**
* 判断key是否存在
*/
public boolean hasKey(String key) {
Boolean hasKeyFlag = redisTemplate.hasKey(key);
if (Objects.isNull(hasKeyFlag)) {
return false;
}
return hasKeyFlag;
}
/**
* 删除某个Key
*/
public void delete(String key) {
redisTemplate.delete(key);
}
/**
* 批量删除keys
*/
public void deleteKeys(String... keys) {
redisTemplate.delete(Arrays.asList(keys));
}
/**
* 获取key对应的字符数据
*/
public String getStringByKey(String key) {
return (String) redisTemplate.opsForValue().get(key);
}
/**
* 获取key对应对象数据
*/
public Object getObjectByKey(String key) {
return redisTemplate.opsForValue().get(key);
}
/**
* 保存数据
*
* @param key 键
* @param value 值
*/
public void saveByKey(String key, Object value) {
redisTemplate.boundValueOps(key).set(value);
}
/**
* 保存数据,指定生命周期(秒)
*
* @param key 键
* @param value 值
* @param expireTime 生命时间
*/
public void saveByKeyWithExpire(String key, Object value, Long expireTime) {
if (expireTime <= 0) {
saveByKey(key, value);
} else {
redisTemplate.boundValueOps(key).set(value, expireTime, TimeUnit.SECONDS);
}
}
/**
* 顺序的递增和递减
*
* @param value 增减根据数值的正负来判断
*/
public void increment(String key, Long value) {
redisTemplate.boundValueOps(key).increment(value);
}
/**
* 添加一个Map集合
*/
public void saveMapValue(String key, Map<String, ?> map, long expireTime) {
redisTemplate.boundHashOps(key).putAll(map);
if (expireTime > 0) {
expire(key, expireTime);
}
}
/**
* 获取map中所有的keys
*/
public Set<?> getMapKeys(String key) {
return redisTemplate.boundHashOps(key).keys();
}
/**
* 获取map中所有的values
*/
public List<?> getMapValues(String key) {
return redisTemplate.boundHashOps(key).values();
}
/**
* 根据map中某个key获取对应的value
*/
public Object getMapValueByMapKey(String redisKey, String mapKey) {
return redisTemplate.boundHashOps(redisKey).get(mapKey);
}
/**
* 根据map中的某个key删除对应的value
*/
public void deleteMapValueByMapKey(String redisKey, String mapKey) {
redisTemplate.boundHashOps(redisKey).delete(mapKey);
}
/**
* 判断map中是否有指定的key
*/
public boolean hasMapKey(String redisKey, String mapKey) {
Boolean hasKey = redisTemplate.boundHashOps(redisKey).hasKey(mapKey);
if (Objects.isNull(hasKey)) {
return false;
}
return hasKey;
}
/**
* 右存放List
*/
public void saveRightListByKey(String key, List<?> values, long expireTime) {
redisTemplate.boundListOps(key).rightPushAll(values);
if (expireTime > 0) {
expire(key, expireTime);
}
}
/**
* 左存放List
*/
public void saveLeftListByKey(String key, List<?> values, long expireTime) {
redisTemplate.boundListOps(key).leftPushAll(values);
if (expireTime > 0) {
expire(key, expireTime);
}
}
/**
* 获取List某范围的值
*
* @param start 起始索引
* @param end 截止索引
*/
public List<?> getListRangeValues(String key, long start, long end) {
long size = getListSize(key);
if ((start < 0 && end < 0) || (start > end)) {
start = 0;
end = size;
} else if (end > size) {
end = size;
} else if (start < 0) {
start = 0;
}
return redisTemplate.boundListOps(key).range(start, end);
}
/**
* 获取List所有数据
*/
public List<?> getListAllValues(String key) {
long size = getListSize(key);
return redisTemplate.boundListOps(key).range(0, size);
}
/**
* 根据指定索引获取指定value
*/
public Object getListValueByIndex(String key, int index) {
return redisTemplate.boundListOps(key).index(index);
}
/**
* 获取list长度
*/
public Long getListSize(String key) {
return Objects.isNull(redisTemplate.boundListOps(key).size()) ? 0 : redisTemplate.boundListOps(key).size();
}
/**
* 根据索引修改某个值
*/
public void updateListValueByIndex(String key, int index, Object newObj) {
redisTemplate.boundListOps(key).set(index, newObj);
}
}