提起spring,redis,大家都在使用spring-data-redis;无可否认,这很好;
现状: 公司的spring版本太低了;2.5.。。古董,升级,涉及面太多了,伤筋动骨;
剑走偏锋,咱们今天曲线救国,直接spring 2.5.6整合最新版本的redis(jedis-2.8.0.jar);
任何未写版本的高谈阔论都是耍流氓;因此一定记得注明版本!!!我的版本是
spring-2.5.6.jar
jedis-2.8.0.jar
第一:引入jar包;
第二:配置redis参数:system.db.properties;
写道
redis.pool.maxTotal=1024
redis.pool.maxIdle=200
redis.pool.minIdle=5
redis.pool.maxWaitMillis=1000
redis.pool.testOnBorrow=true
redis.pool.testOnReturn=true
redis.host=127.0.0.1
redis.port=6379
redis.timeout=1000
redis.pool.maxIdle=200
redis.pool.minIdle=5
redis.pool.maxWaitMillis=1000
redis.pool.testOnBorrow=true
redis.pool.testOnReturn=true
redis.host=127.0.0.1
redis.port=6379
redis.timeout=1000
第三:配置spring的文件:context.redis.xml
注意:我们知道commons-pool2 的maxactive,maxWait已经更改命名。
JedisPoolConfig继承的就是我们熟悉的org.apache.commons.pool.impl.GenericObjectPool.Config。
jedis的大神们做扩展时,能不能考虑下代码的兼容性。。。。(一个坑,引用于万能的网友)我的配置是最新版;2016-05-31
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans default-autowire="byName">
<!-- 加载redis配置文件 已在system.db.properties
<context:property-placeholder location="classpath:redis.properties"/>
-->
<!-- redis连接池的配置 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal">
<value>${redis.pool.maxTotal}</value>
</property>
<property name="maxIdle" value="${redis.pool.maxIdle}"/>
<property name="minIdle" value="${redis.pool.minIdle}"/>
<property name="maxWaitMillis" value="${redis.pool.maxWaitMillis}"/>
<property name="testOnBorrow" value="${redis.pool.testOnBorrow}"/>
<property name="testOnReturn" value="${redis.pool.testOnReturn}"/>
</bean>
<!-- redis的连接池pool,不是必选项:timeout/password -->
<bean id = "jedisPool" class="redis.clients.jedis.JedisPool">
<constructor-arg index="0" ref="jedisPoolConfig"/>
<constructor-arg index="1" value="${redis.host}"/>
<constructor-arg index="2" value="${redis.port}" type="int"/>
<constructor-arg index="3" value="${redis.timeout}" type="int"/>
<!-- <constructor-arg index="4" value="${redis.password}"/>-->
</bean>
<bean id = "redisService" class="com.hh.common.tools.RedisService">
</bean>
</beans>
第四,工具类RedisService;封装了add,get,以及超时的方法;
package com.hh.common.tools;
import java.util.Set;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
/**
* <p>功能:封装redis 缓存服务器服务接口
* 时间 2016年5月31日 下午3:36:21
* @version 1.0
*/
public class RedisService {
/**
* 通过key删除(字节)
* @param key
*/
public void del(byte [] key){
this.getJedis().del(key);
}
/**
* 通过key删除
* @param key
*/
public void del(String key){
this.getJedis().del(key);
}
/**
* 添加key value 并且设置存活时间(byte)
* @param key
* @param value
* @param liveTime
*/
public void set(byte [] key,byte [] value,int liveTime){
this.set(key, value);
this.getJedis().expire(key, liveTime);
}
/**
* 添加key value 并且设置存活时间
* @param key
* @param value
* @param liveTime
*/
public void set(String key,String value,int liveTime){
this.set(key, value);
this.getJedis().expire(key, liveTime);
}
/**
* 添加key value
* @param key
* @param value
*/
public void set(String key,String value){
this.getJedis().set(key, value);
}
/**添加key value (字节)(序列化)
* @param key
* @param value
*/
public void set(byte [] key,byte [] value){
this.getJedis().set(key, value);
}
/**
* 获取redis value (String)
* @param key
* @return
*/
public String get(String key){
String value = this.getJedis().get(key);
return value;
}
/**
* 获取redis value (byte [] )(反序列化)
* @param key
* @return
*/
public byte[] get(byte [] key){
return this.getJedis().get(key);
}
/**
* 通过正则匹配keys
* @param pattern
* @return
*/
public Set<String> keys(String pattern){
return this.getJedis().keys(pattern);
}
/**
* 检查key是否已经存在
* @param key
* @return
*/
public boolean exists(String key){
return this.getJedis().exists(key);
}
/**
* 清空redis 所有数据
* @return
*/
public String flushDB(){
return this.getJedis().flushDB();
}
/**
* 查看redis里有多少数据
*/
public long dbSize(){
return this.getJedis().dbSize();
}
/**
* 检查是否连接成功
* @return
*/
public String ping(){
return this.getJedis().ping();
}
/**
* 获取一个jedis 客户端
* @return
*/
private Jedis getJedis(){
if(jedis == null){
return jedisPool.getResource();
}
return jedis;
}
private RedisService (){
}
//操作redis客户端
private static Jedis jedis;
public JedisPool getJedisPool() {
return jedisPool;
}
public void setJedisPool(JedisPool jedisPool) {
this.jedisPool = jedisPool;
}
private JedisPool jedisPool;
//action调用完了,一定要关掉!
public void close(){
if(jedis != null){
System.out.println("关闭一次redis连接");
jedis.close();
jedis=null;
}
}
}