Redis工具类

public class RdsUtils {

    @Resource
    private static RedisTemplate redisTemplate;


    /**
     * 设置键值对
     *
     * @param key   键
     * @param value 值
     * @return
     */
    public static Boolean set(String key, String value) {
        try {
            redisTemplate.opsForValue().set(key,value);
            return true;
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return false;
    }


    /**
     * 设置键值对,并给键设置过期时间
     *
     * @param key   键
     * @param value 值
     * @param time  过期时间(单位:秒)
     * @return
     */
    public static Boolean setex(String key, String value, int time) {
        try {
            if(time <= 0 ){
                set(key,value);
            }else{
                redisTemplate.opsForValue().set(key,value,time, TimeUnit.SECONDS);
            }
            return true;
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return false;
    }


    /**
     * 设置键值对, 如果键已存在则设置失败
     *
     * @param key   键
     * @param value 值
     * @return
     */
    public static Boolean setIfAbsent(String key, String value) {
        try {
            return redisTemplate.opsForValue().setIfAbsent(key,value);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return false;
    }


    /**
     * 通过键获取值
     *
     * @param key 键
     * @return
     */
    public static String get(String key) {
        try {
            return (String)redisTemplate.opsForValue().get(key);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }


    /**
     * 一次性获取多个 key 的 value 值
     *
     * @param keys 键集合
     * @return
     */
    public static List mget(String... keys) {
        try {
            if(keys == null || keys.length == 0){
                return null;
            }
            return redisTemplate.opsForValue().multiGet(Arrays.asList(keys));
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }


    /**
     * 通过 key 获取 value 的字符数
     *
     * @param key
     * @return
     */
    public static Long strlen(String key) {
        try {
            return redisTemplate.opsForValue().size(key);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return 0L;
    }


    /**
     * 通过 key 获取 value 并截取部分字符串
     * 下标超出范围则返回错误
     *
     * @param key   键
     * @param start 开始下标
     * @param end   结束下标
     * @return
     */
    public static String substr(String key, int start, int end) {
        try {
            String value = get(key);
            if(StringUtils.isEmpty(value)){
                return null;
            }else{
                if(end <= 0){
                    end = value.length();
                }
                return value.substring(start, end);
            }
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }


    /**
     * 通过 key 获取 value,并用新值覆盖
     * 返回 旧值
     *
     * @param key   键
     * @param value 新值
     * @return
     */
    public static String getset(String key, String value) {
        try {
            return (String)redisTemplate.opsForValue().getAndSet(key, value);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }


    /**
     * 通过 key 获取 value 并在已有基础上追加内容
     * 如果不存在则创建 value
     *
     * @param key   键
     * @param value 追加内容
     * @return
     */
    public static Integer append(String key, String value) {
        try {
            return redisTemplate.opsForValue().append(key, value);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }


    /**
     * 通过 key 获取 value 并将 value 当作整数减 1
     *
     * @param key 键
     * @return
     */
    public static Long decr(String key) {
        try {
            return redisTemplate.opsForValue().decrement(key);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return 0L;
    }


    /**
     * 通过 key 获取 value 并将 value 当作整数减去设定值
     *
     * @param key      键
     * @param subValue 设定值
     * @return
     */
    public static Long decr(String key, int subValue) {
        try {
            return redisTemplate.opsForValue().decrement(key,subValue);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return 0L;
    }


    /**
     * 通过 key 获取 value 并将 value 当作整数加 1
     *
     * @param key 键
     * @return
     */
    public static Long incr(String key) {
        try {
            return redisTemplate.opsForValue().increment(key);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return 0L;
    }


    /**
     * 通过 key 获取 value 并将 value 当作整数增加设定值
     *
     * @param key      键
     * @param addValue 增加值
     * @return
     */
    public static Long incr(String key, int addValue) {
        try {
            return redisTemplate.opsForValue().increment(key,addValue);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return 0L;
    }


    /**
     * 通过 key 获取 value 并将 value 当作浮点类型增加设定值
     *
     * @param key      键
     * @param addValue 增加值(浮点类型)
     * @return
     */
    public static Double incrByFloat(String key, float addValue) {
        try {
            return redisTemplate.opsForValue().increment(key,addValue);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }


    /**
     * 通过 key 删除相关记录
     *
     * @param key 键
     * @return
     */
    public static Boolean del(String key) {
        try {
            return redisTemplate.delete(key);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return false;
    }


    /**
     * 判断指定 key 是否存在(且未过期)
     *
     * @param key 键
     * @return
     */
    public static Boolean exists(String key) {
        try {
            Boolean exist = redisTemplate.hasKey(key);
            if(exist){
                Long expire = redisTemplate.getExpire(key, TimeUnit.SECONDS);
                if(expire == null || expire == 0L){
                    return false;
                }
            }
            return false;
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return false;
    }


    /**
     * 设置指定 key 的相对过期时间
     *
     * @param key    键
     * @param expire 过期时间(单位:秒)
     * @return
     */
    public static Boolean expire(String key, int expire) {
        try {
            return redisTemplate.expire(key, expire, TimeUnit.SECONDS);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return false;
    }


    /**
     * 设置指定 key 的绝对过期时间
     *
     * @param key    键
     * @param date   过期时间
     * @return
     */
    public static Boolean expireat(String key, Date date) {
        try {
            return redisTemplate.expireAt(key, date);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return false;
    }



    /**
     * 移除给 key 设定的过期时间,使得 key 永不过期
     *
     * @param key 键
     * @return
     */
    public static Boolean persist(String key) {
        try {
            return redisTemplate.persist(key);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return false;
    }


    /**
     * 判断 hash 数据类型是否包含指定项
     *
     * @param key   键
     * @param field 字段
     * @return
     */
    public static Boolean hexists(String key, String field) {
        try {
            return redisTemplate.opsForHash().hasKey(key, field);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return false;
    }


    /**
     * 获得 hash 数据类型中的指定项内容
     *
     * @param key   键
     * @param field 字段
     * @return
     */
    public static Object hget(String key, String field) {
        try {
            return redisTemplate.opsForHash().get(key, field);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }


    /**
     * 获取 hash 数据类型中指定 key 的全部项
     *
     * @param key 键
     * @return
     */
    public static Map hgetAll(String key) {
        try {
            return redisTemplate.opsForHash().entries(key);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }


    /**
     * 获取 hash 数据类型中的对应项的内容并增加整数值
     *
     * @param key      键
     * @param field    字段
     * @param addValue 增加值
     * @return
     */
    public static Long hincr(String key, String field, int addValue) {
        try {
            return redisTemplate.opsForHash().increment(key, field, addValue);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }


    /**
     * 获取 hash 类型数据中的对应项的内容增加浮点数值
     *
     * @param key      键
     * @param field    字段
     * @param addValue 增加值
     * @return
     */
    public static Double hincrByDouble(String key, String field, float addValue) {
        try {
            return redisTemplate.opsForHash().increment(key, field, addValue);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }


    /**
     * 获取 hash 数据类型中指定 key 的所有 field 值
     *
     * @param key 键
     * @return
     */
    public static List hkeys(String key) {
        try {
            return redisTemplate.opsForHash().values(key);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }


    /**
     * 获取 hash 数据类型中指定 key 的 field 数量
     *
     * @param key 键
     * @return
     */
    public static Long hlen(String key) {
        try {
           return redisTemplate.opsForHash().size(key);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return 0L;
    }


    /**
     * 获取 hash 数据类型中指定 key 和多个 filed 的所有内容
     *
     * @param key    键
     * @param fields 字段集合
     * @return
     */
    public static List hmget(String key, String... fields) {
        try {
            return redisTemplate.opsForHash().multiGet(key, Arrays.asList(fields));
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }


    /**
     * 通过 key filed 新增 hash 数据类型数据
     * 如果指定项不存在则新建,已存在则覆盖。
     *
     * @param key   键
     * @param field 字段
     * @param value 值
     * @return
     */
    public static Boolean hset(String key, String field, String value) {
        try {
            redisTemplate.opsForHash().put(key,field,value);
            return true;
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return false;
    }


    /**
     * 通过 key filed 设置 hash 数据类型数据
     * 如果该项已存在则执行失败
     *
     * @param key   键
     * @param field 字段
     * @return
     */
    public static Boolean hsetIfAbsent(String key, String field, String value) {
        try {
            return redisTemplate.opsForHash().putIfAbsent(key, field, value);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return false;
    }


    /**
     * 获取 hash 数据类型指定 key 所有 value
     *
     * @param key 键
     * @return
     */
    public static List hvals(String key) {
        try {
            return redisTemplate.opsForHash().values(key);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }


    /**
     * 通过 key 获取 list 数据类型的列表长度
     *
     * @param key 键
     * @return
     */
    public static Long llen(String key) {
        try {
            return redisTemplate.opsForList().size(key);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return 0L;
    }


    /**
     * 通过 key 弹出指定 list 列表的头部项
     *
     * @param key 键
     * @return
     */
    public static Object lpop(String key) {
        try {
            return redisTemplate.opsForList().leftPop(key);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }


    /**
     * 通过 key 弹出指定 list 列表的尾部项
     *
     * @param key 键
     * @return
     */
    public static Object rpop(String key) {
        try {
            return redisTemplate.opsForList().rightPop(key);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }


    /**
     * 通过 key 在指定的 list 列表头部添加 1项
     *
     * @param key   键
     * @param value 值
     * @return
     */
    public static Long lpush(String key, String value) {
        try {
            return redisTemplate.opsForList().leftPush(key,value);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }


    /**
     * 通过 key 在指定的 list 列表尾部添加 1项
     *
     * @param key
     * @param value 值
     * @return
     */
    public static Long rpush(String key, String value) {
        try {
            return redisTemplate.opsForList().rightPush(key,value);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }


    /**
     * 通过 key 在指定的 list 列表头部添加 1 项
     * 列表不存在时操作无效
     *
     * @param key   键
     * @param value 值
     * @return
     */
    public static Long lpushx(String key, String value) {
        try {
            return redisTemplate.opsForList().leftPushIfPresent(key, value);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }


    /**
     * 通过 key 在指定的 list 列表头尾部添加 1 项
     * 如果列表不存在,操作无效
     *
     * @param key   键
     * @param value 值
     * @return
     */
    public static Long rpushx(String key, String value) {
        try {
            return redisTemplate.opsForList().rightPushIfPresent(key, value);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }


    /**
     * 通过 key 返回指定 list 列表中的所有数据
     *
     * @param key 键
     * @return
     */
    public static List lrange(String key) {
        try {
            return redisTemplate.opsForList().range(key,0,-1);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }


    /**
     * 通过 key 指定下标索引范围内的 list 列表
     *
     * @param key   键
     * @param start 开始下标
     * @param end   结束下标
     * @return
     */
    public static List lrange(String key, int start, int end) {
        try {
            return redisTemplate.opsForList().range(key,start,end);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }


    /**
     * 通过 key 在 list 列表指定位置设置列表项,
     * 如果指定位置超过 list 总长度则执行失败
     *
     * @param key   键
     * @param index 指定位置下标
     * @param value 值
     * @return
     */
    public static Boolean lset(String key, int index, String value) {
        try {
            redisTemplate.opsForList().set(key,index,value);
            return true;
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return false;

    }


    /**
     * 通过 key 指定下标索引范围截取 list 列表
     *
     * @param key   键
     * @param start 开始下标
     * @param end   结束下标
     * @return
     */
    public static Boolean ltrim(String key, int start, int end) {
        try {
            redisTemplate.opsForList().trim(key,start,end);
            return true;
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return false;
    }




    /**
     * 将成员元素加入到集合中,若已经存在则将被忽略
     *
     * @param key   键
     * @param field 字段
     * @return
     */
    public static Boolean saddIfAbstrent(String key, String field) {
        try {
            redisTemplate.opsForSet().add(key,field);
            return true;
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return false;
    }


    /**
     * 通过 key 获取 set 数据类型集合中的元素数量
     *
     * @param key
     * @return
     */
    public static Long scard(String key) {
        try {
            return redisTemplate.opsForSet().size(key);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return 0L;
    }


    /**
     * 通过指定 key 返回给定集合之间的差集
     * 不存在的集合 key 将视为空集
     *
     * @param key1 集合 1 key
     * @param key2 集合 2 key
     * @return
     */
    public static Set sdiff(String key1, String key2) {
        try {
            return redisTemplate.opsForSet().difference(key1, key2);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }


    /**
     * 返回给定所有给定集合的交集。
     * 不存在的集合 key 被视为空集。
     * 当给定集合当中有一个空集时,结果也为空集(根据集合运算定律)
     *
     * @param key1 集合 1 key
     * @param key2 集合 2 key
     * @return
     */
    public static Set sinter(String key1, String key2) {
        try {
            return redisTemplate.opsForSet().intersect(key1, key2);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }


    /**
     * 返回给定集合的并集
     * 不存在的集合 key 被视为空集
     *
     * @param key1 集合 1 key
     * @param key2 集合 2 key
     * @param keys 多个集合 key 数组
     * @return
     */
    public static Set sunion(String key1, String key2, String... keys) {
        try {
            return redisTemplate.opsForSet().union(key1, key2);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }


    /**
     * 将给定集合之间的差集存储在指定的集合中。
     * 如果指定的集合 key 已存在, 则会被覆盖
     *
     * @param key1 指定集合 key
     * @param key2 集合1 key
     * @param key3 集合2 key
     * @return
     */
    public static Long sdiffstore(String key1, String key2, String key3) {
        try {
            return redisTemplate.opsForSet().differenceAndStore(key1, key2,key3);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }


    /**
     * 将给定集合之间的交集存储在指定的集合中。
     * 如果指定的集合已经存在,则将其覆盖
     *
     * @param key1 指定集合 key
     * @param key2 集合1 key
     * @param key3 集合2 key
     * @return
     */
    public static Long sinterstore(String key1, String key2, String key3) {
        try {
            return redisTemplate.opsForSet().intersectAndStore(key1, key2,key3);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }


    /**
     * 将给定集合的并集存储在指定的集合中。
     * 如果集合已经存在,则将其覆盖
     *
     * @param key1 指定集合 key
     * @param key2 集合1 key
     * @param key3 集合2 key
     * @return
     */
    public static Long sunionstore(String key1, String key2, String key3) {
        try {
            return redisTemplate.opsForSet().unionAndStore(key1, key2,key3);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }


    /**
     * 通过 key 判断 field 是否是集合内的元素
     *
     * @param key   键
     * @param field 字段
     * @return
     */
    public static Boolean sismember(String key, String field) {
        try {
             return redisTemplate.opsForSet().isMember(key, field);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return false;
    }


    /**
     * 通过 key 获取指定集合中的所有的成员。
     * 不存在的集合将被视为空集合
     *
     * @param key 键
     * @return
     */
    public static Set smembers(String key) {
        try {
            return redisTemplate.opsForSet().members(key);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }




    /**
     * 通过 key 移除集合中为 field 的元素
     * 不存在的成员元素会被忽略
     *
     * @param key   键
     * @param fields 需要移除的元素
     * @return
     */
    public static Long srem(String key, String...fields) {
        try {
            return redisTemplate.opsForSet().remove(key, fields);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值