| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- package top.lvzhiqiang.util;
- import org.springframework.data.redis.core.RedisTemplate;
- import java.util.List;
- import java.util.Set;
- import java.util.concurrent.TimeUnit;
- /**
- * Redis工具类
- *
- * @author 施勇
- * 2018年11月27日 下午9:26:11
- */
- public class RedisUtils {
- private RedisTemplate<String, Object> redisTemplate;
- public RedisTemplate<String, Object> getRedisTemplate() {
- return redisTemplate;
- }
- public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
- this.redisTemplate = redisTemplate;
- }
- /**
- * 缓存对象信息
- *
- * @param key 键
- * @param value 值
- * @author 施勇
- * 2018年12月11日 上午10:54:49
- */
- public void set(String key, Object value) {
- redisTemplate.opsForValue().set(key, value);
- }
- /**
- * 缓存信息,并设置缓存时间
- *
- * @param key 键
- * @param value 值
- * @param time 时间
- * @param timeUnit 时间单位
- * @author 施勇
- * 2018年12月11日 上午10:54:27
- */
- public void set(String key, Object value, long time, TimeUnit timeUnit) {
- if (time > 0) {
- redisTemplate.opsForValue().set(key, value, time, timeUnit);
- } else {
- set(key, value);
- }
- }
- /**
- * 原子递增1
- *
- * @param key 键
- * @return java.lang.Long
- */
- public Long increment(String key) {
- return redisTemplate.opsForValue().increment(key);
- }
- /**
- * 原子递增
- *
- * @param key 键
- * @param delta 增量
- * @return java.lang.Long
- */
- public Long increment(String key, Long delta) {
- if (delta == null || delta < 1) {
- delta = 1L;
- }
- return redisTemplate.opsForValue().increment(key, delta);
- }
- /**
- * 递增
- *
- * @param key 键
- * @param delta 递增因子
- * @param time 时间
- * @param timeUnit 时间单位
- * @return java.lang.Long
- * @author shiyong
- * 2022/10/18 14:58
- */
- public Long increment(String key, Long delta, long time, TimeUnit timeUnit) {
- if (null == delta || delta < 1) {
- delta = 1L;
- }
- Long increment = redisTemplate.opsForValue().increment(key, delta);
- redisTemplate.expire(key, time, timeUnit);
- return increment;
- }
- /**
- * 从缓存中获取信息
- *
- * @param key 键
- * @return Object
- * @author 施勇
- * 2018年12月11日 上午10:54:10
- */
- public Object get(Object key) {
- return key == null ? "" : redisTemplate.opsForValue().get(key);
- }
- /**
- * 从redis中获取key对应的过期时间;
- * 如果该值有过期时间,就返回相应的过期时间;
- * 如果该值没有设置过期时间,就返回-1;
- * 如果没有该值,就返回-2;
- */
- public Long getExpire(String key) {
- return redisTemplate.opsForValue().getOperations().getExpire(key);
- }
- /**
- * 设置key的失效时间
- *
- * @param key 键
- * @param time 时长
- * @param timeUnit 时间单位
- * @return java.lang.Boolean
- */
- public Boolean expire(String key, long time, TimeUnit timeUnit) {
- return redisTemplate.expire(key, time, timeUnit);
- }
- /**
- * 是否存在key
- *
- * @param key 键
- * @return boolean
- * @author 施勇
- * 2018年12月11日 上午10:53:54
- */
- public boolean hasKey(String key) {
- if (null == key || "".equals(key)) {
- return false;
- }
- Boolean result = redisTemplate.hasKey(key);
- if (null == result) {
- return false;
- } else {
- return result;
- }
- }
- /**
- * 删除KEY对应的对象
- *
- * @param key 键
- * @author 施勇
- * 2018年12月24日 下午4:14:26
- */
- public void delete(String key) {
- redisTemplate.delete(key);
- }
- /**
- * 查找匹配的key
- *
- * @param pattern 匹配字符串
- * @return java.util.Set<java.lang.String>
- * @author shiyong
- * 2020/1/3 13:42
- */
- public Set<String> getKeys(String pattern) {
- return redisTemplate.keys(pattern);
- }
- /**
- * 在队尾追加元素
- *
- * @param key 键
- * @param value 值
- * @return 添加后队列的长度
- */
- public Long rightPush(String key, Object value) {
- return redisTemplate.opsForList().rightPush(key, value);
- }
- /**
- * 从队首移除元素
- *
- * @param key 键
- * @return 移除的元素
- */
- public Object leftPop(String key) {
- return redisTemplate.opsForList().leftPop(key);
- }
- /**
- * 从队首开始取值,获取指定区间
- *
- * @param key 键
- * @param start 开始下标
- * @param stop 结束下标
- * @return 区间内的全部值
- */
- public List<?> leftRange(String key, long start, long stop) {
- return redisTemplate.opsForList().range(key, start, stop);
- }
- /**
- * 将值加到集合中
- *
- * @param key 键
- * @param values 值
- * @return 添加是否成功(1:成功,0:失败)
- */
- public Long addToSet(String key, Object... values) {
- return redisTemplate.opsForSet().add(key, values);
- }
- /**
- * 集合的数量
- *
- * @param key 键
- * @return 集合数量
- */
- public Long sizeOfSet(String key) {
- return redisTemplate.opsForSet().size(key);
- }
- /**
- * 集合列表
- *
- * @param key 键
- * @return 集合列表
- */
- public Set<Object> getOfSet(String key) {
- return redisTemplate.opsForSet().members(key);
- }
- /**
- * 集合中是否有该值
- *
- * @param key 键
- * @param value 值
- * @return Boolean
- */
- public Boolean hasOfSet(String key, Object value) {
- return redisTemplate.opsForSet().isMember(key, value);
- }
- /**
- * 删除集合中值
- *
- * @param key 键
- * @param value 值
- * @return 删除是否成功(1:成功,0:失败)
- */
- public Long removeOfSet(String key, Object value) {
- return redisTemplate.opsForSet().remove(key, value);
- }
- }
|