Redis缓存工具类

package org.easyrec.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.Response;

import java.util.List;

@Component
public class RedisCacheUtil {

    private static Logger logger = LoggerFactory.getLogger(RedisCacheUtil.class);

    private volatile static RedisCacheUtil redisCacheUtil;

    private static JedisPool jedisPool;


    private RedisCacheUtil() {

        jedisPool = JedisUtils.getJedisCluster();

    }

    /**
     * 创建RedisCacheUtil类对象
     * @return
     */
    public static RedisCacheUtil getInstance() {

        if(redisCacheUtil == null) {
            redisCacheUtil = new RedisCacheUtil();
        }

        return redisCacheUtil;
    }


    /**
     * 通过key值来获取该key值所对应的value
     * @param key
     * @return String
     */
    public String get(String key) {

        Jedis jedis = null;
        try {

            jedis = jedisPool.getResource();

            String result = jedis.get(key);

            return result;
        }catch (Exception e){
            logger.error("=== 读取redis缓存失败 ===");
            e.printStackTrace();
        }finally {
            returnToPool(jedis);
        }

        return null;
    }


    /**
     * 向redis中插入key和对应的value
     * @param key
     * @param value
     * @return String
     */
    public String set(String key, String value) {

        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();

            String result = jedis.set(key, value);

            return result;
        }catch (Exception e){
            logger.error("=== 存入redis缓存失败 ===");
            e.printStackTrace();
        }finally {
            returnToPool(jedis);
        }

        return null;
    }


    /**
     * 向redis中插入key和追加对应的value
     * @param key
     * @param value
     * @return
     */
    public Long lpush(String key, String value) {

        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();

            Long lpush = jedis.lpush(key, value);

            return lpush;
        }catch (Exception e){
            logger.error("=== 存入redis缓存失败 ===");
            e.printStackTrace();
        }finally {
            returnToPool(jedis);
        }
        return null;
    }

    /**
     * 获取管道
     * @return
     */
    public void getPipeline(String key, String value) {

        Jedis jedis = null;
        Pipeline pipelined=null;
        try {
            jedis = jedisPool.getResource();

             pipelined = jedis.pipelined();
             pipelined.lpush(key, value);
        }catch (Exception e){
            logger.error("=== 存入redis缓存失败 ===");
            e.printStackTrace();
        }finally {
            if(pipelined!=null){
                pipelined.sync();
            }
            returnToPool(jedis);

        }
    }

    /**
     * 从右边弹出数据
     * @param key
     * @return
     */
    public String rpop(String key) {

        Jedis jedis = null;
        try {

            jedis = jedisPool.getResource();

            String result = jedis.rpop(key);

            return result;
        }catch (Exception e){
            logger.error("=== 读取redis缓存失败 ===");
            e.printStackTrace();
        }finally {
            returnToPool(jedis);
        }

        return null;
    }

    /**
     * 获取list中所有的数据
     * @param key
     * @return
     */
    public List lrange (String key) {

        Jedis jedis = null;
        try {

            jedis = jedisPool.getResource();

            List<String> stringList = jedis.lrange(key, 0, -1);

            return stringList;
        }catch (Exception e){
            logger.error("=== 读取redis缓存失败 ===");
            e.printStackTrace();
        }finally {
            returnToPool(jedis);
        }

        return null;
    }
    /**
     * 读取redis中list的元素个数
     * @param key
     * @return
     */
    public long listCount(String key) {

        Jedis jedis = null;
        try {

            jedis = jedisPool.getResource();

            Long llen = jedis.llen(key);

            return llen;
        }catch (Exception e){
            logger.error("=== 读取redis缓存失败 ===");
            e.printStackTrace();
        }finally {
            returnToPool(jedis);
        }

        return 0;
    }


    /**
     * 通过hash值和key值查询value
     * @param hkey
     * @param key
     * @return String
     */
    public String hget(String hkey, String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            String result = jedis.hget(hkey, key);
            return result;
        }catch (Exception e){
            logger.error("=== 读取redis缓存失败 ===");
            e.printStackTrace();
        }finally {
            returnToPool(jedis);
        }

        return null;
    }



    /**
     * 向radis中插入hash值,key值和value
     * @param hkey
     * @param key
     * @param value
     * @return Long
     */
    public long hset(String hkey, String key, String value) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            Long result = jedis.hset(hkey, key, value);
            return result;
        }catch (Exception e){
            logger.error("=== redis数据插入失败 ===");
            e.printStackTrace();
        }finally {
            returnToPool(jedis);
        }
        return -1;
    }



    /**
     * 作用:对value加一 计数器
     * @param key
     * @return Long
     */
    public long incr(String key) {
        Jedis jedis = null;

        try {

            jedis = jedisPool.getResource();
            Long result = jedis.incr(key);
            return result;
        }catch (Exception e){
            logger.error("=== value加一失败 ===");
            e.printStackTrace();
        }finally {
            returnToPool(jedis);
        }
        return -1;
    }

    /**
     * 设置key的存在时间
     * @param key
     * @param second
     * @return Long
     */
    public long expire(String key, int second) {
        Jedis jedis = null;

        try {

            jedis = jedisPool.getResource();
            Long result = jedis.expire(key, second);
            return result;
        }catch (Exception e){
            logger.error("=== 设置key的存在时间失败 ===");
            e.printStackTrace();
        }finally {
            returnToPool(jedis);
        }
        return -1;
    }

    /**
     * 显示key的剩余过期时间
     * @param key
     * @return Long
     */
    public long ttl(String key) {
        Jedis jedis = null;

        try {

            jedis = jedisPool.getResource();
            Long result = jedis.ttl(key);
            return result;
        }catch (Exception e){
            logger.error("=== 查询key的剩余时间失败 ===");
            e.printStackTrace();
        }finally {
            returnToPool(jedis);
        }
        return -1;
    }

    /**
     * 通过key删除数据
     * @param key
     * @return Long
     */
    public Long del(String key) {
        Jedis jedis = null;

        try {
            jedis = jedisPool.getResource();
            Long result = jedis.del(key);
            return result;
        }catch (Exception e){
            logger.error("=== 删除redis缓存失败 ===");
            e.printStackTrace();
        }finally {
            returnToPool(jedis);
        }
        return null;

    }

    /**
     * 通过hash值和key值删除数据
     * @param hkey
     * @param key
     * @return Long
     */
    public long hdel(String hkey, String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            Long result = jedis.hdel(hkey, key);
            return result;
        }catch (Exception e){
            logger.error("=== 删除redis缓存失败 ===");
            e.printStackTrace();
        }finally {
            returnToPool(jedis);
        }
        return -1;
    }

    /**
     * 判断key是否存在
     * @param key
     * @return Boolean
     */
    public Boolean exists(String key) {
        Jedis jedis = null;
        try {

            jedis = jedisPool.getResource();
            Boolean result = jedis.exists(key);
            return result;
        }catch (Exception e){
            logger.error("=== 无法查询该key值是否存在 ===");
            e.printStackTrace();
        }finally {
            returnToPool(jedis);
        }
        return false;
    }

    /**
     * 查询redis中key值为输入key值的数据有几条
     * @param keys
     * @return Long
     */
    public Long exists(String... keys) {
        Jedis jedis = null;

        try {

            jedis = jedisPool.getResource();
            Long result = jedis.exists(keys);
            return result;
        }catch (Exception e){
            logger.error("=== 无法查询数据数量 ===");
            e.printStackTrace();
        }finally {
            returnToPool(jedis);
        }
        return null;
    }


    private void returnToPool(Jedis jedis) {
        if(jedis != null) {
            jedis.close();
        }
    }


}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章