import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* Redis工具类
*
* @author xuxiangbin
* @date 2021/9/24 16:00
*/
@Component
@SuppressWarnings("all")
public class RedisUtils {
@Autowired
RedisTemplate<String, Object> redisTemplate;
/**
* 指定缓存失效时间
*
* @param key 键
* @param time 时间(m)
* @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 键 不能为空
* @return 时间(m) 返回0代表永久有效
*/
public long getExpire(String key) {
Assert.notNull(key, "键值不能为空");
return redisTemplate.getExpire(key, TimeUnit.SECONDS);
}
/**
* 判断key是否存在
*
* @param key 键
* @return boolean true存在 false不存在
*/
public boolean hasKey(String key) {
try {
Assert.notNull(key, "键值不能为空");
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(CollectionUtils.arrayToList(key));
}
}
}
/**
* 普通缓存获取
*
* @param key 键
* @return 值
*/
public Object get(String key) {
return key == null ? null : redisTemplate.opsForValue().get(key);
}
/**
* 普通缓存放入
*
* @param key 键
* @param value 值
* @return boolean
*/
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 时间
* @return boolean
*/
public boolean set(String key, Object value, long time) {
try {
if (time > 0) {
redisTemplate.opsForValue().set(key, value, time);
} 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 键
* @param item 值
* @return object
*/
public Object hGet(String key, String item) {
Assert.notNull(key, "键不能为空");
Assert.notNull(item, "值不能为空");
return redisTemplate.opsForHash().get(key, item);
}
/**
* 获取hashkey对应的所有键值
*
* @param key 键
* @return Map<Object, Object>
*/
public Map<Object, Object> hmGet(String key) {
return redisTemplate.opsForHash().entries(key);
}
/**
* hash set
*
* @param key 鍵
* @param map 多个键值
* @return boolean
*/
public boolean hmSet(String key, Map<String, Object> map) {
try {
redisTemplate.opsForHash().putAll(key, map);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* hash set 并设置时间
*
* @param key 键
* @param map 多个键值
* @param time 时间(秒)
* @return boolean
*/
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 boolean
*/
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 时间(秒)
* @return boolean
*/
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 键 不能为空
* @param items 项 可以多个,不能为空
*/
public void hDel(String key, Object... items) {
Assert.notNull(key, "键不能为空");
Assert.notNull(items, "项不能为空");
redisTemplate.opsForHash().delete(key, items);
}
/**
* 判断hash表中是否有该项的值
*
* @param key 键 不能为空
* @param item 项 不能为空
* @return boolean
*/
public boolean hHasKey(String key, String item) {
Assert.notNull(key, "键不能为空");
Assert.notNull(item, "项不能为空");
return redisTemplate.opsForHash().hasKey(key, item);
}
/**
* hash递增,如果不存在,就会创建一个,并把新增后的值返回
*
* @param key jian
* @param item 项
* @param by 要增加几 (大于0)
* @return double
*/
public double hincr(String key, String item, double by) throws Exception {
if (by < 0) {
throw new Exception("增加值必须大于零");
}
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) throws Exception {
if (by < 0) {
throw new Exception("增加值必须大于零");
}
return redisTemplate.opsForHash().increment(key, item, -by);
}
/**
* 根据key获取set中的所有值
*
* @param key 键
* @return Set<Object>
*/
public Set<Object> sGet(String key) {
return redisTemplate.opsForSet().members(key);
}
/**
* 根据value从一个set中查询是否存在
*
* @param key 键
* @param value 值
* @return boolean
*/
public boolean sHashKey(String key, Object value) {
try {
return redisTemplate.opsForSet().isMember(key, value);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 将数据放入set缓存
*
* @param key 键
* @param values 值
* @return long
*/
public long sSet(String key, Object... values) {
try {
return redisTemplate.opsForSet().add(key, values);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* 将set放入缓存并设置时间
*
* @param key 键
* @param time 时间
* @param values 值
* @return long
*/
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;
}
}
/**
* 获取set缓存长度
*
* @param key 键
* @return long
*/
public long sGetSetSize(String key) {
try {
return redisTemplate.opsForSet().size(key);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* 移除值为value的
*
* @param key 键
* @param values 值
* @return long
*/
public long setRemove(String key, Object... values) {
try {
return redisTemplate.opsForSet().remove(key, values);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* 获取list缓存内容
*
* @param key 键
* @param start 开始
* @param end 结束
* @return List<Object>
*/
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.opsForSet().size(key);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* 通过索引获取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().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, List<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 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 long
*/
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;
}
}
}
Redis预定义方法
最新推荐文章于 2024-07-25 12:03:58 发布