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

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();
}
} }

spring整合redis连接的更多相关文章

  1. 网站性能优化小结和spring整合redis

    现在越来越多的地方需要非关系型数据库了,最近网站优化,当然从页面到服务器做了相应的优化后,通过在线网站测试工具与之前没优化对比,发现有显著提升. 服务器优化目前主要优化tomcat,在tomcat目录 ...

  2. spring整合redis之hello

    1.pom.xml文件 <dependencies> <!-- spring核心包 --> <dependency> <groupId>org.spri ...

  3. Redis的安装以及spring整合Redis时出现Could not get a resource from the pool

    Redis的下载与安装 在Linux上使用wget http://download.redis.io/releases/redis-5.0.0.tar.gz下载源码到指定位置 解压:tar -xvf ...

  4. Spring整合Redis&JSON序列化&Spring/Web项目部署相关

    几种JSON框架用法和效率对比: https://blog.csdn.net/sisyphus_z/article/details/53333925 https://blog.csdn.net/wei ...

  5. Spring整合Redis时报错:java.util.NoSuchElementException: Unable to validate object

    我在Spring整合Redis时报错,我是犯了一个很低级的错误! 我设置了Redis的访问密码,在Spring的配置文件却没有配置密码这一项,配置上密码后,终于不报错了!

  6. HttpClient实战三:Spring整合HttpClient连接池

    简介 在微服务架构或者REST API项目中,使用Spring管理Bean是很常见的,在项目中HttpClient使用的一种最常见方式就是:使用Spring容器XML配置方式代替Java编码方式进行H ...

  7. Spring整合redis实现key过期事件监听

    打开redis服务的配置文件   添加notify-keyspace-events Ex  如果是注释了,就取消注释 这个是在以下基础上进行添加的 Spring整合redis:https://www. ...

  8. Spring整合Redis,并配置Jedis连接池

    目录 只言片语 创建redis连接池的配置文件 单机版 spring整合redis(使用JedisPool) 项目中使用示例 集群版 spring整合redis(使用JedisCluster) 项目中 ...

  9. spring整合redis使用RedisTemplate的坑Could not get a resource from the pool

    一.背景 项目中使用spring框架整合redis,使用框架封装的RedisTemplate来实现数据的增删改查,项目上线后,我发现运行一段时间后,会出现异常Could not get a resou ...

随机推荐

  1. 排查 Maxwell can not find database 并且使用 MySQL binlog 解决相关问题

    目前我们在使用 Maxwell 在读线上机器的 binlog 同步我们的离线数据库. 这次错误定位上,首先线要确定问题是发生在生产者 还是队列 还是消费者.经过查看各机器上任务的运行日志,定位到了问题 ...

  2. Golang的面向对象实践method

    最近在系统的学习go的语法,一切都弄好了之后准备弄个im项目出来玩.在这个过程中会把看到有趣的写法和语法啥的拿出来分析一下. 我一直以为go语言对面向对象没有支持,但是后面看到了类似类的概念,meth ...

  3. centos5 安装redmine

    一.下载依赖包 yum -y install libxslt-devel libyaml-devel libxml2-devel gdbm-devel libffi-devel yum -y inst ...

  4. 【python练习题】程序9

    #题目:暂停一秒输出. import time for i in range(5): print (i) time.sleep(1)

  5. 创建一个UWP 打包签名

    Create a certificate for package signing 2017/2/8 3 min to read [ Updated for UWP apps on Windows 10 ...

  6. 打包一个UWP APP

    Before packaging your app Test your app. Before you package your app for store submission, make sure ...

  7. java web 开发入门 --- tomcat/servlet/jsp

    在做java web 开发时,要先安装tomcat.它是一个web服务器,也叫web容器,我们把写好的jsp, html页面放到它里面,然后启动它,就可以用浏览器访问这些页面,地址栏中输入localh ...

  8. python基础数据类型--dict 字典

    字典 字典是python中唯一的映射类型,采用键值对(key-value)的形式存储数据.python对key进行哈希函数运算,根据计算的结果决定value的存储地址,所以字典是无序存储的,且key必 ...

  9. BZOJ1324Exca王者之剑&BZOJ1475方格取数——二分图最大独立集

    题目描述   输入 第一行给出数字N,M代表行列数.N,M均小于等于100 下面N行M列用于描述数字矩阵 输出 输出最多可以拿到多少块宝石 样例输入 2 2 1 2 2 1 样例输出 4   题意就是 ...

  10. BZOJ1835 [ZJOI2010] 基站选址 【动态规划】【线段树】

    题目分析: 首先想一个DP方程,令f[m][n]表示当前在前n个村庄选了m个基站,且第m个基站放在n处的最小值,转移可以枚举上一个放基站的村庄,然后计算两个村庄之间的代价. 仔细思考两个基站之间村庄的 ...