Redis工具类
  TEZNKK3IfmPf 2023年11月14日 20 0

以下给出了两个 Redis 工具类,按自己的需求选择即可,基本上包含了日常开发中常用的Redis操作方法

/**
* @author BNTang
*/
@Slf4j
@Component
public class RedisUtils {

@Resource
private LettuceConnectionFactory factory;

@Resource
private StringRedisTemplate stringRedisTemplate;

@Bean
private StringRedisTemplate redisTemplate() {
// 关闭共享链接
factory.setShareNativeConnection(false);
StringRedisTemplate redisTemplate = new StringRedisTemplate();
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
redisTemplate.setConnectionFactory(factory);
return redisTemplate;
}

/**
* 不设置过期时长
*/
public final static long NOT_EXPIRE = -1;

/**
* 插入值-对象,指定数据库索引,指定过期时间
*
* @param key 键
* @param value 值
* @param dbIndex 数据库索引 范围 0-15
* @param expire 过期时间 单位:秒
*/
public void set(Integer dbIndex, String key, Object value, long expire) {
// 选择数据库
DefaultStringRedisConnection redisConnection = setDbIndex(dbIndex);
redisConnection.set(key, getBeanToJson(value));
if (expire != NOT_EXPIRE) {
redisConnection.expire(key, expire);
}
}

/**
* 插入值-对象
*
* @param dbIndex 数据库索引 范围 0-15
* @param key 键
* @param value 值
*/
public void set(Integer dbIndex, String key, Object value) {
set(dbIndex, key, value, NOT_EXPIRE);
}

/**
* 是否存在该key
*
* @param key key
* @return 数据库索引 范围 0-15
*/
public boolean isExist(Integer dbIndex, String key) {
String s = get(dbIndex, key);
return s != null;
}

/**
* 该key的有效时间
*
* @param dbIndex 数据库索引 范围 0-15
* @param key key
* @return 有效秒数 -1:该key没有设置有效时间 -2:该key有效时间过期
*/
public Long hasKey(Integer dbIndex, String key) {
DefaultStringRedisConnection redisConnection = setDbIndex(dbIndex);
Long aLong = redisConnection.pTtl(key, TimeUnit.SECONDS);
return aLong;
}

/**
* 获取值-对象,指定数据库索引,并设置过期时间
*
* @param dbIndex 数据库索引 范围 0-15
* @param key 键
* @param clazz 字节码对象
* @param expire 过期时间 单位:秒
* @return
*/
public <T> T get(Integer dbIndex, String key, Class<T> clazz, long expire) {
DefaultStringRedisConnection redisConnection = setDbIndex(dbIndex);
String value = redisConnection.get(key);
if (expire != NOT_EXPIRE) {
redisConnection.expire(key, expire);
}
return value == null ? null : getJsonToBean(value, clazz);
}

/**
* 取值-对象 指定数据库索引
*
* @param dbIndex 数据库索引 范围 0-15
* @param key 键
* @param clazz 字节码对象
* @return 获取对象
*/
public <T> T get(Integer dbIndex, String key, Class<T> clazz) {
return get(dbIndex, key, clazz, NOT_EXPIRE);
}

/**
* 获取值-字符串,指定数据库索引,设置过期时间
*
* @param dbIndex 数据库索引 范围 0-15
* @param key 键
* @param expire 过期时间 单位:秒
* @return 获取值
*/
public String get(Integer dbIndex, String key, long expire) {
DefaultStringRedisConnection redisConnection = setDbIndex(dbIndex);
String value = redisConnection.get(key);
if (expire != NOT_EXPIRE) {
redisConnection.expire(key, expire);
}
return value;
}

/**
* 取值-字符串,指定数据库索引
*
* @param dbIndex 数据库索引 范围 0-15
* @param key 键
* @return 获取字符串
*/
public String get(Integer dbIndex, String key) {
return get(dbIndex, key, NOT_EXPIRE);
}

/**
* 删除 指定数据库索引
*
* @param dbIndex 数据库索引 范围 0-15
* @param key 键
* @return 删除数量
*/
public Long delete(Integer dbIndex, String... key) {
DefaultStringRedisConnection redisConnection = setDbIndex(dbIndex);
return redisConnection.del(key);
}

/**
* 设置过期时间 ,指定数据库索引
*
* @param dbIndex 数据库索引 范围 0-15
* @param key 键
* @param expire 过期时间 单位:秒
*/
public void expire(Integer dbIndex, String key, int expire) {
DefaultStringRedisConnection redisConnection = setDbIndex(dbIndex);
if (expire != NOT_EXPIRE) {
redisConnection.expire(key, expire);
}
}

/**
* 批量添加 key (重复的键会覆盖)
*
* @param dbIndex 数据库索引 范围 0-15
* @param map
* @return 是否添加成功
*/
public Boolean batchSet(Integer dbIndex, Map<String, String> map) {
DefaultStringRedisConnection redisConnection = setDbIndex(dbIndex);
if (map.isEmpty()) {
return false;
}
Map<byte[], byte[]> rawKeys = new LinkedHashMap<>(map.size());
for (Map.Entry<String, String> entry : map.entrySet()) {
rawKeys.put(entry.getKey().getBytes(), entry.getValue().getBytes());
}
return redisConnection.mSet(rawKeys);
}

/**
* 对一个 key-value 的值进行加减操作,
* 如果该 key 不存在 将创建一个key 并赋值该 number
* 如果 key 存在,但 value 不是长整型 ,将报错
*
* @param dbIndex 数据库索引 范围 0-15
* @param key 键
* @param number 数
* @return 结果值
*/
public Long increment(Integer dbIndex, String key, long number) {
DefaultStringRedisConnection redisConnection = setDbIndex(dbIndex);
return redisConnection.incrBy(key, number);
}

/**
* 对一个 key-value 的值进行加减操作,
* 如果该 key 不存在 将创建一个key 并赋值该 number
* 如果 key 存在,但 value 不是 纯数字 ,将报错
*
* @param dbIndex 数据库索引 范围 0-15
* @param key 键
* @param number 数
* @return 结果值
*/
public Double increment(Integer dbIndex, String key, Double number) {
DefaultStringRedisConnection redisConnection = setDbIndex(dbIndex);
return redisConnection.incrBy(key, number);
}

/**
* 将数据放入set缓存
*
* @param dbIndex 数据库索引 范围 0-15
* @param key 键
* @return 放进几个
*/
public Long sSet(Integer dbIndex, String key, String... value) {
DefaultStringRedisConnection redisConnection = setDbIndex(dbIndex);
return redisConnection.sAdd(key, value);
}

/**
* 获取变量中的值
*
* @param dbIndex 数据库索引 范围 0-15
* @param key 键
* @return 值
*/
public Set<String> members(Integer dbIndex, String key) {
DefaultStringRedisConnection redisConnection = setDbIndex(dbIndex);
return redisConnection.sMembers(key);
}

/**
* 随机获取变量中指定个数的元素
*
* @param dbIndex 数据库索引 范围 0-15
* @param key 键
* @param count 值
* @return 元素
*/
public List<String> randomMembers(Integer dbIndex, String key, long count) {
DefaultStringRedisConnection redisConnection = setDbIndex(dbIndex);
return redisConnection.sRandMember(key, count);
}

/**
* 随机获取变量中指定个数的元素
*
* @param dbIndex 数据库索引 范围 0-15
* @param key 键
* @return 元素
*/
public String randomMembers(Integer dbIndex, String key) {
DefaultStringRedisConnection redisConnection = setDbIndex(dbIndex);
return redisConnection.sRandMember(key);
}

/**
* 弹出变量中的元素
*
* @param dbIndex 数据库索引 范围 0-15
* @param key 键
* @return 元素
*/
public String pop(Integer dbIndex, String key) {
DefaultStringRedisConnection redisConnection = setDbIndex(dbIndex);
return redisConnection.sPop(key);
}

/**
* 获取变量中值的长度
*
* @param dbIndex 数据库索引 范围 0-15
* @param key 键
* @return 长度
*/
public long size(Integer dbIndex, String key) {
DefaultStringRedisConnection redisConnection = setDbIndex(dbIndex);
return redisConnection.sCard(key);
}

/**
* 根据value从一个set中查询,是否存在
*
* @param dbIndex 数据库索引 范围 0-15
* @param key 键
* @param value 值
* @return true 存在 false不存在
*/
public boolean sHasKey(Integer dbIndex, String key, String value) {
DefaultStringRedisConnection redisConnection = setDbIndex(dbIndex);
return redisConnection.sIsMember(key, value);
}

/**
* 检查给定的元素是否在变量中。
*
* @param dbIndex 数据库索引 范围 0-15
* @param key 键
* @param value 元素对象
* @return 是否存在
*/
public boolean isMember(Integer dbIndex, String key, String value) {
DefaultStringRedisConnection redisConnection = setDbIndex(dbIndex);
return redisConnection.sIsMember(key, value);
}

/**
* 转移变量的元素值到目的变量。
*
* @param dbIndex 数据库索引 范围 0-15
* @param key 键
* @param value 元素对象
* @param destKey 元素对象
* @return 是否
*/
public boolean move(Integer dbIndex, String key, String value, String destKey) {
DefaultStringRedisConnection redisConnection = setDbIndex(dbIndex);
return redisConnection.sMove(key, value, destKey);
}

/**
* 批量移除set缓存中元素
*
* @param dbIndex 数据库索引 范围 0-15
* @param key 键
* @param values 值
* @return 批量删除数量
*/
public Long remove(Integer dbIndex, String key, String... values) {
DefaultStringRedisConnection redisConnection = setDbIndex(dbIndex);
return redisConnection.sRem(key, values);
}

/**
* 通过给定的key求2个set变量的差值
*
* @param dbIndex 数据库索引 范围 0-15
* @param key 键
* @param destKey 键
* @return String集合
*/
public Set<String> difference(Integer dbIndex, String key, String destKey) {
DefaultStringRedisConnection redisConnection = setDbIndex(dbIndex);
return redisConnection.sDiff(key, destKey);
}

//- - - - - - - - - - - - - - - - - - - - - hash类型 - - - - - - - - - - - - - - - - - - - -

/**
* 加入缓存
*
* @param dbIndex 数据库索引 范围 0-15
* @param key 键
* @param map 键
*/
public void add(Integer dbIndex, String key, Map<String, String> map) {
DefaultStringRedisConnection redisConnection = setDbIndex(dbIndex);
if (map.isEmpty()) {
return;
}
byte[] bytes = key.getBytes();
Map<byte[], byte[]> hashes = new LinkedHashMap<>(map.size());
for (Map.Entry<String, String> entry : map.entrySet()) {
hashes.put(entry.getKey().getBytes(), entry.getValue().getBytes());
}
redisConnection.hMSet(bytes, hashes);
}

/**
* 获取 key 下的 所有 hashkey 和 value
*
* @param dbIndex 数据库索引 范围 0-15
* @param key 键
* @return map
*/
public Map<String, String> getHashEntries(Integer dbIndex, String key) {
DefaultStringRedisConnection redisConnection = setDbIndex(dbIndex);
return redisConnection.hGetAll(key);
}

/**
* 验证指定 key 下 有没有指定的 hashkey
*
* @param dbIndex 数据库索引 范围 0-15
* @param key 键
* @param hashKey 值
* @return 是否
*/
public boolean hashKey(Integer dbIndex, String key, String hashKey) {
DefaultStringRedisConnection redisConnection = setDbIndex(dbIndex);
return redisConnection.hExists(key, hashKey);
}

/**
* 获取指定key的值string
*
* @param dbIndex 数据库索引 范围 0-15
* @param key 键
* @param key2 键
* @return 字符串
*/
public String getMapString(Integer dbIndex, String key, String key2) {
DefaultStringRedisConnection redisConnection = setDbIndex(dbIndex);
return redisConnection.hGet(key, key2);
}

/**
* 获取指定的值Int
*
* @param dbIndex 数据库索引 范围 0-15
* @param key 键
* @param key2 键
* @return int
*/
public Integer getMapInt(Integer dbIndex, String key, String key2) {
DefaultStringRedisConnection redisConnection = setDbIndex(dbIndex);
String s = redisConnection.hGet(key, key2);
return Integer.valueOf(s);
}

/**
* 弹出元素并删除
*
* @param dbIndex 数据库索引 范围 0-15
* @param key 键
* @return 字符串
*/
public String popValue(Integer dbIndex, String key) {
DefaultStringRedisConnection redisConnection = setDbIndex(dbIndex);
return redisConnection.sPop(key);
}

/**
* 删除指定 hash 的 HashKey
*
* @param dbIndex 数据库索引 范围 0-15
* @param key 键
* @param hashKeys 值
* @return 删除成功的 数量
*/
public Long delete(Integer dbIndex, String key, String... hashKeys) {
DefaultStringRedisConnection redisConnection = setDbIndex(dbIndex);
return redisConnection.hDel(key, hashKeys);
}

/**
* 删除散列中指定的key
*
* @param dbIndex 数据库索引 范围 0-15
* @param key 键
* @return 删除成功的 数量
*/
public Boolean del(Integer dbIndex, String key) {
DefaultStringRedisConnection redisConnection = setDbIndex(dbIndex);
Long result = redisConnection.del(key.getBytes());
return result != null && result.intValue() == 1;
}

/**
* 给指定 hash 的 hashkey 做增减操作
*
* @param dbIndex 数据库索引 范围 0-15
* @param key 键
* @param hashKey 值
* @param number 数量
* @return 增减
*/
public Long increment(Integer dbIndex, String key, String hashKey, long number) {
DefaultStringRedisConnection redisConnection = setDbIndex(dbIndex);
return redisConnection.hIncrBy(key, hashKey, number);
}

/**
* 给指定 hash 的 hashkey 做增减操作
*
* @param dbIndex 数据库索引 范围 0-15
* @param key 键
* @param hashKey 值
* @param number 数
* @return 增减
*/
public Double increment(Integer dbIndex, String key, String hashKey, Double number) {
DefaultStringRedisConnection redisConnection = setDbIndex(dbIndex);
return redisConnection.hIncrBy(key, hashKey, number);
}

/**
* 获取 key 下的 所有 hashkey 字段
*
* @param dbIndex 数据库索引 范围 0-15
* @param key 键
* @return 字段
*/
public Set<String> hashKeys(Integer dbIndex, String key) {
DefaultStringRedisConnection redisConnection = setDbIndex(dbIndex);
return redisConnection.hKeys(key);
}

/**
* 获取指定 hash 下面的 键值对 数量
*
* @param dbIndex 数据库索引 范围 0-15
* @param key 键
* @return 数量
*/
public Long hashSize(Integer dbIndex, String key) {
DefaultStringRedisConnection redisConnection = setDbIndex(dbIndex);
return redisConnection.hLen(key);
}

//- - - - - - - - - - - - - - - - - - - - - list类型 - - - - - - - - - - - - - - - - - - - -

/**
* 在变量左边添加元素值
*
* @param dbIndex 数据库索引 范围 0-15
* @param key 键
* @param value 值
*/
public void leftPush(Integer dbIndex, String key, String... value) {
DefaultStringRedisConnection redisConnection = setDbIndex(dbIndex);
redisConnection.lPush(key, value);
}

/**
* 获取集合指定位置的值。
*
* @param dbIndex 数据库索引 范围 0-15
* @param key 键
* @param index 索引
* @return 字符串
*/
public String index(Integer dbIndex, String key, long index) {
DefaultStringRedisConnection redisConnection = setDbIndex(dbIndex);
return redisConnection.lIndex(key, index);
}

/**
* 获取指定区间的值。
*
* @param dbIndex 数据库索引 范围 0-15
* @param key 键
* @param start 开始
* @param end 结束
* @return String集合
*/
public List<String> range(Integer dbIndex, String key, long start, long end) {
DefaultStringRedisConnection redisConnection = setDbIndex(dbIndex);
List<String> strings = redisConnection.lRange(key, start, end);
redisConnection.close();
return strings;
}

/**
* 把最后一个参数值放到指定集合的第一个出现中间参数的前面,
* 如果中间参数值存在的话。
*
* @param key 键
* @param pivot 第一个出现参数
* @param value 值
*/
public void leftPush(Integer dbIndex, String key, String pivot, String value) {
DefaultStringRedisConnection redisConnection = setDbIndex(dbIndex);
redisConnection.lInsert(key, RedisListCommands.Position.BEFORE, pivot, value);
}

/**
* 向左边批量添加参数元素。
*
* @param dbIndex 数据库索引 范围 0-15
* @param key 键
* @param values 值
*/
public void leftPushAll(Integer dbIndex, String key, String... values) {
DefaultStringRedisConnection redisConnection = setDbIndex(dbIndex);
redisConnection.lPush(key, values);
}

/**
* 向集合最右边添加元素。
*
* @param dbIndex 数据库索引 范围 0-15
* @param key 键
* @param value 值
*/
public void rightPushAll(Integer dbIndex, String key, String value) {
DefaultStringRedisConnection redisConnection = setDbIndex(dbIndex);
redisConnection.rPush(key, value);
}

/**
* 向右边批量添加参数元素。
*
* @param dbIndex 数据库索引 范围 0-15
* @param key 键
* @param values 值
*/
public void rightPushAll(Integer dbIndex, String key, String... values) {
DefaultStringRedisConnection redisConnection = setDbIndex(dbIndex);
redisConnection.rPush(key, values);
redisConnection.close();
}

/**
* 向已存在的集合中添加元素。
*
* @param dbIndex 数据库索引 范围 0-15
* @param key 键
* @param value 值
*/
public void rightPushIfPresent(Integer dbIndex, String key, String value) {
DefaultStringRedisConnection redisConnection = setDbIndex(dbIndex);
redisConnection.rPushX(key, value);
}

/**
* 向已存在的集合中添加元素。
*
* @param dbIndex 数据库索引 范围 0-15
* @param key 键
* @return 元素个数
*/
public long listLength(Integer dbIndex, String key) {
DefaultStringRedisConnection redisConnection = setDbIndex(dbIndex);
return redisConnection.lLen(key);
}

/**
* 移除集合中的左边第一个元素。
*
* @param dbIndex 数据库索引 范围 0-15
* @param key 键
*/
public void leftPop(Integer dbIndex, String key) {
DefaultStringRedisConnection redisConnection = setDbIndex(dbIndex);
redisConnection.lPop(key);
}

/**
* 移除集合中右边的元素。
*
* @param dbIndex 数据库索引 范围 0-15
* @param key 键
*/
public void rightPop(Integer dbIndex, String key) {
DefaultStringRedisConnection redisConnection = setDbIndex(dbIndex);
redisConnection.rPop(key);
}

/**
* 根据key 获取值
*
* @param dbIndex 数据库索引 范围 0-15
* @param key 键
* @return 字符串
*/
public String getValueByKey(Integer dbIndex, String key) {
DefaultStringRedisConnection redisConnection = setDbIndex(dbIndex);
return redisConnection.get(key);
}

/**
* Object转成JSON数据
*
* @param object 对象
* @return json字符串
*/
public static String getBeanToJson(Object object) {
try {
return JSON.toJSONString(object);
} catch (Exception e) {
log.info("getBeanToJson is error{ }", e);
return null;
}
}

/**
* JSON数据,转成Object
*
* @param jsonData json字符串
* @param clazz 类型
* @param <T> 泛型
* @return 泛型对象
*/
public static <T> T getJsonToBean(String jsonData, Class<T> clazz) {
try {
return JSON.parseObject(jsonData, clazz);
} catch (Exception e) {
log.info("getJsonToBean is error{ }", e);
return null;
}
}

/**
* 设置数据库索引
*
* @param dbIndex 数据库索引 范围 0-15
* @return DefaultStringRedisConnection
*/
private DefaultStringRedisConnection setDbIndex(Integer dbIndex) {
if (dbIndex == null || dbIndex > 15 || dbIndex < 0) {
dbIndex = 0;
}
RedisConnection redisConnection = stringRedisTemplate.getConnectionFactory().getConnection();
DefaultStringRedisConnection stringRedisConnection = new DefaultStringRedisConnection(redisConnection);
stringRedisConnection.select(dbIndex);

return stringRedisConnection;
}
}

/**
* @author BNTang
*/
public class RedisUtils {

@Autowired
private RedisTemplate<String, Object> redisTemplate;

/**
* 指定缓存失效时间
*
* @param key 键
* @param time 时间(秒)
* @return Boolean
*/
public Boolean expire(String key, Long time) {
try {
if (time > 0) {
redisTemplate.expire(key, time, TimeUnit.SECONDS);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

/**
* 根据key获取过期时间
*
* @param key 键 不能为 null
* @return 时间(秒) 返回 0代表为永久有效
*/
public Long getExpire(String key) {
return redisTemplate.getExpire(key, TimeUnit.SECONDS);
}

/**
* 判断 key是否存在
*
* @param key 键
* @return true 存在 false不存在
*/
public Boolean hasKey(String key) {
try {
return redisTemplate.hasKey(key);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

/**
* 删除缓存
*
* @param key 可以传一个值 或多个
*/
public void del(String... key) {
if (key != null && key.length > 0) {
if (key.length == 1) {
redisTemplate.delete(key[0]);
} else {
redisTemplate.delete(Arrays.asList(key));
}
}
}

/**
* 普通缓存获取
*
* @param key 键
* @return 值
*/
public Object get(String key) {
return key == null ? null : redisTemplate.opsForValue().get(key);
}

/**
* 普通缓存放入
*
* @param key 键
* @param value 值
* @return true成功 false失败
*/
public Boolean set(String key, Object value) {
try {
redisTemplate.opsForValue().set(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

/**
* 普通缓存放入并设置时间
*
* @param key 键
* @param value 值
* @param time 时间(秒) time要大于0 如果time小于等于0 将设置无限期
* @return true成功 false 失败
*/
public Boolean set(String key, Object value, Long time) {
try {
if (time > 0) {
redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
} else {
set(key, value);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

/**
* 递增
*
* @param key 键
* @param delta 要增加几(大于0)
* @return Long
*/
public Long incr(String key, Long delta) {
if (delta < 0) {
throw new RuntimeException("递增因子必须大于0");
}
return redisTemplate.opsForValue().increment(key, delta);
}

/**
* 递减
*
* @param key 键
* @param delta 要减少几(小于0)
* @return Long
*/
public Long decr(String key, Long delta) {
if (delta < 0) {
throw new RuntimeException("递减因子必须大于0");
}
return redisTemplate.opsForValue().increment(key, -delta);
}

/**
* HashGet
*
* @param key 键 不能为 null
* @param item 项 不能为 null
* @return 值
*/
public Object hget(String key, String item) {
return redisTemplate.opsForHash().get(key, item);
}

/**
* 获取 hashKey对应的所有键值
*
* @param key 键
* @return 对应的多个键值
*/
public Map<Object, Object> hmget(String key) {
return redisTemplate.opsForHash().entries(key);
}

/**
* HashSet
*
* @param key 键
* @param map 对应多个键值
* @return true 成功 false 失败
*/
public Boolean hmset(String key, Map<String, Object> map) {
try {
redisTemplate.opsForHash().putAll(key, map);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

/**
* HashSet 并设置时间
*
* @param key 键
* @param map 对应多个键值
* @param time 时间(秒)
* @return true成功 false失败
*/
public Boolean hmset(String key, Map<String, Object> map, Long time) {
try {
redisTemplate.opsForHash().putAll(key, map);
if (time > 0) {
expire(key, time);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

/**
* 向一张hash表中放入数据,如果不存在将创建
*
* @param key 键
* @param item 项
* @param value 值
* @return true 成功 false失败
*/
public Boolean hset(String key, String item, Object value) {
try {
redisTemplate.opsForHash().put(key, item, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

/**
* 向一张hash表中放入数据,如果不存在将创建
*
* @param key 键
* @param item 项
* @param value 值
* @param time 时间(秒) 注意:如果已存在的hash表有时间,这里将会替换原有的时间
* @return true 成功 false失败
*/
public Boolean hset(String key, String item, Object value, Long time) {
try {
redisTemplate.opsForHash().put(key, item, value);
if (time > 0) {
expire(key, time);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

/**
* 删除hash表中的值
*
* @param key 键 不能为 null
* @param item 项 可以使多个不能为 null
*/
public void hdel(String key, Object... item) {
redisTemplate.opsForHash().delete(key, item);
}

/**
* 判断hash表中是否有该项的值
*
* @param key 键 不能为 null
* @param item 项 不能为 null
* @return true 存在 false不存在
*/
public Boolean hHasKey(String key, String item) {
return redisTemplate.opsForHash().hasKey(key, item);
}

/**
* hash递增 如果不存在,就会创建一个 并把新增后的值返回
*
* @param key 键
* @param item 项
* @param by 要增加几(大于0)
* @return Double
*/
public Double hincr(String key, String item, Double by) {
return redisTemplate.opsForHash().increment(key, item, by);
}

/**
* hash递减
*
* @param key 键
* @param item 项
* @param by 要减少记(小于0)
* @return Double
*/
public Double hdecr(String key, String item, Double by) {
return redisTemplate.opsForHash().increment(key, item, -by);
}

/**
* 根据 key获取 Set中的所有值
*
* @param key 键
* @return Set
*/
public Set<Object> sGet(String key) {
try {
return redisTemplate.opsForSet().members(key);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

/**
* 根据value从一个set中查询,是否存在
*
* @param key 键
* @param value 值
* @return true 存在 false不存在
*/
public Boolean sHasKey(String key, Object value) {
try {
return redisTemplate.opsForSet().isMember(key, value);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

/**
* 将数据放入set缓存
*
* @param key 键
* @param values 值 可以是多个
* @return 成功个数
*/
public Long sSet(String key, Object... values) {
try {
return redisTemplate.opsForSet().add(key, values);
} catch (Exception e) {
e.printStackTrace();
return 0L;
}
}

/**
* 将set数据放入缓存
*
* @param key 键
* @param time 时间(秒)
* @param values 值 可以是多个
* @return 成功个数
*/
public Long sSetAndTime(String key, Long time, Object... values) {
try {
Long count = redisTemplate.opsForSet().add(key, values);
if (time > 0)
expire(key, time);
return count;
} catch (Exception e) {
e.printStackTrace();
return 0L;
}
}

/**
* 获取set缓存的长度
*
* @param key 键
* @return Long
*/
public Long sGetSetSize(String key) {
try {
return redisTemplate.opsForSet().size(key);
} catch (Exception e) {
e.printStackTrace();
return 0L;
}
}

/**
* 移除值为value的
*
* @param key 键
* @param values 值 可以是多个
* @return 移除的个数
*/
public Long setRemove(String key, Object... values) {
try {
return redisTemplate.opsForSet().remove(key, values);
} catch (Exception e) {
e.printStackTrace();
return 0L;
}
}

/**
* 获取list缓存的内容
*
* @param key 键
* @param start 开始
* @param end 结束 0 到 -1代表所有值
* @return List
*/
public List<Object> lGet(String key, Long start, Long end) {
try {
return redisTemplate.opsForList().range(key, start, end);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

/**
* 获取list缓存的长度
*
* @param key 键
* @return Long
*/
public Long lGetListSize(String key) {
try {
return redisTemplate.opsForList().size(key);
} catch (Exception e) {
e.printStackTrace();
return 0L;
}
}

/**
* 通过索引 获取list中的值
*
* @param key 键
* @param index 索引 index>=0时, 0 表头,1 第二个元素,依次类推;
* index<0时,-1,表尾,-2倒数第二个元素,依次类推
* @return Object
*/
public Object lGetIndex(String key, Long index) {
try {
return redisTemplate.opsForList().index(key, index);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

/**
* 将list放入缓存
*
* @param key 键
* @param value 值
* @return Boolean
*/
public Boolean lSet(String key, Object value) {
try {
redisTemplate.opsForList().rightPush(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

/**
* 将list放入缓存
*
* @param key 键
* @param value 值
* @param time 时间(秒)
* @return Boolean
*/
public Boolean lSet(String key, Object value, Long time) {
try {
redisTemplate.opsForList().rightPush(key, value);
if (time > 0)
expire(key, time);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

/**
* 将list放入缓存
*
* @param key 键
* @param value 值
* @return Boolean
*/
public Boolean lSet(String key, List<Object> value) {
try {
redisTemplate.opsForList().rightPushAll(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

/**
* 将list放入缓存
*
* @param key 键
* @param value 值
* @param time 时间(秒)
* @return Boolean
*/
public Boolean lSet(String key, List<Object> value, Long time) {
try {
redisTemplate.opsForList().rightPushAll(key, value);
if (time > 0)
expire(key, time);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

/**
* 根据索引修改list中的某条数据
*
* @param key 键
* @param index 索引
* @param value 值
* @return Boolean
*/
public Boolean lUpdateIndex(String key, Long index, Object value) {
try {
redisTemplate.opsForList().set(key, index, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

/**
* 移除N个值为value
*
* @param key 键
* @param count 移除多少个
* @param value 值
* @return 移除的个数
*/
public Long lRemove(String key, Long count, Object value) {
try {
return redisTemplate.opsForList().remove(key, count, value);
} catch (Exception e) {
e.printStackTrace();
return 0L;
}
}
}

此外,我们需要一个泛型为<String, Object>形式的RedisTemplate,并且设置这个RedisTemplate在往Redis里写数据时key及value的序列化方式(默认使用的JdkSerializationRedisSerializer这样的会导致我们通过Redis Desktop Manager显示的我们key跟value的时候为不可读字符串)。

public class LettuceRedisConfigure {

@Bean
@ConditionalOnClass(RedisOperations.class)
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(factory);

Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(mapper);

StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();

// key采用 String的序列化方式
template.setKeySerializer(stringRedisSerializer);

// hash的 key也采用 String的序列化方式
template.setHashKeySerializer(stringRedisSerializer);

// value序列化方式采用 jackson
template.setValueSerializer(jackson2JsonRedisSerializer);

// hash的 value序列化方式采用 jackson
template.setHashValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();

return template;
}

@Bean
@ConditionalOnBean(name = "redisTemplate")
public RedisUtils redisService() {
return new RedisUtils();
}
}
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月14日 0

暂无评论

推荐阅读
  TEZNKK3IfmPf   2024年05月31日   29   0   0 redis用户
  TEZNKK3IfmPf   2024年05月31日   30   0   0 dataredis
  TEZNKK3IfmPf   2024年05月31日   27   0   0 sqlite数据库
  TEZNKK3IfmPf   2024年05月31日   27   0   0 awkredis
  TEZNKK3IfmPf   2024年05月31日   31   0   0 数据库mysql
  TEZNKK3IfmPf   2024年05月31日   27   0   0 数据库mysql
TEZNKK3IfmPf