package com.lanhai.chronicd.common.util;

import java.util.List;
import java.util.Map;
import java.util.Set;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * 缓存工具类
 *
 * @author liqianxi
 */
public class RedisUtil {

    /**
     * 从缓存获取满足指定模式的Key
     * @param jedisPool
     * @param pattern 查询KEY的模式
     * @return 缓存Key
     */
    public static Set<String> keys(JedisPool jedisPool, String pattern) {
        if (jedisPool == null || pattern == null) {
            return null;
        }

        Jedis jedis = null;
        Set<String> keySet;
        try {
            jedis = jedisPool.getResource();
            keySet =  jedis.keys(pattern);

        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }

        return keySet;
    }

    /**
     * 从缓存获取数据
     * @param jedisPool
     * @param key 缓存KEY
     * @return 缓存数据
     */
    public static String getSingleStringDataFromCache(JedisPool jedisPool, String key) {
        if (jedisPool == null || key == null) {
            return null;
        }

        Jedis jedis = null;
        String data;
        try {
            jedis = jedisPool.getResource();
            data =  jedis.get(key);

        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }

        return data;
    }

    /**
     * 从缓存获取多个数据
     * @param jedisPool
     * @param keys 缓存KEY数组
     * @return 缓存数据
     */
    public static List<String> getMultiStringDataFromCache(JedisPool jedisPool, String... keys) {
        if (jedisPool == null || keys == null || keys.length == 0) {
            return null;
        }

        Jedis jedis = null;
        List<String> data;
        try {
            jedis = jedisPool.getResource();
            data =  jedis.mget(keys);

        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }

        return data;
    }

    /**
     * 向缓存设定数据
     * @param jedisPool
     * @param key 缓存KEY
     * @param value 缓存值
     * @param time 过期时间(秒)
     * @return 缓存数据
     */
    public static void setSingleStringDataToCache(JedisPool jedisPool, String key, String value, int time) {
        if (jedisPool == null || key == null) {
            return;
        }

        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            // 设定新值
            jedis.set(key, value);

            if (time > 0) {
                // 过期时间
                jedis.expire(key, time);
            }

        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }

    /**
     * 向缓存设定数据
     * @param jedisPool
     * @param key 缓存KEY
     * @param value 缓存值
     * @param time 过期时间(秒)
     * @return 缓存数据
     */
    public static void setSingleStringDataToCacheNx(JedisPool jedisPool, String key, String value, int time) {
        if (jedisPool == null || key == null) {
            return;
        }

        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            // 设定新值
            jedis.setnx(key, value);

            if (time > 0) {
                // 过期时间
                jedis.expire(key, time);
            }

        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }

    /**
     * 向缓存设定多个数据
     * @param jedisPool
     * @param keyValues 缓存KEY,VALUE数组
     * @param time 过期时间(秒)
     * @return 缓存数据
     */
    public static void setMultiStringDataToCache(JedisPool jedisPool, String[] keyValues, int time) {
        if (jedisPool == null || keyValues == null) {
            return;
        }

        int length = keyValues.length;
        if (length == 0 || length % 2 != 0) {
            return;
        }

        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            // 设定新值
            jedis.mset(keyValues);

            if (time > 0) {
                for (int i = 0; i < length;) {
                    // 过期时间
                    jedis.expire(keyValues[i], time);
                    i += 2;
                }
            }

        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }

    /**
     * 删除缓存Key
     * @param jedisPool
     * @param keys 缓存Keys
     */
    public static long del(JedisPool jedisPool, String... keys) {
        if (jedisPool == null || keys == null) {
            return 0;
        }

        int length = keys.length;
        if (length == 0) {
            return 0;
        }

        Jedis jedis = null;
        long result;
        try {
            jedis = jedisPool.getResource();
            result = jedis.del(keys);

        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }

        return result;
    }

    /**
     * 缓存数据增加指定值
     * @param key 缓存KEY
     * @return 增加指定值后的值
     */
    public static long incrByIncrement(JedisPool jedisPool, String key, int increment) {
        if (jedisPool == null || key == null) {
            return 0;
        }

        if (increment <= 0) {
            return 0;
        }

        Jedis jedis = null;
        long data = 0;
        try {
            jedis = jedisPool.getResource();
            data =  jedis.incrBy(key, increment);

        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }

        return data;
    }

    /**
     * 缓存数据加1
     * @param key 缓存KEY
     * @return 缓存数据加1的值
     */
    public static long incr(JedisPool jedisPool, String key) {
        return incrByIncrement(jedisPool, key, 1);
    }

    /**
     * Set中添加元素
     * @param jedisPool
     * @param key
     * @param members
     * @return
     */
    public static long saddJedis(JedisPool jedisPool, String key, String... members) {
        if (jedisPool == null || key == null) {
            return 0;
        }

        Jedis jedis = null;
        long data = 0;
        try {
            jedis = jedisPool.getResource();
            data =  jedis.sadd(key, members);

        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
        return data;
    }

    /**
     * Set中的值
     * @param jedisPool
     * @param key
     * @return
     */
    public static Set<String> smembersJedis(JedisPool jedisPool, String key) {
        if (jedisPool == null || key == null) {
            return null;
        }

        Jedis jedis = null;
        Set<String> data = null;
        try {
            jedis = jedisPool.getResource();
            data =  jedis.smembers(key);

        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
        return data;
    }

    /**
     * Set中删除元素
     * @param jedisPool
     * @param key
     * @param members
     * @return
     */
    public static long sremJedis(JedisPool jedisPool, String key, String... members) {
        if (jedisPool == null || key == null) {
            return 0;
        }

        Jedis jedis = null;
        long data = 0;
        try {
            jedis = jedisPool.getResource();
            data =  jedis.srem(key, members);

        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
        return data;
    }

    /**
     * Set中是否已存在
     * @param jedisPool
     * @param key
     * @param members
     * @return
     */
    public static boolean sismemberJedis(JedisPool jedisPool, String key, String members) {
        if (jedisPool == null || key == null) {
            return false;
        }

        Jedis jedis = null;
        boolean data = false;
        try {
            jedis = jedisPool.getResource();
            data =  jedis.sismember(key, members);

        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
        return data;
    }

    /**
     * 设定Hash
     * @param jedisPool
     * @param key 缓存KEY
     * @param hash 值
     * @param time 缓存时间(秒级)
     */
    public static void hmset(JedisPool jedisPool, String key, Map<String, String> hash, int time) {
        if (jedisPool == null || key == null) {
            return;
        }

        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            // 设定新值
            jedis.hmset(key, hash);

            if (time > 0) {
                // 过期时间
                jedis.expire(key, time);
            }

        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }

    /**
     * 设定Hash指定域
     * @param jedisPool
     * @param key 缓存KEY
     * @param field
     * @param value 值
     */
    public static long hset(JedisPool jedisPool, String key, String field, String value) {
        long result = -1;
        if (jedisPool == null || key == null) {
            return result;
        }

        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            // 设定新值
            result = jedis.hset(key, field, value);

        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }

        return result;
    }

    /**
     * 设定Hash指定域
     * @param jedisPool
     * @param key 缓存KEY
     * @param field
     * @param value 值
     */
    public static long hsetnx(JedisPool jedisPool, String key, String field, String value) {
        long result = -1;
        if (jedisPool == null || key == null) {
            return result;
        }

        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            // 设定新值
            result = jedis.hsetnx(key, field, value);

        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }

        return result;
    }

    /**
     * 设定Hash指定域
     * @param jedisPool
     * @param key 缓存KEY
     * @param field
     */
    public static boolean hexists(JedisPool jedisPool, String key, String field) {
        boolean result = false;
        if (jedisPool == null || key == null) {
            return result;
        }

        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            // 设定新值
            result = jedis.hexists(key, field);

        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }

        return result;
    }

    /**
     * 设定Hash指定域
     * @param jedisPool
     * @param key 缓存KEY
     * @param fields
     */
    public static long hdel(JedisPool jedisPool, String key, String... fields) {
        long result = -1;
        if (jedisPool == null || key == null) {
            return result;
        }

        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            // 设定新值
            result = jedis.hdel(key, fields);

        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }

        return result;
    }

    /**
     * 获取Hash
     * @param jedisPool
     * @param key 缓存KEY
     */
    public static Map<String, String> hgetAll(JedisPool jedisPool, String key) {
        Map<String, String> result = null;
        if (jedisPool == null || key == null) {
            return result;
        }

        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            // 设定新值
            result = jedis.hgetAll(key);

        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }

        return result;
    }

    /**
     * 获取Hash
     * @param jedisPool
     * @param key 缓存KEY
     */
    public static String hget(JedisPool jedisPool, String key, String field) {
        String result = null;
        if (jedisPool == null || key == null) {
            return result;
        }

        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            // 设定新值
            result = jedis.hget(key, field);
            if ("nil".equals(result)) {
                result = null;
            }

        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }

        return result;
    }
}

  

posted on 2017-01-18 19:15  辛植  阅读(671)  评论(0编辑  收藏  举报