【tower-boot 系列】redis集成
redis 介绍
Redis是一种支持key-value等多种数据结构的存储系统。可用于缓存,事件发布或订阅,高速队列等场景。支持网络,提供字符串,哈希,列表,队列,集合结构直接存取,基于内存,可持久化。
redis 集成
一、核心装配
1、RedisTemplate 注入
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory,
ObjectMapper objectMapper, KeyStringSerializer keyStringSerializer) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
//key序列化方式
RedisSerializer<String> defalutSerializer = template.getStringSerializer();
//值序列化方式
RedisSerializer<Object> jsonRedisSerializer = new GenericJackson2JsonRedisSerializer(objectMapper);
//设置key 的序列化方式
template.setKeySerializer(keyStringSerializer);
template.setHashKeySerializer(keyStringSerializer);
//设置值 的序列化方式
template.setValueSerializer(jsonRedisSerializer);
template.setHashValueSerializer(jsonRedisSerializer);
//设置默认的序列化方式
template.setDefaultSerializer(defalutSerializer);
template.afterPropertiesSet();
return template;
}
2、自自定义key 序列化 , redisKeyPrefix前缀兼容多项目隔离
@Slf4j
public class KeyStringSerializer implements RedisSerializer<String> {
private final Charset charset;
public KeyStringSerializer() {
this.charset = StandardCharsets.UTF_8;
}
@Autowired
private CustomRedisProperties redisProperties;
@Override
public byte[] serialize(String s) throws SerializationException {
String newValue = getKeyPrefix() + s;
return newValue.getBytes(charset);
}
@Override
public String deserialize(byte[] bytes) throws SerializationException {
String saveKey = new String(bytes, charset);
String keyPrefix = getKeyPrefix();
if (StrUtil.isNotBlank(keyPrefix)) {
int indexOf = saveKey.indexOf(keyPrefix);
if (indexOf > 0) {
log.info("key缺少前缀");
} else {
saveKey = saveKey.substring(indexOf);
}
log.info("saveKey:{}", saveKey);
}
return saveKey;
}
private String getKeyPrefix() {
return redisProperties.getRedisKeyPrefix();
}
二、redis操作封装
注入bean实例,方便引用
@Bean
public RedisService redisService(ObjectMapper objectMapper, RedisTemplate<String, Object> redisTemplate) {
return new DefaultRedisServiceImpl(objectMapper, redisTemplate);
}
RedisService 接口实现
package com.hxl.redis.load;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* @Author hxl
* @description
* @Date 2023-06-14 16:19
**/
public interface RedisService {
/**
* *公共基础操作*
**/
Boolean exists(String key);
Boolean expire(String key, long timeout);
Boolean expire(final String key, final long timeout, final TimeUnit unit);
int delete(String... keys);
Boolean delete(String key);
/**
* *基础类型(Integer、String、实体类等) 操作*
**/
Boolean set(String key, Object value);
Boolean set(String key, Object value, long expireTime);
Boolean set(String key, Object value, long expireTime, TimeUnit timeUnit);
String get(String key);
<T> T getObject(String key, Class<T> type);
<T> List<T> getObjectList(String key, Class<T> type);
/**
* *双向列表 list操作*
**/
Long setCacheList(String key, Object value);
Long setCacheList(String key, List<Object> values);
Long setLeftCacheList(String key, Object value);
Long setLeftCacheList(String key, List<Object> values);
<T> T rightPop(String key, Class<T> type);
<T> T leftPop(String key, Class<T> type);
<T> List<T> getCacheList(String key, Class<T> type);
Long size(String key);
/**
* *hash map操作*
*/
Boolean hPut(String key, String hashKey, Object value);
Boolean hPutAll(String key, Map<String, String> maps);
Boolean hExists(String key, String field);
Long hDelete(String key, Object... fields);
Map<Object, Object> hGetAll(String key);
Object hGet(String key, String field);
List<Object> hMultiGet(String key, Collection<Object> fields);
Long hSize(String key);
/**
* *set 集合 不重复*
*/
boolean addSet(String key, Object value);
boolean removeSet(String key, Object... value);
Set<Object> getAllSet(String key);
/**
* *set 有序集合 不重复*
*/
Boolean addZSet(String key, Object value, double score);
Long removeZSet(String key, Object... value);
/**
* ZSet数据增加分数
*
* @param key key
* @param value value
* @param delta 分数
* @return duuble
*/
Double incrZSet(String key, Object value, double delta);
/**
* 升序 区间查询
*
* @param key key
* @param min 最小分数
* @param max 最大分数
* @return Set<Object>
*/
Set<Object> zSetRangeByScore(String key, double min, double max);
/**
* 降序 区间查询
*
* @param key key
* @param min 最小分数
* @param max 最大分数
* @return Set<Object>
*/
Set<Object> zSetRangeDescByScore(String key, double min, double max);
}
具实现见:DefaultRedisServiceImpl
三、使用方法
1、pom 依赖
<dependency>
<groupId>com.hxl</groupId>
<artifactId>tower-boot-redis</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
2、yml 配置
edisKeyPrefix 项目前缀,会默认拼接key前缀
spring:
redis:
host: 127.0.0.1
port: 6379
database: 0
password:
timeout: 5000
lettuce:
pool:
max-active: 8
max-idle: 8
max-wait: -1
min-idle: 0
redisKeyPrefix: tower
github 项目地址:
https://github.com/sanxiaoshitou/tower-boot