spring整合redis连接

两种连接方式:(写了一半,未测试)

spring xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
    
    <!-- 加载配置文件 -->
    <!-- <context:property-placeholder location="classpath:redis-config/*.properties" /> -->
    
    <!-- jedis客户端单机版  id是我们随便起的名字,后面全限定名要复制对,这种方式不能配置密码,如果有密码,用下面的jedisPool配置
    然后还有个属性 poolConfig可以配也开不配置,不配置时会有默认配置-->
    <bean id="jedisPool" class="redis.clients.jedis.JedisPool">
        <constructor-arg name="host" value="127.0.0.1"></constructor-arg>
        <constructor-arg name="port" value="6379"></constructor-arg>
    </bean>
    
    <!-- 如果要配置连接密码需要用下面这种单机配置 -->
<!--     <bean id="jedisPool" class="redis.clients.jedis.JedisPool" >
        <constructor-arg name="host" value="${redis.host}"></constructor-arg>
        <constructor-arg name="port" value="${redis.port}"></constructor-arg>
        <constructor-arg name="password" value="${redis.password}"></constructor-arg>
        <constructor-arg name="timeout" value="${redis.timeout}"></constructor-arg>
        <constructor-arg name="database" value="${redis.database}"></constructor-arg>
        
        <constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg>
    </bean> -->

    <!-- 单机和集群都可能用到的配置 -->
<!--     <bean class="redis.clients.jedis.JedisPoolConfig" id="jedisPoolConfig">
        <property name="maxIdle" value="${maxIdle}" />
        <property name="maxTotal" value="${maxActive}" />
        <property name="maxWaitMillis" value="${maxWait}" />
        <property name="testOnBorrow" value="${testOnBorrow}" />
        <property name="blockWhenExhausted" value="${blockWhenExhausted}" />
    </bean>  -->
    
    <!-- 单机redisClient实现类 -->
    <bean id="jedisClientSingle" class="com.tydic.jtcrm.redis.util.JedisClientSingle">
        <property name="jedisPool" ref="jedisPool" />
    </bean>
    
    <!-- 集群相关 -->
    <!-- <bean id="jedisCluster" class="redis.clients.jedis.JedisCluster">
        多节点配置
        <constructor-arg name="jedisClusterNode">
            <set>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg index="0" value="${redis.host}"></constructor-arg>
                    <constructor-arg index="1" value="${redis.port}"></constructor-arg>
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg index="0" value="192.168.101.3"></constructor-arg>
                    <constructor-arg index="1" value="7002"></constructor-arg>
                </bean>
            </set>
        </constructor-arg>
        
        其他参数配置
        <constructor-arg name="connectionTimeout" value="2000"/>
         
        <constructor-arg name="soTimeout" value="2000"/>
         
        <constructor-arg name="maxAttempts" value="3"/>
         
        <constructor-arg name="password" value="${redis.password}"/>
         
        连接池配置,可以和带密码的单机版公用一个
        <constructor-arg name="poolConfig" ref="jedisPoolConfig"/>
        
    </bean> -->
    
    <!-- 集群redisClient实现类 -->
    <!-- <bean id="jedisClientCluster" class="com.tydic.jtcrm.redis.util.JedisClientCluster">
        <property name="jedisCluster" ref="jedisCluster" />
    </bean> -->
    
    <!-- 配置我们自定义的jedisClient实现类 -->
    <!-- <bean id="jedisClient" class="com.tydic.jtcrm.redis.service.JedisClient">
    </bean> -->

</beans>

代码:

单机版,使用jedispool:

package com.tydic.jtcrm.redis.util;


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

import javax.annotation.Resource;

import com.tydic.xxcrm.redis.service.JedisClient;

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

public class JedisClientSingle implements JedisClient{
    //单机版需要注入在 application-jedis.xml中配置的bean
    private JedisPool jedisPool;
    
    /**
     * 必须有set方法,否则无法属性注入
     */
    public void setJedisPool(JedisPool jedisPool) {
        this.jedisPool = jedisPool;
    }

    public String get(String key) {
        Jedis jedis = jedisPool.getResource();
        String result = jedis.get(key);
        jedis.close();
        return result;
    }

    public String set(String key, String value) {
        Jedis jedis = jedisPool.getResource();
        String result = jedis.set(key, value);
        jedis.close();
        return result;
    }
    
      public Boolean exists(String key){    
            Jedis jedis = null;  
            try {  
                jedis = jedisPool.getResource();  
                return jedis.exists(key);  
            }catch (Exception e) {  
                e.printStackTrace();  
            }finally{  
                if(jedis != null){  
                    jedis.close();  
                }  
            }  
            return false;  
        }    

    public String hget(String hkey, String key) {
        Jedis jedis = jedisPool.getResource();
        String result = jedis.hget(hkey, key);
        jedis.close();
        return result;
    }

    public Long hset(String hkey, String key, String value) {
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.hset(hkey, key, value);
        jedis.close();
        return result;
    }

    public Long incr(String key) {
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.incr(key);
        jedis.close();
        return result;
    }

    public Long expire(String key, int second) {
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.expire(key, second);
        jedis.close();
        return result;
    }

    public Long ttl(String key) {
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.ttl(key);
        jedis.close();
        return result;
    }

    public Long del(String key) {
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.del(key);
        jedis.close();
        return result;
    }

    public Long hdel(String hkey, String key) {
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.hdel(hkey,key);
        jedis.close();
        return result;
    }
    
    /**************************** redis 列表List start***************************/  
    
    /** 
     * 将一个值插入到列表头部,value可以重复,返回列表的长度 
     * @param key 
     * @param value String 
     * @return 返回List的长度 
     */  
    public  Long lpush(String key, String value){  
        Jedis jedis = jedisPool.getResource();  
        Long length = jedis.lpush(key, value);  
        jedis.close();  
        return length;  
    }  
      
    /** 
     * 将多个值插入到列表头部,value可以重复 
     * @param key 
     * @param values String[] 
     * @return 返回List的数量size 
     */  
    public  Long lpush(String key, String[] values){  
        Jedis jedis = jedisPool.getResource();  
        Long size = jedis.lpush(key, values);  
        jedis.close();  
        //System.out.println(result);  
        return size;  
    }  
      
    /** 
     * 获取List列表 
     * @param key 
     * @param start Long,开始索引 
     * @param end Long, 结束索引 
     * @return List<String> 
     */  
    public  List<String> lrange(String key, int start, int end){  
        Jedis jedis = jedisPool.getResource();  
        List<String> list = jedis.lrange(key, start, end);  
        jedis.close();  
        return list;  
    }  
      
    /** 
     * 通过索引获取列表中的元素 
     * @param key 
     * @param index,索引,0表示最新的一个元素 
     * @return String 
     */  
    public  String lindex(String key, Long index){  
        Jedis jedis = jedisPool.getResource();  
        String str = jedis.lindex(key, index);  
        jedis.close();  
        return str;  
    }  
      
    /** 
     * 获取列表长度,key为空时返回0 
     * @param key 
     * @return Long 
     */  
    public  Long llen(String key){  
        Jedis jedis = jedisPool.getResource();  
        Long length = jedis.llen(key);  
        jedis.close();  
        return length;  
    }  
      
    /** 
     * 在列表的元素前或者后插入元素,返回List的长度 
     * @param key 
     * @param where LIST_POSITION 
     * @param pivot 以该元素作为参照物,是在它之前,还是之后(pivot:枢轴;中心点,中枢;[物]支点,支枢;[体]回转运动。) 
     * @param value 
     * @return Long 
     */  
    public  Long linsert(String key, LIST_POSITION where, String pivot, String value){  
        Jedis jedis = jedisPool.getResource();  
        Long length = jedis.linsert(key, where, pivot, value);  
        jedis.close();  
        return length;  
    }  
      
    /** 
     * 将一个或多个值插入到已存在的列表头部,当成功时,返回List的长度;当不成功(即key不存在时,返回0) 
     * @param key 
     * @param value String 
     * @return Long 
     */  
    public  Long lpushx(String key, String value){  
        Jedis jedis = jedisPool.getResource();  
        Long length = jedis.lpushx(key, value);  
        jedis.close();  
        return length;  
    }  
      
    /** 
     * 将一个或多个值插入到已存在的列表头部,当成功时,返回List的长度;当不成功(即key不存在时,返回0) 
     * @param key 
     * @param values String[] 
     * @return Long 
     */  
    public  Long lpushx(String key, String[] values){  
        Jedis jedis = jedisPool.getResource();  
        Long length = jedis.lpushx(key, values);  
        jedis.close();  
        return length;  
    }  
      
    /** 
     * 移除列表元素,返回移除的元素数量 
     * @param key 
     * @param count,标识,表示动作或者查找方向 
     * <li>当count=0时,移除所有匹配的元素;</li> 
     * <li>当count为负数时,移除方向是从尾到头;</li> 
     * <li>当count为正数时,移除方向是从头到尾;</li> 
     * @param value 匹配的元素 
     * @return Long 
     */  
    public  Long lrem(String key, Long count, String value){  
        Jedis jedis = jedisPool.getResource();  
        Long length = jedis.lrem(key, count, value);  
        jedis.close();  
        return length;  
    }  
      
    /** 
     * 通过索引设置列表元素的值,当超出索引时会抛错。成功设置返回true 
     * @param key 
     * @param index 索引 
     * @param value 
     * @return Boolean 
     */  
    public  Boolean lset(String key, Long index, String value){  
        Jedis jedis = jedisPool.getResource();  
        String statusCode = jedis.lset(key, index, value);  
        jedis.close();  
        if("OK".equalsIgnoreCase(statusCode)){  
            return true;  
        }else{  
            return false;  
        }  
    }  
      
    /** 
     * 对一个列表进行修剪(trim),就是说,让列表只保留指定区间内的元素,不在指定区间之内的元素都将被删除。 
     * @param key 
     * @param start 
     * <li>可以为负数(-1是列表的最后一个元素,-2是列表倒数第二的元素。)</li> 
     * <li>如果start大于end,则返回一个空的列表,即列表被清空</li> 
     * @param end 
     * <li>可以为负数(-1是列表的最后一个元素,-2是列表倒数第二的元素。)</li> 
     * <li>可以超出索引,不影响结果</li> 
     * @return Boolean 
     */  
    public  Boolean ltrim(String key, Long start, Long end){  
        Jedis jedis = jedisPool.getResource();  
        String statusCode = jedis.ltrim(key, start, end);  
        jedis.close();  
        if("OK".equalsIgnoreCase(statusCode)){  
            return true;  
        }else{  
            return false;  
        }  
    }  
      
    /** 
     * 移出并获取列表的第一个元素,当列表不存在或者为空时,返回Null 
     * @param key 
     * @return String 
     */  
    public  String lpop(String key){  
        Jedis jedis = jedisPool.getResource();  
        String value = jedis.lpop(key);  
        jedis.close();  
        return value;  
    }  
      
    /** 
     * 移除并获取列表最后一个元素,当列表不存在或者为空时,返回Null 
     * @param key 
     * @return String 
     */  
    public  String rpop(String key){  
        Jedis jedis = jedisPool.getResource();  
        String value = jedis.rpop(key);  
        jedis.close();  
        return value;  
    }  
      
    /** 
     * 在列表中的尾部添加一个个值,返回列表的长度 
     * @param key 
     * @param value 
     * @return Long 
     */  
    public  Long rpush(String key, String value){  
        Jedis jedis = jedisPool.getResource();  
        Long length = jedis.rpush(key, value);  
        jedis.close();  
        return length;  
    }  
      
    /** 
     * 在列表中的尾部添加多个值,返回列表的长度 
     * @param key 
     * @param values 
     * @return Long 
     */  
    public  Long rpush(String key, String[] values){  
        Jedis jedis = jedisPool.getResource();  
        Long length = jedis.rpush(key, values);  
        jedis.close();  
        return length;  
    }  
      
    /** 
     * 仅当列表存在时,才会向列表中的尾部添加一个值,返回列表的长度 
     * @param key 
     * @param value 
     * @return Long 
     */  
    public  Long rpushx(String key, String value){  
        Jedis jedis = jedisPool.getResource();  
        Long length = jedis.rpushx(key, value);  
        jedis.close();  
        return length;  
    }  
      
    /** 
     * 移除列表的最后一个元素,并将该元素添加到另一个列表并返回 
     * @param sourceKey 源列表的key,当源key不存在时,结果返回Null 
     * @param targetKey 目标列表的key,当目标key不存在时,会自动创建新的 
     * @return String 
     */  
    public  String rpopLpush(String sourceKey, String targetKey){  
        Jedis jedis = jedisPool.getResource();  
        String value = jedis.rpoplpush(sourceKey, targetKey);  
        jedis.close();  
        return value;  
    }  
      
    /** 
     * 移出并获取列表的【第一个元素】, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。 
     * @param timeout 单位为秒 
     * @param keys 
     * <li>当有多个key时,只要某个key值的列表有内容,即马上返回,不再阻塞。</li> 
     * <li>当所有key都没有内容或不存在时,则会阻塞,直到有值返回或者超时。</li> 
     * <li>当超期时间到达时,keys列表仍然没有内容,则返回Null</li> 
     * @return List<String> 
     */  
    public  List<String> blpop(int timeout, String... keys){  
        Jedis jedis = jedisPool.getResource();  
        List<String> values = jedis.blpop(timeout, keys);  
        jedis.close();  
        return values;  
    }  
      
    /** 
     * 移出并获取列表的【最后一个元素】, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。 
     * @param timeout 单位为秒 
     * @param keys 
     * <li>当有多个key时,只要某个key值的列表有内容,即马上返回,不再阻塞。</li> 
     * <li>当所有key都没有内容或不存在时,则会阻塞,直到有值返回或者超时。</li> 
     * <li>当超期时间到达时,keys列表仍然没有内容,则返回Null</li> 
     * @return List<String> 
     */  
    public  List<String> brpop(int timeout, String... keys){  
        Jedis jedis = jedisPool.getResource();  
        List<String> values = jedis.brpop(timeout, keys);  
        jedis.close();  
        return values;  
    }  
      
    /** 
     * 从列表中弹出列表最后一个值,将弹出的元素插入到另外一个列表中并返回它;  
     * 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。 
     * @param sourceKey 源列表的key,当源key不存在时,则会进行阻塞 
     * @param targetKey 目标列表的key,当目标key不存在时,会自动创建新的 
     * @param timeout 单位为秒 
     * @return String 
     */  
    public  String brpopLpush(String sourceKey, String targetKey, int timeout){  
        Jedis jedis = jedisPool.getResource();  
        String value = jedis.brpoplpush(sourceKey, targetKey, timeout);  
        jedis.close();  
        return value;  
    }  
      
    /**************************** redis 列表List end***************************/  

    /**************************** redis 集合Set start***************************/  
    /**Redis的Set是string类型的无序集合。集合成员是唯一的,这就意味着集合中不能出现重复的数据。**/  
      
      
    /** 
     * 向集合添加一个或多个成员,返回添加成功的数量 
     * @param key 
     * @param members 
     * @return Long 
     */  
    public  Long sadd(String key, String... members){  
        Jedis jedis = null;  
        try {  
            jedis = jedisPool.getResource();  
            return jedis.sadd(key, members);  
        }catch (Exception e) {  
            e.printStackTrace();  
        }finally{  
            if(jedis != null){  
                jedis.close();  
            }  
        }  
        return null;  
    }  
      
    /** 
     * 获取集合的成员数 
     * @param key 
     * @return 
     */  
    public  Long scard(String key){  
        Jedis jedis = null;  
        try {  
            jedis = jedisPool.getResource();  
            return jedis.scard(key);    
        }catch (Exception e) {  
            e.printStackTrace();  
        }finally{  
            if(jedis != null){  
                jedis.close();  
            }  
        }  
        return null;  
    }  
      
    /** 
     * 返回集合中的所有成员 
     * @param key 
     * @return Set<String> 
     */  
    public  Set<String> smembers(String key){  
        Jedis jedis = null;  
        try {  
            jedis = jedisPool.getResource();  
            return jedis.smembers(key);    
        }catch (Exception e) {  
            e.printStackTrace();  
        }finally{  
            if(jedis != null){  
                jedis.close();  
            }  
        }  
        return null;  
    }  
      
    /** 
     * 判断 member 元素是否是集合 key 的成员,在集合中返回True 
     * @param key 
     * @param member 
     * @return Boolean 
     */  
    public  Boolean sIsMember(String key, String member){  
        Jedis jedis = null;  
        try {  
            jedis = jedisPool.getResource();  
            return jedis.sismember(key, member);    
        }catch (Exception e) {  
            e.printStackTrace();  
        }finally{  
            if(jedis != null){  
                jedis.close();  
            }  
        }  
        return null;  
    }  
      
    /** 
     * 返回给定所有集合的差集(获取第一个key中与其它key不相同的值,当只有一个key时,就返回这个key的所有值) 
     * @param keys 
     * @return Set<String> 
     */  
    public  Set<String> sdiff(String... keys){  
        Jedis jedis = null;  
        try {  
            jedis = jedisPool.getResource();  
            return jedis.sdiff(keys);    
        }catch (Exception e) {  
            e.printStackTrace();  
        }finally{  
            if(jedis != null){  
                jedis.close();  
            }  
        }  
        return null;  
    }  
      
    /** 
     * 返回给定所有集合的差集并存储在 targetKey中,类似sdiff,只是该方法把返回的差集保存到targetKey中 
     * <li>当有差集时,返回true</li> 
     * <li>当没有差集时,返回false</li> 
     * @param targetKey 
     * @param keys 
     * @return 
     */  
    public  Boolean sdiffStore(String targetKey, String... keys){  
        Jedis jedis = null;  
        try {  
            jedis = jedisPool.getResource();  
            Long statusCode = jedis.sdiffstore(targetKey, keys);  
            if(1L == statusCode){  
                return true;  
            }   
        }catch (Exception e) {  
            e.printStackTrace();  
        }finally{  
            if(jedis != null){  
                jedis.close();  
            }  
        }  
        return false;  
    }  
      
    /** 
     * 返回给定所有集合的交集(获取第一个key中与其它key相同的值,要求所有key都要有相同的值,如果没有相同,返回Null。当只有一个key时,就返回这个key的所有值) 
     * @param keys 
     * @return Set<String> 
     */  
    public  Set<String> sinter(String... keys){  
        Jedis jedis = null;  
        try {  
            jedis = jedisPool.getResource();  
            return jedis.sinter(keys);  
        }catch (Exception e) {  
            e.printStackTrace();  
        }finally{  
            if(jedis != null){  
                jedis.close();  
            }  
        }  
        return null;  
    }  
      
    /** 
     * 返回给定所有集合的交集并存储在 targetKey中,类似sinter 
     * @param targetKey 
     * @param keys 
     * @return Boolean 
     */  
    public  Boolean sinterStore(String targetKey, String... keys){  
        Jedis jedis = null;  
        try {  
            jedis = jedisPool.getResource();  
            Long statusCode = jedis.sinterstore(targetKey, keys);  
            if(1L == statusCode){  
                return true;  
            }  
        }catch (Exception e) {  
            e.printStackTrace();  
        }finally{  
            if(jedis != null){  
                jedis.close();  
            }  
        }  
        return false;  
    }  
      
    /** 
     * 将 member 元素从 sourceKey 集合移动到 targetKey 集合 
     * <li>成功返回true</li> 
     * <li>当member不存在于sourceKey时,返回false</li> 
     * <li>当sourceKey不存在时,也返回false</li> 
     * @param sourceKey 
     * @param targetKey 
     * @param member 
     * @return Boolean 
     */  
    public  Boolean smove(String sourceKey, String targetKey, String member){  
        Jedis jedis = null;  
        try {  
            jedis = jedisPool.getResource();  
            Long value = jedis.smove(sourceKey, targetKey, member);  
            if(value > 0){  
                return true;  
            }  
        }catch (Exception e) {  
            e.printStackTrace();  
        }finally{  
            if(jedis != null){  
                jedis.close();  
            }  
        }  
        return false;  
    }  
      
    /** 
     * 移除并返回集合中的一个随机元素 
     * <li>当set为空或者不存在时,返回Null</li> 
     * @param key 
     * @return String 
     */  
    public  String spop(String key){  
        Jedis jedis = null;  
        try {  
            jedis = jedisPool.getResource();  
            return jedis.spop(key);  
        }catch (Exception e) {  
            e.printStackTrace();  
        }finally{  
            if(jedis != null){  
                jedis.close();  
            }  
        }  
        return null;  
    }  
      
    /** 
     * 返回集合中一个或多个随机数 
     * <li>当count大于set的长度时,set所有值返回,不会抛错。</li> 
     * <li>当count等于0时,返回[]</li> 
     * <li>当count小于0时,也能返回。如-1返回一个,-2返回两个</li> 
     * @param key 
     * @param count 
     * @return List<String> 
     */  
    public  List<String> srandMember(String key, int count){  
        Jedis jedis = null;  
        try {  
            jedis = jedisPool.getResource();  
            return jedis.srandmember(key, count);  
        }catch (Exception e) {  
            e.printStackTrace();  
        }finally{  
            if(jedis != null){  
                jedis.close();  
            }  
        }  
        return null;  
    }  
      
    /** 
     * 移除集合中一个或多个成员 
     * @param key 
     * @param members 
     * @return 
     */  
    public  Boolean srem(String key, String... members){  
        Jedis jedis = null;  
        try {  
            jedis = jedisPool.getResource();  
            //Integer reply, specifically: 1 if the new element was removed   
            //0 if the new element was not a member of the set  
            Long value = jedis.srem(key, members);  
            if(value > 0){  
                return true;  
            }  
        }catch (Exception e) {  
            e.printStackTrace();  
        }finally{  
            if(jedis != null){  
                jedis.close();  
            }  
        }  
        return false;  
    }  
      
    /** 
     * 返回所有给定集合的并集,相同的只会返回一个 
     * @param keys 
     * @return 
     */  
    public  Set<String> sunion(String... keys){  
        Jedis jedis = null;  
        try {  
            jedis = jedisPool.getResource();  
            return jedis.sunion(keys);  
        }catch (Exception e) {  
            e.printStackTrace();  
        }finally{  
            if(jedis != null){  
                jedis.close();  
            }  
        }  
        return null;  
    }  
      
    /** 
     * 所有给定集合的并集存储在targetKey集合中 
     * <li>注:合并时,只会把keys中的集合返回,不包括targetKey本身</li> 
     * <li>如果targetKey本身是有值的,合并后原来的值是没有的,因为把keys的集合重新赋值给targetKey</li> 
     * <li>要想保留targetKey本身的值,keys要包含原来的targetKey</li> 
     * @param targetKey 
     * @param keys 
     * @return 
     */  
    public  Boolean sunionStore(String targetKey, String... keys){  
        Jedis jedis = null;  
        try {  
            jedis = jedisPool.getResource();  
            //返回合并后的长度  
            Long statusCode = jedis.sunionstore(targetKey, keys);  
            if(statusCode > 0){  
                return true;  
            }  
        }catch (Exception e) {  
            e.printStackTrace();  
        }finally{  
            if(jedis != null){  
                jedis.close();  
            }  
        }  
        return false;  
    }  
      
    /**************************** redis 集合Set end***************************/  
    
}

集群版,使用jedisCluster:

package com.txxxc.jtcrm.redis.util;

import java.util.List;

import com.tydic.jtcrm.redis.service.JedisClient;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;

public class JedisClientCluster implements JedisClient {
   
   private JedisCluster jedisCluster;
   
   public JedisCluster getJedisCluster() {
      return jedisCluster;
   }
 
   public void setJedisCluster(JedisCluster jedisCluster) {
      this.jedisCluster = jedisCluster;
   }
 
   @Override
   public String set(String key, String value) {
      return jedisCluster.set(key, value);
   }
 
   @Override
   public String get(String key) {
      return jedisCluster.get(key);
   }
 
   @Override
   public Boolean exists(String key) {
      return jedisCluster.exists(key);
   }
 
   @Override
   public Long expire(String key, int seconds) {
      return jedisCluster.expire(key, seconds);
   }
 
//   @Override
   public Long ttl(String key) {
      return jedisCluster.ttl(key);
   }
 
//   @Override
   public Long incr(String key) {
      return jedisCluster.incr(key);
   }
 
//   @Override
   public Long hset(String key, String field, String value) {
      return jedisCluster.hset(key, field, value);
   }
 
//   @Override
   public String hget(String key, String field) {
      return jedisCluster.hget(key, field);
   }
 
//   @Override
   public Long hdel(String key, String field) {
      return jedisCluster.hdel(key, field);
   }
 
//   @Override
   public Boolean hexists(String key, String field) {
      return jedisCluster.hexists(key, field);
   }
 
//   @Override
   public List<String> hvals(String key) {
      return jedisCluster.hvals(key);
   }
 
   @Override
   public Long del(String key) {
      return jedisCluster.del(key);
   }
 
   @Override
   public  Long rpush(String key, String value){  
       Long length = jedisCluster.rpush(key, value); 
       return length;  
   } 
   
   @Override
   public  String lpop(String key){  
       return jedisCluster.lpop(key);  
   } 
   
   @Override
   public  Long llen(String key){  
       return jedisCluster.llen(key); 
   } 
   
   @Override
   public  List<String> lrange(String key, int start, int end){  
       List<String> list = jedisCluster.lrange(key, start, end);  
       return list;  
   }
   
//   @Override
   public  Long lrem(String key, Long count, String value){  
       Long length = jedisCluster.lrem(key, count, value);  
       return length;  
   }  

}

接口:

package com.txxxc.jtcrm.redis.service;

import java.util.List;

public interface JedisClient {

    //获取值
    String get(String key);
    //设置值
    String set(String key,String value);
    /*//获取hash类型的值
    String hget(String hkey,String key);
    //设置hash类型的值
    Long hset(String hkey,String key,String value);
    //使某个值自增1
    Long incr(String key);*/
    //设置某个值的有效期,单位是秒
    Long expire(String key,int second);
    //获取某个值的有效期(返回-1为永久有效,返回-2为已经失效,返回其他正整数为剩余有效期毫秒值)
//    Long ttl(String key);
    //删除缓存数据
    Long del(String key);
    //删除hash类型数据
//    Long hdel(String hkey,String key);
    Boolean exists(String key);
    Long rpush(String key, String value);
    String lpop(String key);
    Long llen(String key);
    List<String> lrange(String key, int start, int end);
//    Long lrem(String key, Long count, String value);
    
}

测试:

package com.txxxc.xxcrm.redis;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.ctg.ixxxc.cache.pool.CtgJedisPool;
import com.ctg.ixxxc.cache.pool.CtgJedisPoolConfig;
import com.ctg.ixxxc.cache.pool.CtgJedisPoolException;
import com.ctg.ixxxc.cache.pool.ProxyJedis;
import com.txxxc.xxcrm.redis.util.JedisClientSingle;
import com.txxxc.xxcrm.redis.util.JedisClientSingle2;

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

/**   
 * @Package com.tydic.jtcrm.redis 
 * @ClassName RedisTest.java
 * @author libin   
 * @date 2019年4月13日 上午10:01:01 
 * @version V1.0   
*/
public class RedisTest {

    public static void main1(String[] args) {
        //创建一个jedis对象
        Jedis jedis = new Jedis("127.0.0.1", 6379);
        //调用jedis对象的方法,方法名和redis命令一致
        jedis.set("key1", "jedistest1");
        String s1 = jedis.get("key1");
        System.out.println(s1);
//        Set<String> keys = jedis.keys("*");
//        System.out.println(keys);
        //关闭jedis连接
        jedis.close();
        
    }
    public static void main2(String[] args) {
        //创建连接池对象
        JedisPool jedisPool = new JedisPool("127.0.0.1", 6379);
        //从连接池中获取一个jedis对象
        Jedis jedis = jedisPool.getResource();
        jedis.set("key2", "jedisPool2");
        String string = jedis.get("key2");
        System.out.println(string);
        //关闭jedis对象
        jedis.close();
        //关闭连接池
        jedisPool.close();

    }
    
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:redis-config/applicationContext-jedis.xml");
        JedisClientSingle j = (JedisClientSingle) applicationContext.getBean(JedisClientSingle.class);
        j.set("b", "6u");
        String s = j.get("b");
        System.out.println(s);
    }
    
    
    public static void main3(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:redis-config/applicationContext-jedis2.xml");
        JedisClientSingle2 j = (JedisClientSingle2) applicationContext.getBean(JedisClientSingle2.class);
        j.set("b", "5k");
        String s = j.get("b");
        System.out.println("----------------------------------"+s);
        
        
    }
    
    
    public static void main4(String[] args) throws CtgJedisPoolException {
        
        JedisPoolConfig poolConfig = new JedisPoolConfig(); // 线程池配置
        poolConfig.setMaxIdle(3); // 最大空闲连接数
        poolConfig.setMaxTotal(10); // 最大连接数(空闲+使用中)
        poolConfig.setMinIdle(3); // 保持的最小空闲连接数
        poolConfig.setMaxWaitMillis(3000); // 借出连接时最大的等待时间
        
        List<HostAndPort> nodes = new ArrayList<HostAndPort>();
        
        String[] hostArr = "172.16.1.249:10051".split(",");
        for (String host : hostArr) {
            String[] hostPort = host.split(":");
            nodes.add(new HostAndPort(hostPort[0], Integer.parseInt(hostPort[1])));
        }
        
        CtgJedisPoolConfig ctgJedisPoolConfig = new CtgJedisPoolConfig(nodes);
        ctgJedisPoolConfig.setDatabase(21) // 分组对应的桶位
                .setPassword("cust_center_all#Tydic123") // “用户#密码”
                .setPoolConfig(poolConfig) // 线程池配置
                .setPeriod(3000) // 后台监控执行周期,毫秒
                .setMonitorTimeout(2000); // 后台监控ping命令超时时间,毫秒
        
        CtgJedisPool ctgJedisPool = new CtgJedisPool(ctgJedisPoolConfig);
        ProxyJedis jedis = ctgJedisPool.getResource();
        
        jedis.set("a", "7777777");
        System.out.println("-----------: "+jedis.get("a"));
        if(jedis!=null){
            jedis.close();
        }
    }
    

}
原文地址:https://www.cnblogs.com/libin6505/p/10715094.html