Spring Boot 整合 Redis 集群详解
package com.zt.zteam.main.utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* @ClassName: RedisUtils
* @Author: zhangyong
* @Date: 2024/4/1 14:56
* @Description: redis 工具类
*/
@Component
@Slf4j
public class RedisUtils {
@Autowired
public RedisTemplate redisTemplate;
/**
* @Description: 设置缓存对象
* @Date: 2024/4/2 14:18
*/
public <T> void set(String key, T value) {
redisTemplate.opsForValue().set(key, value);
}
/**
* @Description: 设置缓存对象,附带设定有效期
* @Date: 2024/4/2 14:18
*/
public <T> void set(String key, T value, Integer timeout, TimeUnit timeUnit) {
redisTemplate.opsForValue().set(key, value, timeout, timeUnit);
}
/**
* @Description: 设置缓存对象的有效期
* @Date: 2024/4/2 14:18
*/
public <T> void expire(String key, Integer timeout, TimeUnit timeUnit) {
redisTemplate.expire(key, timeout, timeUnit);
}
/**
* @Description: 获取缓存对象
* @Date: 2024/4/2 14:18
*/
public <T> T get(String key) {
ValueOperations<String, T> operation = redisTemplate.opsForValue();
return operation.get(key);
}
/**
* @Description: 删除缓存对象
* @Date: 2024/4/2 14:18
*/
public boolean remove(String key) {
return redisTemplate.delete(key);
}
/**
* @Description: 从redis缓存中移除指定前缀的所有值
* @Date: 2024/4/2 14:18
*/
public void removePrefix(String prefix) {
Set keys = redisTemplate.keys(prefix + "*");
redisTemplate.delete(keys);
}
/**
* @Description: 伪批量存入缓存
* @Date: 2024/4/2 14:18
*/
public void setBatch(Map<String, String> cachedMap) {
for (String key : cachedMap.keySet()) {
set(key, cachedMap.get(key));
}
}
/**
* @Description: key 是否存在
* @Date: 2024/4/2 14:18
*/
public boolean exists(String key) {
return redisTemplate.hasKey(key);
}
/**
* @Description: 根据key 删除 value
* @Date: 2024/4/2 14:18
*/
public boolean del(String key) {
return redisTemplate.delete(key);
}
/**
* @Description: 判断key是否存在
* @Date: 2024/4/2 14:18
*/
public boolean hasKey(String key) {
try {
return redisTemplate.hasKey(key);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* @Description: 指定缓存失效时间
* @Date: 2024/4/13 8:53
*/
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;
}
}
/**
* @Description: 获取key 的过期时间
* @Date: 2024/4/13 8:54
*/
public long getExpire(String key) {
return redisTemplate.getExpire(key, TimeUnit.SECONDS);
}
/**
* @Description: 删除缓存 支持一个到多个
* @Date: 2024/4/13 8:55
*/
public void del(String... key) {
if (key != null && key.length > 0) {
if (key.length == 1) {
redisTemplate.delete(key[0]);
} else {
redisTemplate.delete(CollectionUtils.arrayToList(key));
}
}
}
/**
* @Description: 增加操作
* @Date: 2024/4/13 8:55
*/
public long incr(String key, long count) {
if (count < 0) {
throw new RuntimeException("递增因子必须大于0");
}
return redisTemplate.opsForValue().increment(key, count);
}
/**
* @Description: 减少操作
* @Date: 2024/4/13 8:55
*/
public long decr(String key, long count) {
if (count < 0) {
throw new RuntimeException("递减因子必须大于0");
}
return redisTemplate.opsForValue().increment(key, -count);
}
/**
* @Description: hash get 操作
* @Date: 2024/4/13 8:55
*/
public Object hGet(String key, String item) {
return redisTemplate.opsForHash().get(key, item);
}
/**
* @Description: hash mget 操作 获取 key 对应的所有键值
* @Date: 2024/4/13 8:55
*/
public Map<Object, Object> hmGet(String key) {
return redisTemplate.opsForHash().entries(key);
}
/**
* @Description: hash set 操作 批量set
* @Date: 2024/4/13 8:55
*/
public boolean hmSet(String key, Map<String, Object> map) {
try {
redisTemplate.opsForHash().putAll(key, map);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* @Description: hash set 操作 带过期时间的批量set
* @Date: 2024/4/13 8:55
*/
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;
}
}
/**
* @Description: hash set 操作 单个set
* @Date: 2024/4/13 8:55
*/
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;
}
}
/**
* @Description: hash set 操作 单个set 带过期时间
* @Date: 2024/4/13 8:55
*/
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;
}
}
/**
* @Description: hash 删除操作 支持当个或多个
* @Date: 2024/4/13 8:55
*/
public void hDel(String key, Object... item) {
redisTemplate.opsForHash().delete(key, item);
}
/**
* @Description: hash 判断 key 是否存在
* @Date: 2024/4/13 8:55
*/
public boolean hHasKey(String key, String item) {
return redisTemplate.opsForHash().hasKey(key, item);
}
/**
* @Description: hash 递增操作
* @Date: 2024/4/13 8:55
*/
public double hincr(String key, String item, double by) {
return redisTemplate.opsForHash().increment(key, item, by);
}
/**
* @Description: hash 递减操作
* @Date: 2024/4/13 8:55
*/
public double hdecr(String key, String item, double by) {
return redisTemplate.opsForHash().increment(key, item, -by);
}
/**
* @Description: set 根据key 获取value 值
* @Date: 2024/4/13 8:55
*/
public Set<Object> sGet(String key) {
try {
return redisTemplate.opsForSet().members(key);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* @Description: set 判断value是否存在set集合中
* @Date: 2024/4/13 8:55
*/
public boolean sHasValue(String key, Object value) {
try {
return redisTemplate.opsForSet().isMember(key, value);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* @Description: set 添加元素操作 value 可以是一个或多个
* @Date: 2024/4/13 8:55
*/
public long sSet(String key, Object... values) {
try {
return redisTemplate.opsForSet().add(key, values);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* @Description: set 添加元素操作 value 可以是一个或多个 带缓存时间
* @Date: 2024/4/13 8:55
*/
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 0;
}
}
/**
* @Description: 获取 set 集合的元素个数
* @Date: 2024/4/13 8:55
*/
public long sGetSize(String key) {
try {
return redisTemplate.opsForSet().size(key);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* @Description: 删除 set 集合中元素 支持一个或者多个 返回删除的个数
* @Date: 2024/4/13 8:55
*/
public long setDel(String key, Object... values) {
try {
Long count = redisTemplate.opsForSet().remove(key, values);
return count;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* @Description: 获取 list 缓存的内容 从start 位置到 end 位置
* @Date: 2024/4/13 8:55
*/
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;
}
}
/**
* @Description: 获取 list 缓存的元素个数
* @Date: 2024/4/13 8:55
*/
public long lGetSize(String key) {
try {
return redisTemplate.opsForList().size(key);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* @Description: 通过索引获取list 集合中的元素 类似 list(0)
* @Date: 2024/4/13 8:55
*/
public Object lGetIndex(String key, long index) {
try {
return redisTemplate.opsForList().index(key, index);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* @Description: 像list 集合中添加元素
* @Date: 2024/4/13 8:55
*/
public boolean lSet(String key, Object value) {
try {
redisTemplate.opsForList().rightPush(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* @Description: 像list 集合中添加元素 带缓存时间
* @Date: 2024/4/13 8:55
*/
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;
}
}
/**
* @Description: 像list 集合中添加元素 批量操作
* @Date: 2024/4/13 8:55
*/
public boolean lBatchSet(String key, List<Object> value) {
try {
redisTemplate.opsForList().rightPushAll(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* @Description: 像list 集合中添加元素 批量操作 带过期时间
* @Date: 2024/4/13 8:55
*/
public boolean lBatchSet(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;
}
}
/**
* @Description: 根据索引修改 list 中的某个元素
* @Date: 2024/4/13 8:55
*/
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;
}
}
/**
* @Description: 删除 count 个值为value 的元素 返回删除的个数
* @Date: 2024/4/13 8:55
*/
public long lRemove(String key, long count, Object value) {
try {
Long remove = redisTemplate.opsForList().remove(key, count, value);
return remove;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
}