代码改变世界

spring+redis整合

2018-03-20 10:41  sihao560  阅读(548)  评论(0编辑  收藏  举报

redis整合,之前想要bean注入,但是因为框架太老,未成功,查找很多资料最终使用创建公共方法的方式使用redis

不多说,直接上代码。

pom

    <!-- redis -->
        <dependency>
            <groupId>org.springframework.data</groupId>        
            <artifactId>spring-data-redis</artifactId>        
            <version>1.6.0.RELEASE</version>        
        </dependency>     
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.7.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
            <version>2.3</version>
        </dependency>        

 

 

redis.properties

#redis\u5730\u5740
redis.host=127.0.0.1
#redis\u7AEF\u53E3\u53F7
redis.port=6379
#redis\u7684\u5BC6\u7801
redis.auth=123456

#\u662F\u5426\u4F7F\u7528JedisPool\u9ED8\u8BA4\u7684\u914D\u7F6E,\u786E\u5B9Atrue,\u9ED8\u8BA4true
defaultSetting=false;
#jedisPool\u7684timeout\u65F6\u95F4,\u9ED8\u8BA42000
connectionTimeOut=2000
#\u8FDE\u63A5\u8017\u5C3D\u65F6\u662F\u5426\u963B\u585E, false\u62A5\u5F02\u5E38,ture\u963B\u585E\u76F4\u5230\u8D85\u65F6, \u9ED8\u8BA4true
redis.blockWhenExhausted=true
#\u8BBE\u7F6E\u7684\u9010\u51FA\u7B56\u7565\u7C7B\u540D, \u9ED8\u8BA4DefaultEvictionPolicy(\u5F53\u8FDE\u63A5\u8D85\u8FC7\u6700\u5927\u7A7A\u95F2\u65F6\u95F4,\u6216\u8FDE\u63A5\u6570\u8D85\u8FC7\u6700\u5927\u7A7A\u95F2\u8FDE\u63A5\u6570)
redis.evictionPolicyClassName=org.apache.commons.pool2.impl.DefaultEvictionPolicy
#\u662F\u5426\u542F\u7528pool\u7684jmx\u7BA1\u7406\u529F\u80FD, \u9ED8\u8BA4true
redis.jmxEnabled=true
#\u662F\u5426\u542F\u7528\u540E\u8FDB\u5148\u51FA, \u9ED8\u8BA4true
redis.lifo=true
#\u6700\u5927\u7A7A\u95F2\u8FDE\u63A5\u6570, \u9ED8\u8BA48\u4E2A
redis.maxIdle=100
#\u6700\u5927\u8FDE\u63A5\u6570, \u9ED8\u8BA48\u4E2A
redis.maxTotal=300
#\u83B7\u53D6\u8FDE\u63A5\u65F6\u7684\u6700\u5927\u7B49\u5F85\u6BEB\u79D2\u6570(\u5982\u679C\u8BBE\u7F6E\u4E3A\u963B\u585E\u65F6BlockWhenExhausted),\u5982\u679C\u8D85\u65F6\u5C31\u629B\u5F02\u5E38, \u5C0F\u4E8E\u96F6:\u963B\u585E\u4E0D\u786E\u5B9A\u7684\u65F6\u95F4,\u9ED8\u8BA4-1
redis.maxWaitMillis=-1
#\u9010\u51FA\u8FDE\u63A5\u7684\u6700\u5C0F\u7A7A\u95F2\u65F6\u95F4 \u9ED8\u8BA41800000\u6BEB\u79D2(30\u5206\u949F)
redis.minEvictableIdleTimeMillis=1800000
#\u6700\u5C0F\u7A7A\u95F2\u8FDE\u63A5\u6570, \u9ED8\u8BA40
redis.minIdle=0
#\u6BCF\u6B21\u9010\u51FA\u68C0\u67E5\u65F6 \u9010\u51FA\u7684\u6700\u5927\u6570\u76EE \u5982\u679C\u4E3A\u8D1F\u6570\u5C31\u662F : 1/abs(n), \u9ED8\u8BA43
redis.numTestsPerEvictionRun=3
#\u5BF9\u8C61\u7A7A\u95F2\u591A\u4E45\u540E\u9010\u51FA, \u5F53\u7A7A\u95F2\u65F6\u95F4>\u8BE5\u503C \u4E14 \u7A7A\u95F2\u8FDE\u63A5>\u6700\u5927\u7A7A\u95F2\u6570 \u65F6\u76F4\u63A5\u9010\u51FA,\u4E0D\u518D\u6839\u636EMinEvictableIdleTimeMillis\u5224\u65AD(\u9ED8\u8BA4\u9010\u51FA\u7B56\u7565) 
redis.softMinEvictableIdleTimeMillis=1800000
#\u5728\u83B7\u53D6\u8FDE\u63A5\u7684\u65F6\u5019\u68C0\u67E5\u6709\u6548\u6027, \u9ED8\u8BA4false
redis.testOnBorrow=false
#\u5728\u7A7A\u95F2\u65F6\u68C0\u67E5\u6709\u6548\u6027, \u9ED8\u8BA4false
redis.testWhileIdle=false
#\u9010\u51FA\u626B\u63CF\u7684\u65F6\u95F4\u95F4\u9694(\u6BEB\u79D2) \u5982\u679C\u4E3A\u8D1F\u6570,\u5219\u4E0D\u8FD0\u884C\u9010\u51FA\u7EBF\u7A0B, \u9ED8\u8BA4-1
redus.timeBetweenEvictionRunsMillis=-1

JedisPoolUtil

package com.ly.zx.util.redis;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;

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

public class JedisPoolUtil {
    private static final String PROPERTIES_PATH = "redis.properties";  
    private static JedisPool jedisPool;  
  
      
    static {  
        if (jedisPool == null) {  
            try {  
                init();  
            } catch (FileNotFoundException e) {  
                e.printStackTrace();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
    }  
  
    /** 
     * 初始化Jedis连接池 
     *  
     * @throws IOException 
     */  
    private static void init() throws IOException {  
        URL resource = JedisPoolUtil.class.getClassLoader().getResource(PROPERTIES_PATH);  
        if (resource == null) {  
            throw new FileNotFoundException("没有找到指定redis的配置文件:" + PROPERTIES_PATH);  
        }  
        //加载配置文件  
        InputStream input = new FileInputStream(resource.getFile());  
        Properties p = new Properties();  
        p.load(input);  
        //开始配置JedisPool  
        String host = p.getProperty("redis.host") == null ? "localhost" : p.getProperty("redis.host");  
        int port = p.getProperty("redis.port") == null ? 6379 : Integer.parseInt(p.getProperty("redis.port"));  
        String auth = p.getProperty("redis.auth");  
        int poolTimeOut = p.getProperty("connectionTimeOut") == null ? 2000  
                : Integer.parseInt(p.getProperty("connectionTimeOut"));  
        //判断使用默认的配置方式还是采用自定义配置方式,  
        boolean isSetDefault = p.getProperty("defaultSetting") == null ? true  
                : Boolean.parseBoolean(p.getProperty("defaultSetting"));  
        if (isSetDefault) {  
            jedisPool = new JedisPool(new GenericObjectPoolConfig(), host, port, poolTimeOut, auth);  
        } else {  
            JedisPoolConfig config = new JedisPoolConfig();  
            String blockWhenExhausted = p.getProperty("redis.blockWhenExhausted");  
            if (blockWhenExhausted != null) {  
                config.setBlockWhenExhausted(Boolean.parseBoolean(blockWhenExhausted));  
            }  
            String evictionPolicyClassName = p.getProperty("redis.evictionPolicyClassName");  
            if (evictionPolicyClassName != null) {  
                config.setEvictionPolicyClassName(evictionPolicyClassName);  
            }  
            String jmxEnabled = p.getProperty("redis.jmxEnabled");  
            if (jmxEnabled != null) {  
                config.setJmxEnabled(Boolean.parseBoolean(jmxEnabled));  
            }  
            String lifo = p.getProperty("redis.lifo");  
            if (lifo != null) {  
                config.setLifo(Boolean.parseBoolean(lifo));  
            }  
            String maxIdle = p.getProperty("redis.maxIdle");  
            if (maxIdle != null) {  
                config.setMaxIdle(Integer.parseInt(maxIdle));  
            }  
            String maxTotal = p.getProperty("redis.maxTotal");  
            if (maxTotal != null) {  
                config.setMaxTotal(Integer.parseInt(maxTotal));  
            }  
            String maxWaitMillis = p.getProperty("redis.maxWaitMillis");  
            if (maxWaitMillis != null) {  
                config.setMaxWaitMillis(Long.parseLong(maxWaitMillis));  
            }  
            String minEvictableIdleTimeMillis = p.getProperty("redis.minEvictableIdleTimeMillis");  
            if (minEvictableIdleTimeMillis != null) {  
                config.setMinEvictableIdleTimeMillis(Long.parseLong(minEvictableIdleTimeMillis));  
            }  
            String minIdle = p.getProperty("redis.minIdle");  
            if (minIdle != null) {  
                config.setMinIdle(Integer.parseInt(minIdle));  
            }  
            String numTestsPerEvictionRun = p.getProperty("redis.numTestsPerEvictionRun");  
            if (numTestsPerEvictionRun != null) {  
                config.setNumTestsPerEvictionRun(Integer.parseInt(numTestsPerEvictionRun));  
            }  
            String softMinEvictableIdleTimeMillis = p.getProperty("redis.softMinEvictableIdleTimeMillis");  
            if (softMinEvictableIdleTimeMillis != null) {  
                config.setSoftMinEvictableIdleTimeMillis(Long.parseLong(softMinEvictableIdleTimeMillis));  
            }  
            String testOnBorrow = p.getProperty("redis.testOnBorrow");  
            if (testOnBorrow != null) {  
                config.setTestOnBorrow(Boolean.parseBoolean(testOnBorrow));  
            }  
            String testWhileIdle = p.getProperty("redis.testWhileIdle");  
            if (testWhileIdle != null) {  
                config.setTestWhileIdle(Boolean.parseBoolean(testWhileIdle));  
            }  
            String timeBetweenEvictionRunsMillis = p.getProperty("redus.timeBetweenEvictionRunsMillis");  
            if (timeBetweenEvictionRunsMillis != null) {  
                config.setTimeBetweenEvictionRunsMillis(Long.parseLong(timeBetweenEvictionRunsMillis));  
            }  
            jedisPool = new JedisPool(config, host, port, poolTimeOut, auth);  
        }  
  
    }  
  
    public static Jedis getJedis() {  
        return jedisPool.getResource();  
    }  
  
    public static void closeJedis(Jedis jedis) {  
        if (jedis != null) {  
            jedis.close();  
        }  
    }  
}

RedisUtil

package com.ly.zx.util.redis;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.exceptions.JedisDataException;

import com.alibaba.fastjson.JSON;

public class RedisUtil {
    private static final int DEFAULT_SETEX_TIMEOUT = 60 * 60;// setex的默认时间  
      
    /** 
     * 添加一个字符串值,成功返回1,失败返回0 
     *  
     * @param key 
     * @param value 
     * @return 
     */  
    public static int set(String key, String value) {  
        if (isValueNull(key, value)) {  
            return 0;  
        }  
        Jedis jedis = null;  
        try {  
            jedis = JedisPoolUtil.getJedis();  
            if (jedis.set(key, value).equalsIgnoreCase("ok")) {  
                return 1;  
            } else {  
                return 0;  
            }  
        } finally {  
            JedisPoolUtil.closeJedis(jedis);  
        }  
  
    }  
  
    /** 
     * 缓存一个字符串值,成功返回1,失败返回0,默认缓存时间为1小时,以本类的常量DEFAULT_SETEX_TIMEOUT为准 
     *  
     * @param key 
     * @param value 
     * @return 
     */  
    public static int setEx(String key, String value) {  
        if (isValueNull(key, value)) {  
            return 0;  
        }  
        Jedis jedis = null;  
        try {  
            jedis = JedisPoolUtil.getJedis(); 
            
            if (jedis.setex(key, DEFAULT_SETEX_TIMEOUT, value).equalsIgnoreCase("ok")) {  
                return 1;  
            } else {  
                return 0;  
            }  
        } finally {  
            JedisPoolUtil.closeJedis(jedis);  
        }  
    }  
    /** 
     * 判断key是否存在
     *  
     * @param key 
     * @param value 
     * @return 
     */  
    public static boolean keyExists(String key) {
        if (isValueNull(key)) {  
            return false;  
        }  
        Jedis jedis = null;  
        try {  
            jedis = JedisPoolUtil.getJedis(); 
            return jedis.exists(key); 
        }catch(Exception e){
            return false;
        }finally {  
            JedisPoolUtil.closeJedis(jedis);  
        }  
    }  
  
    /** 
     * 缓存一个字符串值,成功返回1,失败返回0,缓存时间以timeout为准,单位秒 
     *  
     * @param key 
     * @param value 
     * @param timeout 
     * @return 
     */  
    public static int setEx(String key, String value, int timeout) {  
        if (isValueNull(key, value)) {  
            return 0;  
        }  
        Jedis jedis = null;  
        try {  
            jedis = JedisPoolUtil.getJedis();  
            if (jedis.setex(key, timeout, value).equalsIgnoreCase("ok")) {  
                return 1;  
            } else {  
                return 0;  
            }  
        } finally {  
            JedisPoolUtil.closeJedis(jedis);  
        }  
    }  
  
    /** 
     * 添加一个指定类型的对象,成功返回1,失败返回0 
     *  
     * @param key 
     * @param value 
     * @return 
     */  
    public static <T> int set(String key, T value) {  
        if (isValueNull(key, value)) {  
            return 0;  
        }  
        Jedis jedis = null;  
        try {  
            jedis = JedisPoolUtil.getJedis();  
            byte[] data = SerializeUtils.serialize(value);  
            if (jedis.set(key.getBytes(), data).equalsIgnoreCase("ok")) {  
                return 1;  
            } else {  
                return 0;  
            }  
        } finally {  
            JedisPoolUtil.closeJedis(jedis);  
        }  
    }  
  
    /** 
     * 缓存一个指定类型的对象,成功返回1,失败返回0,默认缓存时间为1小时,以本类的常量DEFAULT_SETEX_TIMEOUT为准 
     *  
     * @param key 
     * @param value 
     * @return 
     */  
    public static <T> int setEx(String key, T value) {  
        if (isValueNull(key, value)) {  
            return 0;  
        }  
        Jedis jedis = null;  
        try {  
            jedis = JedisPoolUtil.getJedis();  
            byte[] data = SerializeUtils.serialize(value);  
            if (jedis.setex(key.getBytes(), DEFAULT_SETEX_TIMEOUT, data).equalsIgnoreCase("ok")) {  
                return 1;  
            } else {  
                return 0;  
            }  
        } finally {  
            JedisPoolUtil.closeJedis(jedis);  
        }  
    }  
  
    /** 
     * 缓存一个指定类型的对象,成功返回1,失败返回0,缓存时间以timeout为准,单位秒 
     *  
     * @param key 
     * @param value 
     * @param timeout 
     * @return 
     */  
    public static <T> int setEx(String key, T value, int timeout) {  
        if (isValueNull(key, value)) {  
            return 0;  
        }  
        Jedis jedis = null;  
        try {  
            jedis = JedisPoolUtil.getJedis();  
            byte[] data = SerializeUtils.serialize(value);  
            if (jedis.setex(key.getBytes(), timeout, data).equalsIgnoreCase("ok")) {  
                return 1;  
            } else {  
                return 0;  
            }  
        } finally {  
            JedisPoolUtil.closeJedis(jedis);  
        }  
    }  
  
    /** 
     * 将一个数值+1,成功返回+后的结果,失败返回null 
     *  
     * @param key 
     * @return 
     * @throws JedisDataException 
     */  
    public static Long incr(String key) throws JedisDataException {  
        if (isValueNull(key)) {  
            return null;  
        }  
        Jedis jedis = null;  
        try {  
            jedis = JedisPoolUtil.getJedis();  
            return jedis.incr(key);  
        } finally {  
            JedisPoolUtil.closeJedis(jedis);  
        }  
    }  
  
    /** 
     * 将一个数值-1,成功返回-后的结果,失败返回null 
     *  
     * @param key 
     * @return 
     * @throws JedisDataException 
     */  
    public static Long decr(String key) throws JedisDataException {  
        if (isValueNull(key)) {  
            return null;  
        }  
        Jedis jedis = null;  
        try {  
            jedis = JedisPoolUtil.getJedis();  
            return jedis.decr(key);  
        } finally {  
            JedisPoolUtil.closeJedis(jedis);  
        }  
    }  
  
    /** 
     * 添加一个字符串值到list中,,成功返回1,失败返回0 
     *  
     * @param key 
     * @param value 
     * @return 
     */  
    public static int setList(String key, String... value) {  
        if (isValueNull(key, value)) {  
            return 0;  
        }  
        Jedis jedis = null;  
        try {  
            jedis = JedisPoolUtil.getJedis();  
            Long result = jedis.rpush(key, value);  
            if (result != null && result != 0) {  
                return 1;  
            } else {  
                return 0;  
            }  
        } finally {  
            JedisPoolUtil.closeJedis(jedis);  
        }  
    }  
  
    /** 
     * 缓存一个字符串值到list中,全部list的key默认缓存时间为1小时,成功返回1,失败返回0 
     *  
     * @param key 
     * @param value 
     * @return 
     */  
    public static int setExList(String key, String... value) {  
        if (isValueNull(key, value)) {  
            return 0;  
        }  
        Jedis jedis = null;  
        try {  
            jedis = JedisPoolUtil.getJedis();  
            Long result = jedis.rpush(key, value);  
            jedis.expire(key, DEFAULT_SETEX_TIMEOUT);  
            if (result != null && result != 0) {  
                return 1;  
            } else {  
                return 0;  
            }  
  
        } finally {  
            JedisPoolUtil.closeJedis(jedis);  
        }  
    }  
  
    /** 
     * 缓存一个字符串值到list中,全部list的key缓存时间为timeOut,单位为秒,成功返回1,失败返回0 
     *  
     * @param key 
     * @param value 
     * @return 
     */  
    public static int setExList(String key, int timeOut, String... value) {  
        if (isValueNull(key, value)) {  
            return 0;  
        }  
        Jedis jedis = null;  
        try {  
            jedis = JedisPoolUtil.getJedis();  
            Long result = jedis.rpush(key, value);  
            jedis.expire(key, timeOut);  
            if (result != null && result != 0) {  
                return 1;  
            } else {  
                return 0;  
            }  
  
        } finally {  
            JedisPoolUtil.closeJedis(jedis);  
        }  
    }  
  
    /** 
     * 添加一个<T>类型对象值到list中,成功返回1,失败返回0 
     *  
     * @param key 
     * @param value 
     * @return 
     */  
    public static <T> int setList(String key, T... value) {  
        if (isValueNull(key, value)) {  
            return 0;  
        }  
        Jedis jedis = null;  
        try {  
            jedis = JedisPoolUtil.getJedis();  
            int res = 0;  
            for (T t : value) {  
                byte[] data = SerializeUtils.serialize(t);  
                Long result = jedis.rpush(key.getBytes(), data);  
                if (result != null && result != 0) {  
                    res++;  
                }  
            }  
            if (res != 0) {  
                return 1;  
            } else {  
                return 0;  
            }  
        } finally {  
            JedisPoolUtil.closeJedis(jedis);  
        }  
    }  
  
    /** 
     * 缓存一个<T>类型对象值到list中,全部list的key默认缓存时间为1小时,成功返回1,失败返回0 
     *  
     * @param key 
     * @param value 
     * @return 
     */  
    
    public static <T> int setExList(String key, T... value) {  
        if (isValueNull(key, value)) {  
            return 0;  
        }  
        Jedis jedis = null;  
        try {  
            jedis = JedisPoolUtil.getJedis();  
            int res = 0;  
            for (T t : value) {  
                byte[] data = SerializeUtils.serialize(t);  
                Long result = jedis.rpush(key.getBytes(), data);  
                if (result != null && result != 0) {  
                    res++;  
                }  
            }  
            jedis.expire(key, DEFAULT_SETEX_TIMEOUT);  
            if (res != 0) {  
                return 1;  
            } else {  
                return 0;  
            }  
        } finally {  
            JedisPoolUtil.closeJedis(jedis);  
        }  
    }  
  
    /** 
     * 缓存一个<T>类型对象值到list中,全部list的key缓存时间为timeOut,单位秒,成功返回1,失败返回0 
     *  
     * @param key 
     * @param value 
     * @return 
     */  
    
    public static <T> int setExList(String key, int timeOut, T... value) {  
        if (isValueNull(key, value)) {  
            return 0;  
        }  
        Jedis jedis = null;  
        try {  
            jedis = JedisPoolUtil.getJedis();  
            int res = 0;  
            for (T t : value) {  
                byte[] data = SerializeUtils.serialize(t);  
                Long result = jedis.rpush(key.getBytes(), data);  
                if (result != null && result != 0) {  
                    res++;  
                }  
            }  
            jedis.expire(key, timeOut);  
            if (res != 0) {  
                return 1;  
            } else {  
                return 0;  
            }  
        } finally {  
            JedisPoolUtil.closeJedis(jedis);  
        }  
    }  
  
    /** 
     * 添加一个List集合成功返回1,失败返回0 
     *  
     * @param key 
     * @param value 
     * @return 
     * @throws IOException 
     * @throws RuntimeException 
     */  
    public static <T> int setList(String key, List<T> value) throws RuntimeException, IOException {  
        if (isValueNull(key, value)) {  
            return 0;  
        }  
        Jedis jedis = null;  
        try {  
            jedis = JedisPoolUtil.getJedis();  
            byte[] data = SerializeUtils.serialize(value);  
            if (jedis.set(key.getBytes(), data).equalsIgnoreCase("ok")) {  
                return 1;  
            } else {  
                return 0;  
            }  
        } finally {  
            JedisPoolUtil.closeJedis(jedis);  
        }  
    }  
  
    /** 
     * 缓存一个List<T>集合,成功返回1,失败返回0,默认缓存时间为1小时,以本类的常量DEFAULT_SETEX_TIMEOUT为准 
     *  
     * @param key 
     * @param value 
     * @return 
     * @throws IOException 
     * @throws RuntimeException 
     */  
  
    public static <T> int setExList(String key, List<T> value) {  
        if (isValueNull(key, value)) {  
            return 0;  
        }  
        Jedis jedis = null;  
        try {  
            jedis = JedisPoolUtil.getJedis();  
            byte[] data = SerializeUtils.serialize(value);  
            if (jedis.setex(key.getBytes(), DEFAULT_SETEX_TIMEOUT, data).equalsIgnoreCase("ok")) {  
                return 1;  
            } else {  
                return 0;  
            }  
        }catch(Exception e){
            return 0;  
        }    finally {  
        
            JedisPoolUtil.closeJedis(jedis);  
        }  
    }  
  
    /** 
     * 缓存一个List<T>集合,成功返回1,失败返回0,缓存时间以timeout为准,单位秒 
     *  
     * @param key 
     * @param value 
     * @param timeout 
     * @return 
     * @throws IOException 
     * @throws RuntimeException 
     */  
    public static <T> int setExList(String key, List<T> value, int timeout) throws RuntimeException, IOException {  
        if (isValueNull(key, value)) {  
            return 0;  
        }  
        Jedis jedis = null;  
        try {  
            jedis = JedisPoolUtil.getJedis();  
            byte[] data = SerializeUtils.serialize(value);  
            if (jedis.setex(key.getBytes(), timeout, data).equalsIgnoreCase("ok")) {  
                return 1;  
            } else {  
                return 0;  
            }  
        } finally {  
            JedisPoolUtil.closeJedis(jedis);  
        }  
    }  
  
    /** 
     * 添加一个字符串到set,如果key存在就在就最追加,如果key不存在就创建,成功返回1,失败或者没有受影响返回0 
     *  
     * @param key 
     * @param value 
     * @return 
     */  
    public static int setSet(String key, String... value) {  
        if (isValueNull(key, value)) {  
            return 0;  
        }  
        Jedis jedis = null;  
        try {  
            jedis = JedisPoolUtil.getJedis();  
            Long result = jedis.sadd(key, value);  
            if (result != null && result != 0) {  
                return 1;  
            } else {  
                return 0;  
            }  
        } finally {  
            JedisPoolUtil.closeJedis(jedis);  
        }  
    }  
  
    /** 
     * 添加一个字符串set,如果key存在就在就最追加,整个set的key默认一小时后过期,如果key存在就在可以种继续添加,如果key不存在就创建,成功返回1,失败或者没有受影响返回0 
     *  
     * @param key 
     * @param value 
     * @return 
     */  
    public static int setExSet(String key, String... value) {  
        if (isValueNull(key, value)) {  
            return 0;  
        }  
        Jedis jedis = null;  
        try {  
            jedis = JedisPoolUtil.getJedis();  
            Long result = jedis.sadd(key, value);  
            jedis.expire(key, DEFAULT_SETEX_TIMEOUT);  
            if (result != null && result != 0) {  
                return 1;  
            } else {  
                return 0;  
            }  
        } finally {  
            JedisPoolUtil.closeJedis(jedis);  
        }  
    }  
  
    /** 
     * 添加一个字符串set,如果key存在就在就最追加,整个set的key有效时间为timeOut时间,单位秒,如果key存在就在可以种继续添加,如果key不存在就创建,,成功返回1,失败或者没有受影响返回0 
     *  
     * @param key 
     * @param value 
     * @return 
     */  
    public static int setExSet(String key, int timeOut, String... value) {  
        if (isValueNull(key, value)) {  
            return 0;  
        }  
        Jedis jedis = null;  
        try {  
            jedis = JedisPoolUtil.getJedis();  
            Long result = jedis.sadd(key, value);  
            jedis.expire(key, timeOut);  
            if (result != null && result != 0) {  
                return 1;  
            } else {  
                return 0;  
            }  
        } finally {  
            JedisPoolUtil.closeJedis(jedis);  
        }  
    }  
  
    /** 
     * 添加一个<T>类型到set集合,如果key存在就在就最追加,成功返回1,失败或者没有受影响返回0 
     *  
     * @param key 
     * @param value 
     * @return 
     */  
    
    public static <T> int setSet(String key, T... value) {  
        if (isValueNull(key, value)) {  
            return 0;  
        }  
        Jedis jedis = null;  
        try {  
            jedis = JedisPoolUtil.getJedis();  
            int res = 0;  
            for (T t : value) {  
                byte[] data = SerializeUtils.serialize(t);  
                Long result = jedis.sadd(key.getBytes(), data);  
                if (result != null && result != 0) {  
                    res++;  
                }  
            }  
            if (res != 0) {  
                return 1;  
            } else {  
                return 0;  
            }  
        } finally {  
            JedisPoolUtil.closeJedis(jedis);  
        }  
    }  
  
    /** 
     * 缓存一个<T>类型到set集合,如果key存在就在就最追加,整个set的key默认有效时间为1小时,成功返回1,失败或者没有受影响返回0 
     *  
     * @param key 
     * @param value 
     * @return 
     */  
    
    public static <T> int setExSet(String key, T... value) {  
        if (isValueNull(key, value)) {  
            return 0;  
        }  
        Jedis jedis = null;  
        try {  
            jedis = JedisPoolUtil.getJedis();  
            int res = 0;  
            for (T t : value) {  
                byte[] data = SerializeUtils.serialize(t);  
                Long result = jedis.sadd(key.getBytes(), data);  
                if (result != null && result != 0) {  
                    res++;  
                }  
            }  
            jedis.expire(key, DEFAULT_SETEX_TIMEOUT);  
            if (res != 0) {  
                return 1;  
            } else {  
                return 0;  
            }  
        } finally {  
            JedisPoolUtil.closeJedis(jedis);  
        }  
    }  
  
    /** 
     * 缓存一个<T>类型到set集合,如果key存在就在就最追加,整个set的key有效时间为timeOut,单位秒,成功返回1,失败或者没有受影响返回0 
     *  
     * @param key 
     * @param value 
     * @return 
     */  
    
    public static <T> int setExSet(String key, int timeOut, T... value) {  
        if (isValueNull(key, value)) {  
            return 0;  
        }  
        Jedis jedis = null;  
        try {  
            jedis = JedisPoolUtil.getJedis();  
            int res = 0;  
            for (T t : value) {  
                byte[] data = SerializeUtils.serialize(t);  
                Long result = jedis.sadd(key.getBytes(), data);  
                if (result != null && result != 0) {  
                    res++;  
                }  
            }  
            jedis.expire(key, timeOut);  
            if (res != 0) {  
                return 1;  
            } else {  
                return 0;  
            }  
        } finally {  
            JedisPoolUtil.closeJedis(jedis);  
        }  
    }  
  
    /** 
     * 添加一个Map<K, V>集合,成功返回1,失败返回0 
     *  
     * @param key 
     * @param value 
     * @param timeout 
     * @return 
     */  
    public static <K, V> int setMap(String key, Map<K, V> value) {  
        if (value == null || key == null || "".equals(key)) {  
            return 0;  
        }  
        Jedis jedis = null;  
        try {  
            jedis = JedisPoolUtil.getJedis();  
            String data = JSON.toJSONString(value);  
            if (jedis.set(key, data).equalsIgnoreCase("ok")) {  
                return 1;  
            } else {  
                return 0;  
            }  
        } finally {  
            JedisPoolUtil.closeJedis(jedis);  
        }  
    }  
  
    /** 
     * 缓存一个Map<K, V>集合,成功返回1,失败返回0,默认缓存时间为1小时,以本类的常量DEFAULT_SETEX_TIMEOUT为准 
     *  
     * @param key 
     * @param value 
     * @param timeout 
     * @return 
     */  
    public static <K, V> int setExMap(String key, Map<K, V> value) {  
        if (value == null || key == null || "".equals(key)) {  
            return 0;  
        }  
        Jedis jedis = null;  
        try {  
            jedis = JedisPoolUtil.getJedis();  
            String data = JSON.toJSONString(value);  
            if (jedis.setex(key, DEFAULT_SETEX_TIMEOUT, data).equalsIgnoreCase("ok")) {  
                return 1;  
            } else {  
                return 0;  
            }  
        } finally {  
            JedisPoolUtil.closeJedis(jedis);  
        }  
    }  
  
    /** 
     * 缓存一个Map<K, V>集合,成功返回1,失败返回0,缓存时间以timeout为准,单位秒 
     *  
     * @param key 
     * @param value 
     * @param timeout 
     * @return 
     */  
    public static <K, V> int setExMap(String key, Map<K, V> value, int timeout) {  
        if (value == null || key == null || "".equals(key)) {  
            return 0;  
        }  
        Jedis jedis = null;  
        try {  
            jedis = JedisPoolUtil.getJedis();  
            String data = JSON.toJSONString(value);  
            if (jedis.setex(key, timeout, data).equalsIgnoreCase("ok")) {  
                return 1;  
            } else {  
                return 0;  
            }  
        } finally {  
            JedisPoolUtil.closeJedis(jedis);  
        }  
    }  
  
    /** 
     * 获取一个字符串值 
     *  
     * @param key 
     * @return 
     */  
    public static String get(String key) {  
        if (isValueNull(key)) {  
            return null;  
        }  
        Jedis jedis = null;  
        try {  
            jedis = JedisPoolUtil.getJedis();  
            return jedis.get(key);  
        } finally {  
            JedisPoolUtil.closeJedis(jedis);  
        }  
    }  
  
    /** 
     * 获得一个指定类型的对象 
     *  
     * @param key 
     * @param value 
     * @return 
     */  
    public static <T> T get(String key, Class<T> clazz) {  
        if (isValueNull(key)) {  
            return null;  
        }  
        Jedis jedis = null;  
        try {  
            jedis = JedisPoolUtil.getJedis();  
  
            byte[] data = jedis.get(key.getBytes());  
            T result = (T) SerializeUtils.unserialize(data);  
            return result;  
        } finally {  
            JedisPoolUtil.closeJedis(jedis);  
        }  
    }  
  
    /** 
     * 获得一个字符串集合,区间以偏移量 START 和 END 指定。 其中 0 表示列表的第一个元素, 1 
     * 表示列表的第二个元素,以此类推。 你也可以使用负数下标,以 -1 表示列表的最后一个元素, -2 表示列表的倒数第二个元素,以此类推。 
     *  
     * @param key 
     * @param start 
     * @param end 
     * @return 
     */  
    public static List<String> getList(String key, long start, long end) {  
        if (isValueNull(key)) {  
            return null;  
        }  
        Jedis jedis = null;  
        try {  
            jedis = JedisPoolUtil.getJedis();  
            List<String> result = jedis.lrange(key, start, end);  
            return result;  
        } finally {  
            JedisPoolUtil.closeJedis(jedis);  
        }  
    }  
  
    /** 
     * 获得一个<T>类型的对象集合,区间以偏移量 START 和 END 指定。 其中 0 表示列表的第一个元素, 1 表示列表的第二个元素,以此类推。 
     * 你也可以使用负数下标,以 -1 表示列表的最后一个元素, -2 表示列表的倒数第二个元素,以此类推。 
     *  
     * @param key 
     * @param start 
     * @param end 
     * @return 
     */  
    public static <T> List<T> getList(String key, long start, long end, Class<T> clazz) {  
        if (isValueNull(key)) {  
            return null;  
        }  
        Jedis jedis = null;  
        try {  
            jedis = JedisPoolUtil.getJedis();  
            List<byte[]> lrange = jedis.lrange(key.getBytes(), start, end);  
            List<T> result = null;  
            if (lrange != null) {  
                for (byte[] data : lrange) {  
                    if (result == null) {  
                        result = new ArrayList<T>();  
                    }  
                    result.add((T) SerializeUtils.unserialize(data));  
                }  
            }  
            return result;  
        } finally {  
            JedisPoolUtil.closeJedis(jedis);  
        }  
    }  
  
    /** 
     * 获得list中存了多少个值 
     *  
     * @return 
     */  
    public static long getListCount(String key) {  
        if (isValueNull(key)) {  
            return 0;  
        }  
        Jedis jedis = null;  
        try {  
            jedis = JedisPoolUtil.getJedis();  
            return jedis.llen(key);  
        } finally {  
            JedisPoolUtil.closeJedis(jedis);  
        }  
    }  
  
    /** 
     * 获得一个List<T>的集合, 
     *  
     * @param key 
     *            键 
     * @param clazz 
     *            返回集合的类型 
     * @return 
     * @return 
     * @throws IOException 
     */  
    public static <T> T getList(String key, Class<?> clazz) {  
        if (isValueNull(key)) {  
            return null;  
        }  
        Jedis jedis = null;  
        try {  
            jedis = JedisPoolUtil.getJedis();  
            byte[] data = jedis.get(key.getBytes()); 

            T result = (T) SerializeUtils.unserialize(data);

            return result;  
        }catch (Exception e){
            return null;
        }    finally {  
        
            JedisPoolUtil.closeJedis(jedis);  
        }  
    }  
  
    /** 
     * 获得一个字符串set集合 
     *  
     * @param key 
     * @return 
     */  
    public static Set<String> getSet(String key) {  
        if (isValueNull(key)) {  
            return null;  
        }  
        Jedis jedis = null;  
        try {  
            jedis = JedisPoolUtil.getJedis();  
            Set<String> result = jedis.smembers(key);  
            return result;  
        } finally {  
            JedisPoolUtil.closeJedis(jedis);  
        }  
    }  
  
    /** 
     * 获得一个字符串set集合 
     *  
     * @param key 
     * @return 
     */  
    public static <T> Set<T> getSet(String key, Class<T> clazz) {  
        if (isValueNull(key)) {  
            return null;  
        }  
        Jedis jedis = null;  
        try {  
            jedis = JedisPoolUtil.getJedis();  
            Set<byte[]> smembers = jedis.smembers(key.getBytes());  
            Set<T> result = null;  
            if (smembers != null) {  
                for (byte[] data : smembers) {  
                    if (result == null) {  
                        result = new HashSet<T>();  
                    }  
                    result.add((T) SerializeUtils.unserialize(data));  
                }  
            }  
            return result;  
        } finally {  
            JedisPoolUtil.closeJedis(jedis);  
        }  
    }  
  
    /** 
     * 获得集合中存在多少个值 
     *  
     * @param key 
     * @return 
     */  
    public static long getSetCount(String key) {  
        if (isValueNull(key)) {  
            return 0;  
        }  
        Jedis jedis = null;  
        try {  
            jedis = JedisPoolUtil.getJedis();  
            return jedis.scard(key);  
        } finally {  
            JedisPoolUtil.closeJedis(jedis);  
        }  
    }  
  
    /** 
     * 获得一个Map<v,k>的集合 
     *  
     * @param key 
     * @param v 
     * @param k 
     * @return 
     */  
    public static <K, V> Map<K, V> getMap(String key, Class<K> k, Class<V> v) {  
        if (key == null || "".equals(key)) {  
            return null;  
        }  
        Jedis jedis = null;  
        try {  
            jedis = JedisPoolUtil.getJedis();  
            String data = jedis.get(key);  
            @SuppressWarnings("unchecked")  
            Map<K, V> result = (Map<K, V>) JSON.parseObject(data);  
            return result;  
        } finally {  
            JedisPoolUtil.closeJedis(jedis);  
        }  
    }  
  
    /** 
     * 删除一个值 
     *  
     * @param key 
     */  
    public static void del(String... key) {  
        Jedis jedis = null;  
        try {  
            jedis = JedisPoolUtil.getJedis();  
            for (int i = 0; i < key.length; i++) {  
                jedis.del(key);  
            }  
        } finally {  
            JedisPoolUtil.closeJedis(jedis);  
        }  
    }  
    /** 
     * 删除所有值
     *  
     * @param key 
     */  
    public static void batchDel() {  
        Jedis jedis = null;  
        try {  
            jedis = JedisPoolUtil.getJedis();  
            Set<String> keys = jedis.keys("*");
            for (String pt : keys) {
                jedis.del(pt);
            } 
        } finally {  
            JedisPoolUtil.closeJedis(jedis);  
        }  
    }  
  
    // --------------------------公用方法区------------------------------------  
    /** 
     * 检查值是否为null,如果为null返回true,不为null返回false 
     *  
     * @param obj 
     * @return 
     */  
    private static boolean isValueNull(Object... obj) {  
        for (int i = 0; i < obj.length; i++) {  
            if (obj[i] == null || "".equals(obj[i])) {  
                return true;  
            }  
        }  
        return false;  
    } 
}

SerializeUtils

package com.ly.zx.util.redis;

import java.io.ByteArrayInputStream;  
import java.io.ByteArrayOutputStream;  
import java.io.ObjectInputStream;  
import java.io.ObjectOutputStream;  

public class SerializeUtils {
    /**  
     * 将对象进行序列化  
     * @param object  
     * @return  
     * @attention 传递对象
     */  
    public static byte[] serialize(Object object) {  
        ObjectOutputStream oos = null;  
        ByteArrayOutputStream baos = null;  
        try {  
            // 序列化  
            baos = new ByteArrayOutputStream();  
            oos = new ObjectOutputStream(baos);  
            oos.writeObject(object);  
            byte[] bytes = baos.toByteArray();  
            return bytes;  
        } catch (Exception e) {  
            throw new RuntimeException(e.getMessage(), e);  
        }  
    }  
      
    /**  
     * 反序列化  
     * @param bytes  
     * @return  
     * @attention 反序列化的操作  
     */  
    public static Object unserialize(byte[] bytes) {  
        ByteArrayInputStream bais = null;  
        try {  
            // 反序列化  
            bais = new ByteArrayInputStream(bytes);  
            ObjectInputStream ois = new ObjectInputStream(bais);  
            return ois.readObject();  
        } catch (Exception e) {  
            throw new RuntimeException(e.getMessage(), e);  
        }  
    }  
}

 使用时直接调用公共方法就行