一.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方法了。