redis工具类

package com.orchard.common.support.redis;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Slf4j
@Component
public class RedisUtil {
    
    @Autowired
    RedisTemplate redisTemplate;

    public  boolean set(String key, Object value) {
        boolean result = false;
        try {
            ValueOperations<Serializable, Object> operations = this.redisTemplate.opsForValue();
            operations.set(key, value);
            result = true;
        } catch (Exception var5) {
            JSONObject json = new JSONObject();
            String jsonStr =  key + ":" + value.toString();
            json.put("redis设置值异常", jsonStr);
            log.info(json.toJSONString());
        }
        return result;
    }
    
    public boolean set(String key, Object value, Long expireTime) {
        boolean result = false;
        try {
            ValueOperations<Serializable, Object> operations = this.redisTemplate.opsForValue();
            operations.set(key, value);
            this.redisTemplate.expire(key, expireTime.longValue(), TimeUnit.SECONDS);
            result = true;
        } catch (Exception var6) {
            JSONObject json = new JSONObject();
            String jsonStr = key + ":" + value.toString() + "时间戳-" + expireTime ;
            json.put("redis设置时间范围值异常", jsonStr);
            log.info(json.toJSONString());
        }
        
        return result;
    }
    
    public void remove(String... keys) {
        String[] var2 = keys;
        int var3 = keys.length;
        
        for(int var4 = 0; var4 < var3; ++var4) {
            String key = var2[var4];
            this.remove(key);
        }
        
    }
    
    public void removePattern(String pattern) {
        Set<Serializable> keys = this.redisTemplate.keys(pattern);
        if(keys.size() > 0) {
            this.redisTemplate.delete(keys);
        }
        
    }
    
    public void remove(String key) {
        if(this.exists(key)) {
            this.redisTemplate.delete(key);
        }
        
    }
    
    public boolean exists(String key) {
        return this.redisTemplate.hasKey(key).booleanValue();
    }
    
    public Object get(String key) {
        
        Object result = null;
        ValueOperations<Serializable, Object> operations = this.redisTemplate.opsForValue();
        try{
            result = operations.get(key);
        }catch (Exception e ){
            log.error(
                    "从Redis获取数据异常--Date:{},key:{},errorMsg:{}",
                    new Date(),
                    key,
                    e.getMessage());
            return null;
        }
        return result;
    }
    
    public void hmSet(String key, Object hashKey, Object value) {
        HashOperations<String, Object, Object> hash = this.redisTemplate.opsForHash();
        hash.put(key, hashKey, value);
    }
    
    public Object hmGet(String key, Object hashKey) {
        HashOperations<String, Object, Object> hash = this.redisTemplate.opsForHash();
        return hash.get(key, hashKey);
    }
    
    public Set<Object> hmKeys(String key) {
        HashOperations<String, Object, Object> hash = this.redisTemplate.opsForHash();
        return hash.keys(key);
    }

    public Boolean hmhasKey(String key, Object hashKey) {
        HashOperations<String, Object, Object> hash = this.redisTemplate.opsForHash();
        return hash.hasKey(key, hashKey);
    }


    public void hmDel(String masterKey, Object... hashKey) {
        HashOperations<String, Object, Object> hash = this.redisTemplate.opsForHash();
        hash.delete(masterKey, hashKey);
    }
    
    public void lPush(String k, Object v) {
        ListOperations<String, Object> list = this.redisTemplate.opsForList();
        list.rightPush(k, v);
    }

    public void lPush(String k, Object v,long timeout) {
        ListOperations<String, Object> list = this.redisTemplate.opsForList();
        this.redisTemplate.expire(k, timeout, TimeUnit.SECONDS);
        list.rightPush(k, v);
    }

    public List<Object> lRange(String k, long l, long l1) {
        ListOperations<String, Object> list = this.redisTemplate.opsForList();
        return list.range(k, l, l1);
    }

    public Object blpop(String k) {
        ListOperations<String, Object> list = this.redisTemplate.opsForList();
        return list.leftPop(k);
    }


    public Long lLen(String k) {
        ListOperations<String, Object> list = this.redisTemplate.opsForList();
        return list.size(k);
    }
    
    public void add(String key, Object value) {
        SetOperations<String, Object> set = this.redisTemplate.opsForSet();
        set.add(key, value);
    }
    
    public Set<Object> setMembers(String key) {
        SetOperations<String, Object> set = this.redisTemplate.opsForSet();
        return set.members(key);
    }
    
    public void zAdd(String key, Object value, double scoure) {
        ZSetOperations<String, Object> zset = this.redisTemplate.opsForZSet();
        zset.add(key, value, scoure);
    }
    
    public Set<Object> rangeByScore(String key, double scoure, double scoure1) {
        ZSetOperations<String, Object> zset = this.redisTemplate.opsForZSet();
        return zset.rangeByScore(key, scoure, scoure1);
    }
    
    public Object lPop(String k) {
        ListOperations<String, Object> list = this.redisTemplate.opsForList();
        return list.leftPop(k);
    }
    
    public boolean expire(String key, long timeout) {
        return this.redisTemplate.expire(key, timeout, TimeUnit.SECONDS).booleanValue();
    }
    
    public long ttl(String key) {
        return this.redisTemplate.getExpire(key).longValue();
    }

    /**
     * 增量的方式将long值存储在变量中
     * @param key
     * @param num
     * @return
     */
    public Long increment(String key,Integer num) {
        return this.redisTemplate.opsForValue().increment(key,num);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值