一.redis安装:
用源码工程来编译安装
1、 到官网下载最新stable版,这里使用的是:redis-3.2.6.tar.gz
2、 cd /usr/local
3、 make redis-src
4、 tar -zxvf redis-3.2.6.tar.gz -c ./redis-src/
2、解压源码并进入目录cd /usr/local/redis-src/redis-3.2.6
3、 先执行make,检查是否报错
如果报错提示缺少gcc,则安装gcc : yum install -y gcc
如果报错提示:newer version ofjemalloc required
4、安装redis,指定安装目录,如 /usr/local/redis
make prefix=/usr/local/redis install
5、拷贝一份配置文件到安装目录下
切换到源码目录,里面有一份配置文件redis.conf,然后将其拷贝到安装路径下
cp redis.conf /usr/local/redis/
6、启动redis
cd /usr/local/redis
bin/redis-server redis.conf (如果想后台进程运行,修改:daemonize yes)
vim redis.conf 将下面的变量修改为:
daemonize yes
将bind的id换成真实的ip地址,比如:
bind 192.168.1.1
在集群配置中,要对redis配置密码,修改的配置是将redis.conf这个文件的下面的变量配置成如下的:
requirepass cloudtplwebapp (这里设置一个密码:cloudtplwebapp)
7、连接redis
另开一个xshell,然后:
#cd /usr/local/redis/
127.0.0.1:6379>
二.配置maven依赖
<!-- 整合redis所需的jar包 -->
<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>
二.配置applicationcontext.xml
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemalocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- 引入jdbc配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties,classpath:redis.properties" />
<!—在自己的application中加入如下配置 -->
<import resource="redis-context.xml"/>
</beans>
其中redis-context.xml的内容如下:
编写redis.properties属性文件
#redis.pool.maxactive=1024
#redis.pool.maxidle=200
#redis.pool.maxwait=1000
#redis.pool.testonborrow=true
redis.ip.0=192.168.1.1
redis.port.0=6379
#密码
redis.pass=cloudtplwebapp
#设置默认的连接的db
redis.default.db=0
redis.timeout=100000
redis.maxactive=300
redis.ip.1=192.168.1.1
redis.port.1=6379
redis.pool.maxtotal=1024
redis.pool.maxidle=200
redis.pool.maxwaitmillis=1000
redis.pool.testonborrow=true
redis.expiration=300
redis的redisservice如下:
package xxx.xxx.xxx.rediscache;
import java.io.serializable;
import java.util.map;
import java.util.set;
/**
* redisservice.java redis的服务操作类
* @attention
* @author toto
* @date 2017-1-12
* @note begin modify by 涂作权 2017-1-12 原始创建
*/
public interface redisservice<t> {
/**
* 将内容保存到缓存中
*
* @param key
* @param value
* @throws exception
* @attention
* @author toto
* @date 2017-1-12
* @note begin modify by 涂作权 2017-1-12 原始创建
*/
public void save(final string key, object value) throws exception;
* 获取内存中的内容
* @param <t>
* @param key
* @param elementtype
* @return
* @throws exception
* @attention
* @author toto
* @date 2017-1-12
* @note begin modify by 原始创建 2017-1-12 原始创建
*/
public <t> t getbykey(final string key, class<t> elementtype) throws exception;
* 通过传递key删除所有的在缓存中的内容
* @note begin modify by 涂作权 2017-1-12 原始创建
public void del(string... key) throws exception;
* 批量删除<br/>
* @param pattern
* @attention 该操作会执行模糊查询,请尽量不要使用,以免影响性能或误删
* @note begin modify by 涂作权 2017-1-12 原始创建
public void batchdel(string... pattern) throws exception;
* 取得缓存(int型)
* @attention 方法的使用注意事项
* @note begin modify by 涂作权 2017-1-12 原始创建
public integer getint(string key) throws exception;
* 取得缓存(字符串类型)
public string getstr(string key) throws exception;
* 取得缓存(字符串类型)
* @param retain
public string getstr(string key,boolean retain) throws exception;
* 获取缓存<br>
* @attention 注:基本数据类型(character除外),请直接使用get(string key, class<t> clazz)取值
public object getobj(string key) throws exception;
* 获取缓存<br>
* @param retain 是否保留
* @attention 注:java 8种基本类型的数据请直接使用get(string key, class<t> clazz)取值
public object getobj(string key,boolean retain) throws exception ;
* 获取缓存
* @note begin modify by 修改人 修改时间 修改内容摘要说明
public <t> t get(string key, class<t> clazz) throws exception;
* 将value对象写入缓存
* @param time 失效时间(秒)
public void set(string key,object value,long expirationtime)throws exception;
/**
* 递减操作
* @param by
* @return
public double decr(string key, double by)throws exception;
* 递增操作
* @param by
public double incr(string key, double by)throws exception;
* 获取double类型值
public double getdouble(string key) throws exception;
* 设置double类型值
public void setdouble(string key, double value,long expirationtime) throws exception;
* @param time 失效时间(秒)
public void setint(string key, int value, long expirationtime) throws exception;
* 将map写入缓存
* @param map
public <t> void setmap(string key, map<string, t> map, long expirationtime)throws exception;
* 向key对应的map中添加缓存对象
public <t> void addmap(string key, map<string, t> map)throws exception;
* @param key cache对象key
* @param field map对应的key
* @param value 值
*/
public void addmap(string key, string field, string value)throws exception;
* @param key cache对象key
* @param obj 对象
public <t> void addmap(string key, string field, t obj)throws exception;
* 获取map缓存
* @param clazz
public <t> map<string, t> mget(string key, class<t> clazz)throws exception;
* 获取map缓存中的某个对象
* @param field
public <t> t getmapfield(string key, string field, class<t> clazz)throws exception;
* 删除map中的某个对象
* @author lh
* @date 2016年8月10日
* @param key map对应的key
* @param field map中该对象的key
public void delmapfield(string key, string... field)throws exception;
* 指定缓存的失效时间
* @author fangjun
* @date 2016年8月14日
* @param key 缓存key
public void expire(string key, long expirationtime) throws exception;
* 添加set
public void sadd(string key, string... value) throws exception;
* 删除set集合中的对象
public void srem(string key, string... value) throws exception;
* set重命名
* @param oldkey
* @param newkey
public void srename(string oldkey, string newkey)throws exception;
* 模糊查询keys
* @param pattern
public set<serializable> keys(string pattern)throws exception;
}
redis的redisserviceimpl的实现类:
package xxx.xxx.xxx.rediscache.impl;
import java.util.concurrent.timeunit;
import org.apache.commons.lang.stringutils;
import org.apache.commons.logging.log;
import org.apache.commons.logging.logfactory;
import org.springframework.dao.dataaccessexception;
import org.springframework.data.redis.connection.redisconnection;
import org.springframework.data.redis.core.boundhashoperations;
import org.springframework.data.redis.core.rediscallback;
import org.springframework.data.redis.core.redistemplate;
import org.springframework.util.collectionutils;
import com.ucap.tpl.rediscache.redisservice;
import com.ucap.tpl.utils.serializeutils;
@suppresswarnings({"unchecked"})
public class redisserviceimpl<t> implements redisservice<t> {
@suppresswarnings("unused")
private static log logger = logfactory.getlog(redisserviceimpl.class);
private static redistemplate<serializable, serializable> redistemplate;
private long expirationtime;
public long getexpirationtime() {
return expirationtime;
}
public redistemplate<serializable, serializable> getredistemplate() {
return redistemplate;
@suppresswarnings("static-access")
public void setredistemplate(
redistemplate<serializable, serializable> redistemplate) {
this.redistemplate = redistemplate;
public void setexpirationtime(long expirationtime) {
this.expirationtime = expirationtime;
* 保存内容到缓存中
public void save(final string key, object value) throws exception {
final byte[] vbytes = serializeutils.serialize(value);
redistemplate.execute(new rediscallback<object>() {
public object doinredis(redisconnection connection)
throws dataaccessexception {
connection.set(redistemplate.getstringserializer().serialize(key), vbytes);
return null;
}
});
* 通过key取到保存在缓存中的内容
public <t> t getbykey(final string key, class<t> elementtype) throws exception {
try {
return redistemplate.execute(new rediscallback<t>() {
public t doinredis(redisconnection connection)
throws dataaccessexception {
byte[] keybytes = redistemplate.getstringserializer().serialize(key);
if (connection.exists(keybytes)) {
byte[] valuebytes = connection.get(keybytes);
t value = (t) serializeutils.unserialize(valuebytes);
return value;
}
return null;
}
});
} catch (exception e) {
e.printstacktrace();
}
return null;
* @attention 这里个的key是可变参数的key
public void del(string... key) throws exception {
if (key != null && key.length > 0) {
if (key.length == 1) {
redistemplate.delete(key[0]);
} else {
redistemplate.delete(collectionutils.arraytolist(key));
public void batchdel(string... pattern) throws exception {
for (string pt : pattern) {
redistemplate.delete(redistemplate.keys(pt + "*"));
public integer getint(string key) throws exception {
string value = (string) redistemplate.boundvalueops(key).get();
if (stringutils.isnotblank(value)) {
return integer.valueof(value);
public string getstr(string key) throws exception {
return (string) redistemplate.boundvalueops(key).get();
public string getstr(string key,boolean retain) throws exception {
if (!retain) {
redistemplate.delete(key);
return value;
* 注:基本数据类型(character除外),请直接使用get(string key, class<t> clazz)取值
public object getobj(string key) throws exception {
return redistemplate.boundvalueops(key).get();
* @note begin modify by 涂作权 2017-1-12 原始创建
public object getobj(string key, boolean retain) throws exception {
object obj = redistemplate.boundvalueops(key).get();
return obj;
* 获取缓存<br>
* 注:该方法暂不支持character数据类型
* @param key key
* @param clazz 类型
public <t> t get(string key, class<t> clazz) throws exception {
return (t) redistemplate.boundvalueops(key).get();
}
public void set(string key,object value,long expirationtime)throws exception {
if(value.getclass().equals(string.class)){
redistemplate.opsforvalue().set(key, value.tostring());
}else if(value.getclass().equals(integer.class)){
redistemplate.opsforvalue().set(key, value.tostring());
}else if(value.getclass().equals(double.class)){
}else if(value.getclass().equals(float.class)){
redistemplate.opsforvalue().set(key, value.tostring());
}else if(value.getclass().equals(short.class)){
}else if(value.getclass().equals(long.class)){
}else if(value.getclass().equals(boolean.class)){
}else{
redistemplate.opsforvalue().set(key, serializeutils.serialize(value));
}
if(expirationtime > 0){
redistemplate.expire(key, expirationtime, timeunit.seconds);
public double decr(string key, double by)throws exception {
return redistemplate.opsforvalue().increment(key, -by);
*/
public double incr(string key, double by)throws exception {
return redistemplate.opsforvalue().increment(key, by);
public double getdouble(string key) throws exception {
string value = (string) redistemplate.boundvalueops(key).get();
if(stringutils.isnotblank(value)){
return double.valueof(value);
return 0d;
public void setdouble(string key, double value,long expirationtime) throws exception {
redistemplate.opsforvalue().set(key, string.valueof(value));
public void setint(string key, int value, long expirationtime) throws exception {
redistemplate.expire(key,expirationtime, timeunit.seconds);
public <t> void setmap(string key, map<string, t> map, long expirationtime)throws exception {
redistemplate.opsforhash().putall(key, map);
public <t> void addmap(string key, map<string, t> map)throws exception {
* 向key对应的map中添加缓存对象
public void addmap(string key, string field, string value)throws exception {
redistemplate.opsforhash().put(key, field, value);
public <t> void addmap(string key, string field, t obj)throws exception {
redistemplate.opsforhash().put(key, field, obj);
public <t> map<string, t> mget(string key, class<t> clazz)throws exception {
boundhashoperations<serializable, string, t> boundhashoperations = redistemplate.boundhashops(key);
return boundhashoperations.entries();
}
public <t> t getmapfield(string key, string field, class<t> clazz)throws exception {
return (t)redistemplate.boundhashops(key).get(field);
public void delmapfield(string key, string... field)throws exception {
boundhashoperations<serializable, string, object> boundhashoperations = redistemplate.boundhashops(key);
boundhashoperations.delete(field);
public void expire(string key, long expirationtime) throws exception {
public void sadd(string key, string... value) throws exception {
redistemplate.boundsetops(key).add(value);
public void srem(string key, string... value) throws exception {
redistemplate.boundsetops(key).remove(value);
public void srename(string oldkey, string newkey)throws exception {
redistemplate.boundsetops(oldkey).rename(newkey);
public set<serializable> keys(string pattern)throws exception {
return redistemplate.keys(pattern);
}
依赖的serializeutils.java代码如下:
在basecontroller 中引入如下的内容:
/** 获取redis的service连接 */
@resource(name = "redisservice")
protected redisserviceimpl redisservice;
最后,在项目中就可以通过redisservice调用其它的redis方法了。