微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

比较全面的redis工具类

比较全面的redis工具类

@Component
public class RedisUtils {

    private static  Redistemplate<String, Object> redistemplate;

    public RedisUtils(Redistemplate<String, Object> redistemplate) {
        RedisUtils.redistemplate = redistemplate;
    }

    /**
     * 判断缓存是否存在
     * @param key 缓存key
     * @return 缓存是否存在
     */
    public static Boolean hasKey(String key){
        return redistemplate.hasKey(key);
    }

    /**
     * 缓存数据
     * @param key 缓存key
     * @param value 缓存值
     */
    public static void put(String key, Object value){
        redistemplate.opsForValue().set(key, value);
    }
    /**
     * 缓存数据
     * @param key 缓存key
     * @param value 缓存值
     * @param timeout 缓存失效时间,单位:秒
     */
    public static void put(String key, Object value, Long timeout){
        put(key, value, Duration.ofSeconds(timeout));
    }

    /**
     * 缓存数据
     * @param key 缓存key
     * @param value 缓存值
     * @param timeout 缓存失效时间,单位:秒
     */
    public static void put(String key, Object value, Duration timeout){
        redistemplate.opsForValue().set(key, value, timeout);
    }

    /**
     * 获取缓存
     * @param key 缓存key
     * @return 缓存值
     */
    public static Object get(String key) {
        return redistemplate.opsForValue().get(key);
    }

    /**
     * 缓存失效时间设置
     * @param key 缓存key
     * @param timeout 失效时间 ,单位s
     */
    public static void expire(String key, Long timeout) {
        redistemplate.expire(key, timeout, TimeUnit.SECONDS);
    }

    /**
     * 缓存失效时间设置
     * @param key 缓存key
     * @param date 失效时间
     */
    public static void expireAt(String key, Date date) {
        redistemplate.expireAt(key, date);
    }

    /**
     * 删除缓存
     * @param key 缓存key
     * @return 是否删除成功
     */
    public static Boolean delete(String key) {
        return redistemplate.delete(key);
    }

    /**
     * 往hash中添加字段和值
     * @param key 缓存key
     * @param field hash表中的key
     * @param value hash表中的value
     */
    public static void hashSet(String key, String field, Object value) {
        redistemplate.opsForHash().put(key, field, value);
    }
    /**
     * 获取hash中field字段的值
     * @param key 缓存key
     * @param field hash表中的key
     * @return hash表中的value
     */
    public static Object hashGet(String key, String field) {
        return redistemplate.opsForHash().get(key, field);
    }

    /**
     * 获取hash表
     * @param key 缓存key
     * @return
     */
    public static Map<Object, Object> entries(String key) {
        return redistemplate.opsForHash().entries(key);
    }

    /**
     * 获取hash中所有的field
     * @param key 缓存key
     * @return
     */
    public static Set<Object> hashKeys(String key) {
        return redistemplate.opsForHash().keys(key);
    }

    /**
     * 获取hash中所有的value
     * @param key 缓存key
     * @return
     */
    public static List<Object> hashValues(String key) {
        return redistemplate.opsForHash().values(key);
    }

    /**
     * 该hash中是否存在field
     * @param key
     * @param field
     * @return
     */
    public static boolean hasKey(String key, String field){
        return redistemplate.opsForHash().hasKey(key, field);
    }

    /**
     * 获取hash key 中指定的field的值
     * @param key
     * @param fieldList
     * @return
     */
    public static List hashMultiGet(String key, Collection fieldList){
        return redistemplate.opsForHash().multiGet(key, fieldList);
    }

    /**
     * 迭代hash 指定条件的field
     * @param key
     * @param options
     * @return
     */
    public static Cursor<Map.Entry<Object, Object>> hScan(String key, Scanoptions options){
        return redistemplate.opsForHash().scan(key, options);
    }

    /**
     * 删除多个缓存
     * @param keys
     * @return 成功删除数量
     */
    public static Long delete(List<String> keys) {
        return redistemplate.delete(keys);
    }

    /**
     * 删除hash中field缓存
     * @param key 缓存key
     * @param fields hash表的key列表
     * @return 成功删除数量
     */
    public static Long hashDelete(String key, List<String> fields) {
        return redistemplate.opsForHash().delete(key, fields.toArray());
    }

    /**
     * 计数器加1
     * @param key
     * @return
     */
    public static Long increment(String key) {
        return redistemplate.opsForValue().increment(key);
    }

    /**
     * 计数器以指定步长递增
     * @param key 计数器key
     * @param delta 自增步长
     * @return
     */
    public static Long increment(String key, long delta){
        return redistemplate.opsForValue().increment(key, delta);
    }

    /**
     * 计数器以指定步长递增
     * @param key 计数器key
     * @param delta 自增步长
     * @return
     */
    public static Double increment(String key, double delta){
        return redistemplate.opsForValue().increment(key, delta);
    }

    /**
     * hash中field对应计数器加值
     * @param key
     * @param field
     * @param delta
     * @return
     */
    public static Long hashIncrement(String key, String field, long delta) {
        return redistemplate.opsForHash().increment(key, field, delta);
    }

    /**
     * 计数器减1
     * @param key
     * @return
     */
    public static Long decrement(String key) {
        return redistemplate.opsForValue().decrement(key);
    }

    /**
     * 计数器以指定步长递减
     * @param key 计数器key
     * @param delta 递减步长
     * @return
     */
    public static Long decrement(String key, long delta){
        return redistemplate.opsForValue().decrement(key, delta);
    }


    /**
     * 从队列右边移除一个元素并返回该元素
     * @author zhanglihui
     * @date 2020-11-06 10:42
     * @param key
     * @return java.lang.Object
     */
    public static Object rightPop(String key){
        return redistemplate.opsForList().rightPop(key);
    }

    /**
     * 往列表尾部插入一个元素
     * @param key
     * @param value
     */
    public static void rightPush(String key, Object value) {
        redistemplate.opsForList().rightPush(key, value);
    }

    /**
     * 从队列尾部插入多个元素
     * @param key  键值
     * @param values  插入元素列表
     * @return 插入个数
     */
    public static Long rightPushAll(String key, Object... values){
        return redistemplate.opsForList().rightPushAll(key, values);
    }

    /**
     * 移除并获取列表头部第一个元素
     * @param key
     * @return
     */
    public static Object leftPop(String key) {
        return redistemplate.opsForList().leftPop(key);
    }

    /**
     * 从队列左边插入一个元素
     * @author zhanglihui
     * @date 2020-11-06 10:42
     * @param key 键值
     * @param value 插入元素
     * @return 插入个数
     */
    public static Long leftPush(String key, Object value){
        return redistemplate.opsForList().leftPush(key, value);
    }

    /**
     * 从队列左边插入多个元素
     * @param key  键值
     * @param values  插入元素列表
     * @return 插入个数
     */
    public static Long leftPushAll(String key, Object... values){
        return redistemplate.opsForList().leftPushAll(key, values);
    }

    /**
     * 获取列表长度
     * @author zhanglihui
     * @date 2020-11-06 10:46
     * @param key
     * @return java.lang.Long
     */
    public static Long listSize(String key){
        return redistemplate.opsForList().size(key);
    }

    /**
     * 获取列表中的数据
     * @author zhanglihui
     * @date 2020-11-06 10:47
     * @param key 缓存值
     * @param start list开始下标
     * @param end list结束下标,-1代表末尾
     * @return java.util.List<java.lang.Object>
     */
    public static List<Object> listRange(String key, int start, int end){
        return redistemplate.opsForList().range(key, start, end);
    }

    /**
     * 保留指定区间内元素,其余元素都将被删除
     * @param key list key
     * @param start 开始下标
     * @param end 结束下标
     */
    public static void listTrim(String key, int start, int end){
        redistemplate.opsForList().trim(key,start,end);
    }

    /**
     * 批量查询
     * @param keys 查询主键列表
     */
    public static List<Object> multiGet(List<String> keys) {
        return redistemplate.opsForValue().multiGet(keys);
    }

    /**
     * 将member元素添加到有序集key中,按score排序
     * @param key 有序集
     * @param member 有续集的成员
     * @param score 有序集的排序值
     * @return 是否添加成功
     */
    public static Boolean zAdd(String key, Object member, double score){
        return redistemplate.opsForZSet().add(key, member, score);
    }

    /**
     * 批量添加元素到有序集key中
     * @param key 有序集
     * @param tuples 元素列表
     * @return 添加数量
     */
    public static Long zAdd(String key, Set<ZSetoperations.TypedTuple<Object>> tuples){
        return redistemplate.opsForZSet().add(key, tuples);
    }

    /**
     * 返回有序集 key 中,指定区间内的成员。
     * @param key 有序集
     * @param start 下标起始位置,从0开始
     * @param end 下标结束位置,-1表示最后一个
     * @return 有序集列表
     */
    public static Set<ZSetoperations.TypedTuple<Object>> zRange(String key, long start, long end){
        return redistemplate.opsForZSet().rangeWithscores(key, start, end);
    }

    /**
     * 根据设置的score获取区间值
     * @param key 有序集
     * @param min score起始值
     * @param max score结束值
     * @return 有序区间值
     */
    public static Set<ZSetoperations.TypedTuple<Object>> zRangeByscore(String key, double min, double max){
        return redistemplate.opsForZSet().rangeByscoreWithscores(key, min, max);
    }

    /**
     * 根据设置的score获取区间值
     * @param key 有序集
     * @param min score起始值
     * @param max score结束值
     * @param offset 偏移值
     * @param count 返回数量
     * @return 有序区间值
     */
    public static Set<ZSetoperations.TypedTuple<Object>> zRangeByscore(String key, double min, double max, int offset, int count){
        return redistemplate.opsForZSet().rangeByscoreWithscores(key, min, max, offset, count);
    }

    /**
     * 移除有序集 key 中,所有 score 值介于 min 和 max 之间
     * @param key 有序集
     * @param min score最小值
     * @param max score最大值
     * @return 删除条数
     */
    public static Long removeByscore(String key, double min, double max){
        return redistemplate.opsForZSet().removeRangeByscore(key, min, max);
    }

    /**
     * 删除zset中的某个元素
     * @param key 有序集
     * @param values 元素
     * @return 删除条数
     */
    public static Long zSetRemove(String key, Object... values){
        return redistemplate.opsForZSet().remove(key, values);
    }

    /**
     * 根据score范围查询元素,倒序排序
     * @param key 有序集
     * @param min score最小值
     * @param max score最大值
     * @param offset 偏移量
     * @param count  返回数量
     * @return Set<ZSetoperations.TypedTuple<Object>>
     */
    public static Set<ZSetoperations.TypedTuple<Object>> zReverseRangeByscoreWithscores(String key, double min, double max, long offset, long count){
        return redistemplate.opsForZSet().reverseRangeByscoreWithscores(key, min, max, offset, count);
    }

    /**
     * 根据score范围查询元素,倒序排序
     * @param key 有序集
     * @param min score最小值
     * @param max score最大值
     * @return Set<ZSetoperations.TypedTuple<Object>>
     */
    public static Set<ZSetoperations.TypedTuple<Object>> zReverseRangeByscoreWithscores(String key, double min, double max){
        return redistemplate.opsForZSet().reverseRangeByscoreWithscores(key, min, max);
    }

    /**
     * zset根据下标倒序查询
     * @param key 缓存key
     * @param start 开始下标
     * @param end 结束下标
     * @return
     */
    public static Set<ZSetoperations.TypedTuple<Object>> zReverseRangeByscore(String key, long start, long end){
        return redistemplate.opsForZSet().reverseRangeWithscores(key, start, end);
    }



    /**
     * 获取元素的分值
     * @param key 有序集
     * @param value 元素
     * @return 分值
     */
    public static Double score(String key, Object value){
        return redistemplate.opsForZSet().score(key, value);
    }

    /**
     * 指定元素增加score
     * @param key 有序集
     * @param value 元素
     * @param delta 增加值
     * @return score
     */
    public static Double incrementscore(String key, Object value, double delta){
        return redistemplate.opsForZSet().incrementscore(key, value, delta);
    }

    /**
     * 查询zset大小
     * @param key 有序集
     * @return zset元素数量
     */
    public static Long zSize(String key){
        return redistemplate.opsForZSet().size(key);
    }

    /**
     * 根据score范围查询数量
     * @param key 有序集
     * @param min score最小值
     * @param max score最大值
     * @return 统计数量
     */
    public static Long zCount(String key, double min, double max){
        return redistemplate.opsForZSet().count(key, min, max);
    }

    /**
     * 根据参数 count 的值,移除列表中与参数 delta 相等的元素。
     * @param key 队列
     * @param count
     * count > 0 : 从表头开始向表尾搜索,移除与 VALUE 相等的元素,数量为 COUNT 。
     * count < 0 : 从表尾开始向表头搜索,移除与 VALUE 相等的元素,数量为 COUNT 的绝对值。
     * count = 0 : 移除表中所有与 VALUE 相等的值。
     * @param value 元素
     * @return
     */
    public static Long listRem (String key, long count, Object value){
        return redistemplate.opsForList().remove(key, count, value);
    }


    /**
     * 移除有序集中,指定排名(rank)区间内的所有成员。
     * @param key key
     * @param start 排名开始位置
     * @param end 排名结束位置
     * @return 被移除成员的数量
     */
    public static Long zRemByRank(String key, long start, long end){
        return redistemplate.opsForZSet().removeRange(key, start, end);
    }

    /**
     * 返回哈希表中,所有的字段和值
     * @param key key
     * @return 所有的字段和值
     */
    public static Map<Object,Object> hashAll(String key){
        return redistemplate.opsForHash().entries(key);
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。