SpringBoot RedisTemplate 工具类

package com.just.common.util;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * @author liuguozhen
 * @date 2019/08/12 14:46
 * @description redis工具
 */
@Component
public class RedisUtil {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    // =============================common============================

    /**
     * 为给定的key设定过期时间
     *
     * @param key  键
     * @param time 过期时间
     * @param unit 单位(纳秒,微秒,毫秒,秒,分钟,小时,天)
     * @return true成功 false失败
     */
    public Boolean expire(String key, long time, TimeUnit unit) {
        return redisTemplate.expire(key, time, unit);
    }

    /**
     * 为给定的key设置到期日期时间戳
     *
     * @param key  键
     * @param date 到期日期时间戳
     * @return true成功 false失败
     */
    public Boolean expireAt(String key, Date date) {
        return redisTemplate.expireAt(key, date);
    }

    /**
     * 根据key 获取过期时间
     *
     * @param key 键
     * @return -1是不过期 -2关键字不存在 其他为过期时间
     */
    public Long getExpire(String key) {
        // -2表示key不存在或者已过期
        // -1表示key永久存在
        return redisTemplate.getExpire(key);
    }

    /**
     * 获取key的过期时间并根据unit转换
     *
     * @param key  键
     * @param unit 单位
     * @return 转换后的时间
     */
    public Long getExpire(String key, TimeUnit unit) {
        return redisTemplate.getExpire(key, unit);
    }

    /**
     * 判断key是否存在
     *
     * @param key 键
     * @return true存在 false不存在
     */
    public Boolean hasKey(String key) {
        return redisTemplate.hasKey(key);
    }

    /**
     * 删除缓存
     *
     * @param key 键
     * @return true成功 false失败
     */
    public Boolean delete(String key) {
        return redisTemplate.delete(key);
    }

    /**
     * 批量删除缓存
     *
     * @param keys 键(可以多个)
     * @return 成功数量
     */
    public Long delete(String... keys) {
        List<String> keyList = Arrays.asList(keys);
        return redisTemplate.delete(keyList);
    }

    // ============================String=============================

    /**
     * 追加到已存在key的value值后,如果key不存在,新增
     *
     * @param key   键
     * @param value 值
     * @return 追加/新增后的长度
     */
    public Integer append(String key, String value) {
        return redisTemplate.opsForValue().append(key, value);
    }

    /**
     * 存储值递减1
     *
     * @param key 键
     * @return 递减后的数值
     */
    public Long decrement(String key) {
        return redisTemplate.opsForValue().decrement(key);
    }

    /**
     * 存储值递减delta
     *
     * @param key   键
     * @param delta 递减值
     * @return 递减后的数值
     */
    public Long decrement(String key, long delta) {
        return redisTemplate.opsForValue().decrement(key, delta);
    }

    /**
     * 获取key对应value的开始位到结束位的子字符串
     *
     * @param key   键
     * @param start 开始位
     * @param end   结束位
     * @return 对应value的子字符串
     */
    public String get(String key, long start, long end) {
        return redisTemplate.opsForValue().get(key, start, end);
    }

    /**
     * 获取key对应value值
     *
     * @param key 键
     * @return 对应value
     */
    public Object get(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    /**
     * 存储值递增1
     *
     * @param key 键
     * @return 递增后的值
     */
    public Long increment(String key) {
        return redisTemplate.opsForValue().increment(key);
    }

    /**
     * 存储浮点型值递增delta
     *
     * @param key   键
     * @param delta 浮点型递增值
     * @return 递增后浮点型的值
     */
    public Double increment(String key, double delta) {
        return redisTemplate.opsForValue().increment(key, delta);
    }

    /**
     * 存储值递增delta
     *
     * @param key   键
     * @param delta 递增值
     * @return 递增后的值
     */
    public Long increment(String key, long delta) {
        return redisTemplate.opsForValue().increment(key, delta);
    }

    /**
     * 存储key
     *
     * @param key   键
     * @param value 值
     */
    public void set(String key, Object value) {
        redisTemplate.opsForValue().set(key, value);
    }

    /**
     * key对应value的长度
     *
     * @param key 键
     * @return value的长度
     */
    public Long size(String key) {
        return redisTemplate.opsForValue().size(key);
    }

    // ================================Map=================================

    /**
     * 删除key对应map中的hashKeys的值
     *
     * @param key      键
     * @param hashKeys map键
     * @return 成功数量
     */
    public Long delete(String key, Object... hashKeys) {
        return redisTemplate.opsForHash().delete(key, hashKeys);
    }

    /**
     * 获取key对应的map
     *
     * @param key 键
     * @return 对应map
     */
    public Map<Object, Object> entries(String key) {
        return redisTemplate.opsForHash().entries(key);
    }

    /**
     * 判断key对应的map是否含有hashKey
     *
     * @param key     键
     * @param hashKey map键
     * @return true存在 false不存在
     */
    public Boolean hasKey(String key, Object hashKey) {
        return redisTemplate.opsForHash().hasKey(key, hashKey);
    }

    /**
     * key对应map的hashKey浮点型递增
     *
     * @param key     键
     * @param hashKey map键
     * @param delta   递增浮点型的值
     * @return 递增浮点型后的值
     */
    public Double increment(String key, Object hashKey, double delta) {
        return redisTemplate.opsForHash().increment(key, hashKey, delta);
    }

    /**
     * 对应keymap的hashKey递增
     *
     * @param key     键
     * @param hashKey map键
     * @param delta   递增的值
     * @return 递增浮后的值
     */
    public Long increment(String key, Object hashKey, long delta) {
        return redisTemplate.opsForHash().increment(key, hashKey, delta);
    }


    /**
     * key对应map的hashKey值的长度
     *
     * @param key     键
     * @param hashKey map的键
     * @return map的键的长度
     */
    public Long lengthOfValue(String key, Object hashKey) {
        return redisTemplate.opsForHash().lengthOfValue(key, hashKey);
    }

    /**
     * 添加/覆盖key对应map的hashKey的值
     *
     * @param key     键
     * @param hashKey map的键
     * @param value   存储的值
     */
    public void put(String key, Object hashKey, Object value) {
        redisTemplate.opsForHash().put(key, hashKey, value);
    }

    /**
     * 存储一个map
     *
     * @param key 键
     * @param map 存储的map
     */
    public void putAll(String key, Map<Object, Object> map) {
        redisTemplate.opsForHash().putAll(key, map);
    }

    /**
     * key对应map的长度
     *
     * @param key 键
     * @return map的长度
     */
    public Long hSize(String key) {
        return redisTemplate.opsForHash().size(key);
    }

    //  ============================set=============================

    /**
     * 添加key对应的set值
     *
     * @param key    键
     * @param values 值(可以多个)
     * @return 成功的数量
     */
    public Long add(String key, Object... values) {
        return redisTemplate.opsForSet().add(key, values);
    }


    /**
     * key和otherKeys set的差集
     *
     * @param key       键
     * @param otherKeys 其他的键 (可以多个)
     * @return 差集
     */
    public Set<Object> difference(String key, String... otherKeys) {
        List<String> keyList = Arrays.asList(otherKeys);
        return redisTemplate.opsForSet().difference(key, keyList);
    }

    /**
     * key和otherKey set的差集
     *
     * @param key      键
     * @param otherKey 其他的键
     * @return 差集
     */
    public Set<Object> difference(String key, String otherKey) {
        return redisTemplate.opsForSet().difference(key, otherKey);
    }

    /**
     * key和otherKeys set的差集,如果有差集就用存储键保存
     *
     * @param key       键
     * @param destKey   存储键,可以是不存在的,也可以是已经存在的
     * @param otherKeys 其他的键(可以多个)
     * @return 差集的数量
     */
    public Long differenceAndStore(String key, String destKey, String... otherKeys) {
        List<String> keyList = Arrays.asList(otherKeys);
        return redisTemplate.opsForSet().differenceAndStore(key, keyList, destKey);
    }

    /**
     * key和otherKey set的差集,如果有差集就用存储键保存
     *
     * @param key      键
     * @param otherKey 其他的键
     * @param destKey  存储键,可以是不存在的,也可以是已经存在的
     * @return 差集合的数量
     */
    public Long differenceAndStore(String key, String otherKey, String destKey) {
        return redisTemplate.opsForSet().differenceAndStore(key, otherKey, destKey);
    }

    /**
     * 取出key对应set count数量的值打乱
     *
     * @param key   键
     * @param count 取出数量
     * @return 打乱的集合
     */
    public Set<Object> distinctRandomMembers(String key, long count) {
        return redisTemplate.opsForSet().distinctRandomMembers(key, count);
    }

    /**
     * key和otherKeys set的交集
     *
     * @param key       键
     * @param otherKeys 其他的键(可以多个)
     * @return 交集合
     */
    public Set<Object> intersect(String key, String... otherKeys) {
        List<String> keyList = Arrays.asList(otherKeys);
        return redisTemplate.opsForSet().intersect(key, keyList);
    }

    /**
     * key和otherKey set的交集
     *
     * @param key      键
     * @param otherKey 其他的键
     * @return 交集合
     */
    public Set<Object> intersect(String key, String otherKey) {
        return redisTemplate.opsForSet().intersect(key, otherKey);
    }

    /**
     * key和otherKeys set的交集,如果有交集就用存储键保存
     *
     * @param key       键
     * @param destKey   存储键,可以是不存在的,也可以是已经存在的
     * @param otherKeys 其他的键(可以多个)
     * @return 交集合的数量
     */
    public Long intersectAndStore(String key, String destKey, String... otherKeys) {
        List<String> keyList = Arrays.asList(otherKeys);
        return redisTemplate.opsForSet().intersectAndStore(key, keyList, destKey);
    }

    /**
     * key和otherKey set的交集,如果有交集就用存储键保存
     *
     * @param key      键
     * @param otherKey 其他的键
     * @param destKey  存储键,可以是不存在的,也可以是已经存在的
     * @return 交集合的数量
     */
    public Long intersectAndStore(String key, String otherKey, String destKey) {
        return redisTemplate.opsForSet().intersectAndStore(key, otherKey, destKey);
    }

    /**
     * key对应set是否含有value
     *
     * @param key   键
     * @param value 值
     * @return true含有 false不含有
     */
    public Boolean isMember(String key, Object value) {
        return redisTemplate.opsForSet().isMember(key, value);
    }

    /**
     * 取出key对应的set
     *
     * @param key 键
     * @return 对应的值
     */
    public Set<Object> members(String key) {
        return redisTemplate.opsForSet().members(key);
    }

    /**
     * 移除key中value值到destKey中
     *
     * @param key     键
     * @param value   值
     * @param destKey 其他键,可以是不存在的,也可以是已经存在的
     * @return true成功 false失败
     */
    public Boolean move(String key, Object value, String destKey) {
        return redisTemplate.opsForSet().move(key, value, destKey);
    }


    /**
     * 移除key对应set的随机元素
     *
     * @param key 键
     * @return 移除的值
     */
    public Object pop(String key) {
        return redisTemplate.opsForSet().pop(key);
    }

    /**
     * 移除key对应set的随机元素,小于等于count
     *
     * @param key   键
     * @param count 移除的数量
     * @return 移除的值(list)
     */
    public List<Object> pop(String key, long count) {
        return redisTemplate.opsForSet().pop(key, count);
    }

    /**
     * 随机获取key对应set中的值
     *
     * @param key 键
     * @return key中的value
     */
    public Object randomMember(String key) {
        return redisTemplate.opsForSet().randomMember(key);
    }

    /**
     * 随机获取key对应set中的值,小于等于count
     *
     * @param key   键
     * @param count 取的数量
     * @return 取出的值(list)
     */
    public List<Object> randomMember(String key, long count) {
        return redisTemplate.opsForSet().randomMembers(key, count);
    }

    /**
     * 移除key对应set中的values
     *
     * @param key    键
     * @param values 移除的值
     * @return 移除成功的数量
     */
    public Long remove(String key, Object... values) {
        return redisTemplate.opsForSet().remove(key, values);
    }

    /**
     * 获取key对应set的长度
     *
     * @param key 键
     * @return set的长度
     */
    public Long sSize(String key) {
        return redisTemplate.opsForSet().size(key);
    }

    /**
     * key和otherKeys set的并集
     *
     * @param key       键
     * @param otherKeys 其他键(可以多个)
     * @return 并集
     */
    public Set<Object> union(String key, String... otherKeys) {
        List<String> keyList = Arrays.asList(otherKeys);
        return redisTemplate.opsForSet().union(key, keyList);
    }

    /**
     * key和otherKey set的并集
     *
     * @param key      键
     * @param otherKey 其他键
     * @return 并集
     */
    public Set<Object> union(String key, String otherKey) {
        return redisTemplate.opsForSet().union(key, otherKey);
    }

    /**
     * key和otherKeys set的并集,添加到destKey
     *
     * @param key       键
     * @param destKey   存储键,可以是不存在的,也可以是已经存在的
     * @param otherKeys 其他键(可以多个)
     * @return 并集的数量
     */
    public Long unionAndStore(String key, String destKey, String... otherKeys) {
        List<String> keyList = Arrays.asList(otherKeys);
        return redisTemplate.opsForSet().unionAndStore(key, keyList, destKey);
    }

    /**
     * key和otherKey set的并集,添加到destKey
     *
     * @param key      键
     * @param otherKey 其他键
     * @param destKey  存储键,可以是不存在的,也可以是已经存在的
     * @return 并集的数量
     */
    public Long unionAndStore(String key, String otherKey, String destKey) {
        return redisTemplate.opsForSet().unionAndStore(key, otherKey, destKey);
    }

    // ===============================list=================================

    /**
     * 获取key对应list(开始位到结束位,结束位为-1时,取出所有元素)
     *
     * @param key   键
     * @param start 开始位
     * @param end   结束位
     * @return 对应的list
     */
    public List<Object> range(String key, long start, long end) {
        return redisTemplate.opsForList().range(key, start, end);
    }

    /**
     * 清除key对应的list元素(保留开始位到结束位的元素,删除其他元素)
     *
     * @param key   键
     * @param start 开始位
     * @param end   结束位
     */
    public void trim(String key, long start, long end) {
        redisTemplate.opsForList().trim(key, start, end);
    }

    /**
     * 获取key对应list的长度
     *
     * @param key 键
     * @return list的长度
     */
    public Long lSize(String key) {
        return redisTemplate.opsForList().size(key);
    }


    /**
     * 从头部添加元素
     *
     * @param key   键
     * @param value 值
     * @return 添加后list的长度
     */
    public Long leftPush(String key, Object value) {
        return redisTemplate.opsForList().leftPush(key, value);
    }

    /**
     * 从头部添加全部元素
     *
     * @param key    键
     * @param values 值(可以多个)
     * @return 添加后list的长度
     */
    public Long leftPushAll(String key, Object... values) {
        return redisTemplate.opsForList().leftPushAll(key, values);
    }

    /**
     * 从头部添加全部元素
     *
     * @param key    键
     * @param values 值(可以多个)
     * @return 添加后list的长度
     */
    public Long leftPushAll(String key, Collection<Object> values) {
        return redisTemplate.opsForList().leftPushAll(key, values);
    }

    /**
     * 从头部添加元素(仅当key值对应list存在添加成功)
     *
     * @param key   键
     * @param value 值
     * @return 添加后list的长度,0代表失败
     */
    public Long leftPushIfPresent(String key, Object value) {
        return redisTemplate.opsForList().leftPushIfPresent(key, value);
    }

    /**
     * 从头部出发,首先出现的pivot值前添加value
     *
     * @param key   键
     * @param pivot 目标
     * @param value 值
     * @return 添加后list的长度
     */
    public Long leftPush(String key, Object pivot, Object value) {
        return redisTemplate.opsForList().leftPush(key, pivot, value);
    }

    /**
     * 从尾部添加一个元素
     *
     * @param key   键
     * @param value 值
     * @return 添加后list的长度
     */
    public Long rightPush(String key, Object value) {
        return redisTemplate.opsForList().rightPush(key, value);
    }

    /**
     * 从尾部添加全部元素
     *
     * @param key    键
     * @param values 值(可以多个)
     * @return 添加后list的长度
     */
    public Long rightPushAll(String key, Object... values) {
        return redisTemplate.opsForList().rightPushAll(key, values);
    }

    /**
     * 从尾部添加全部元素
     *
     * @param key    键
     * @param values 值(可以多个)
     * @return 添加后list的长度
     */
    public Long rightPushAll(String key, Collection<Object> values) {
        return redisTemplate.opsForList().rightPushAll(key, values);
    }

    /**
     * 从尾部添加元素(仅当key值对应list存在添加成功)
     *
     * @param key   键
     * @param value 值
     * @return 添加后list的长度
     */
    public Long rightPushIfPresent(String key, Object value) {
        return redisTemplate.opsForList().rightPushIfPresent(key, value);
    }

    /**
     * 从尾部出发,首先出现的pivot值前添加value
     *
     * @param key   键
     * @param pivot 目标
     * @param value 值
     * @return 添加后list的长度
     */
    public Long rightPush(String key, Object pivot, Object value) {
        return redisTemplate.opsForList().rightPush(key, pivot, value);
    }

    /**
     * 替换key对应list的index索引位上的值为value
     *
     * @param key   键
     * @param index 索引
     * @param value 值
     */
    public void set(String key, long index, Object value) {
        redisTemplate.opsForList().set(key, index, value);
    }

    /**
     * 从头部开始移除key对应list中的value值,小于等于count
     *
     * @param key   键
     * @param count 次数
     * @param value 值
     * @return 1成功 0失败
     */
    public Long remove(String key, long count, Object value) {
        return redisTemplate.opsForList().remove(key, count, value);
    }

    /**
     * 从头部开始,取出key对应list中index索引位的值
     *
     * @param key   键
     * @param index 索引
     * @return list索引处的值
     */
    public Object index(String key, long index) {
        return redisTemplate.opsForList().index(key, index);
    }

    /**
     * 从头部开始移除一位
     *
     * @param key 键
     * @return 移除的值
     */
    public Object leftPop(String key) {
        return redisTemplate.opsForList().leftPop(key);
    }

    /**
     * 从尾部开始移除一位
     *
     * @param key 键
     * @return 移除的值
     */
    public Object rightPop(String key) {
        return redisTemplate.opsForList().rightPop(key);
    }

    /**
     * 从sourceKey尾部移除一个值删除,然后插入destinationKey的左侧
     *
     * @param sourceKey      移除键
     * @param destinationKey 保存键
     * @return 移除的值
     */
    public Object rightPopAndLeftPush(String sourceKey, String destinationKey) {
        return redisTemplate.opsForList().rightPopAndLeftPush(sourceKey, destinationKey);
    }


}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值