Spring boot对RedisTemplate封装工具类

springboot 使用redis 工具类基本操作

redisTemplate 初始化配置 https://blog.csdn.net/qq_44702705/article/details/109189665

  • redis 工具类
@Component
public class RedisClient<K,V> {

    @Autowired
    private RedisTemplate<K, V> redisTemplate;

    private static final long DEFAULT_EXPIRE_TIME = 60 * 60 * 48; // 默认存活时间2天

    //得到key 过期时间
    public long getExpireTime(K key){
        return redisTemplate.getExpire(key,TimeUnit.SECONDS);
    }
    //设置key 的过期时间
    public void setExpireTime(K key ,long expireTime){
         redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
    }
    //删除key
    public void delkey(K key){
        redisTemplate.delete(key);
    }
    //设置分布式锁
    public Boolean setNx(String key,String value,long expire){
        return (Boolean) redisTemplate.execute((RedisCallback) connection -> {
            Boolean acquire = connection.setNX(key.getBytes(),value.getBytes());
            connection.expire(key.getBytes(),expire);
            return acquire;
        });
    }
    //字段值增加 并返回值
    public Long incrBy(String key,long number){
        return (Long) redisTemplate.execute((RedisCallback) connection -> {
            Long value =  connection.incrBy(key.getBytes(),number);
            return value;
        });
    }
    //字段值减少 并返回值
    public Long decrBy(String key,long number){
        return (Long) redisTemplate.execute((RedisCallback) connection -> {
            Long value =  connection.decrBy(key.getBytes(),number);
            return value;
        });
    }

    /***********      String 类型操作           **************/
    //添加String ,设置过期时间
    public void putExpireTime(K key ,V value,long expireTime){
        BoundValueOperations<K, V> valueOper = redisTemplate.boundValueOps(key);
        valueOper.set(value);
        if (expireTime != -1) {
            redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
        }
    }

    //获取String 中 value
    public Object get(K key){
        BoundValueOperations<K, V> valueOper = redisTemplate.boundValueOps(key);
        if(valueOper != null){
            return valueOper.get();
        }
        return null;
    }

    /***********      list 类型操作           **************/

    //在队列中插入一条数据
    public void putListData(K key, V value) {
        ListOperations<K, V> boundValueOper = redisTemplate.opsForList();
        if (boundValueOper == null) {
            return;
        }
        //从队列右插入
        boundValueOper.rightPush(key, value);
        //从队列左插入
        //boundValueOper.leftPush(key,value);
    }

    //在队列中取出一条数据
    public Object getListData(K key) {
        ListOperations<K, V> boundValueOper = redisTemplate.opsForList();
        if (boundValueOper == null) {
            return null;
        }
        //从队列左边取出
        return boundValueOper.leftPop(key);
        //从队列右边取出
        //return boundValueOper.rightPop(key);
    }

    //在队列中取出所有数据并不删除
    public List<V> getListDatasAndNoTrim(K key){
        ListOperations<K,V> boundlistOper=redisTemplate.opsForList();
        return boundlistOper.range(key,0,-1);
    }

    /***********      hash 类型操作           **************/
    //在hash中插入一条数据
    public <HK, HV> void putHashData(K key, HK field, HV value) {
        HashOperations<K, HK, HV> hashOperations = redisTemplate.opsForHash();
        hashOperations.put(key, field, value);
    }
    //判断 hk 是否存在
    public <HK, HV> boolean isExistHash(K key, HK hKey) {
        HashOperations<K, HK, HV> hashOperations = redisTemplate.opsForHash();
        return hashOperations.hasKey(key, hKey);
    }
    //取出所有 values
    public List<V> getHashValue(K key) {
        HashOperations hashOperations = redisTemplate.opsForHash();
        return hashOperations.values(key);
    }
    //取出所有 hks
    public <HK, HV> Set<HK>  getHashHks(K key) {
        HashOperations hashOperations = redisTemplate.opsForHash();
        return hashOperations.keys(key);
    }
    //删除相关的 field
    public Long hdel(K key,Object ... hashKeys ){
        HashOperations hashOperations = redisTemplate.opsForHash();
        return hashOperations.delete(key,hashKeys);
    }

    /***********      set 类型操作           **************/
    //在set中插入一条数据
    public void putSetData(K key, V... value) {
        SetOperations<K, V> boundSetOper = redisTemplate.opsForSet();
        boundSetOper.add(key, value);
    }
    //得到所有 values
    public Set<V>  getSetValues(K key){
        SetOperations<K, V> boundSetOper = redisTemplate.opsForSet();
        return boundSetOper.members(key);
    }
    //删除相关value
    public Long  delSetValuess(K key, V... value){
        SetOperations<K, V> boundSetOper = redisTemplate.opsForSet();
        return boundSetOper.remove(key,value);
    }

    /***********      zset 类型操作           **************/
    //在Zset中插入一条数据
    public void putZSetData(K key, V value ,long score) {
        ZSetOperations<K, V> boundZSetOper = redisTemplate.opsForZSet();
        boundZSetOper.add(key,value,score);

    }
    //得到分数为 score1,score2的值
    public Set<V>  getZSetValues(K key,long score1 ,long score2){
        ZSetOperations<K, V> boundZSetOper = redisTemplate.opsForZSet();
        return boundZSetOper.range(key,score1,score2);
    }
    //删除相关value
    public Long  delZSet(K key, V... value){
        ZSetOperations<K, V> boundZSetOper = redisTemplate.opsForZSet();
        return boundZSetOper.remove(key,value);
    }
    //删除 值排序内的元素
    public Long  delZSetValue(K key, long value1,long value2){
        ZSetOperations<K, V> boundZSetOper = redisTemplate.opsForZSet();
        return boundZSetOper.removeRange(key,value1,value2);
    }
    //删除分数内 元素
    public Long  delZSetByScore(K key, long value1,long value2){
        ZSetOperations<K, V> boundZSetOper = redisTemplate.opsForZSet();
        return boundZSetOper.removeRangeByScore(key,value1,value2);
    }
}
  •  在其他地方引用
@Autowired
private RedisClient redisClient;
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值