兩種連接方式:(寫了一半,未測試)
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(); } } }