spring整合redis缓存
如果對spring cache不了解,這邊有一篇文章詳細的介紹了
http://jinnianshilongnian.iteye.com/blog/2001040
改項目基于maven構建
相關依賴
Xml代碼
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | <dependency>?? ????<groupId>org.springframework.data</groupId>?? ????<artifactId>spring-data-redis</artifactId>?? ????<version>1.3.4.RELEASE</version>?? </dependency>?? ??? <dependency>?? ????<groupId>redis.clients</groupId>?? ????<artifactId>jedis</artifactId>?? ????<version>2.5.2</version>?? </dependency>?? ??? <dependency>?? ????<groupId>org.springframework</groupId>?? ????<artifactId>spring-jdbc</artifactId>?? ????<version>3.2.13.RELEASE</version>?? </dependency>?? ??? <!--?日志記錄依賴包,很多都依賴此包,像log4j,json-lib等等?-->?? <dependency>?? ????<groupId>commons-logging</groupId>?? ????<artifactId>commons-logging-api</artifactId>?? ????<version>1.1</version>?? </dependency>?? ??? <!--?junit?測試包?-->?? <dependency>?? ????<groupId>junit</groupId>?? ????<artifactId>junit</artifactId>?? ????<version>4.11</version>?? ????<scope>test</scope>?? </dependency>?? ??? <dependency>?? ????<groupId>org.springframework</groupId>?? ????<artifactId>spring-test</artifactId>?? ????<version>3.2.13.RELEASE</version>?? ????<scope>test</scope>?? </dependency> |
Spring相關配置
Xml代碼 ?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | <?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:context="http://www.springframework.org/schema/context"?? ????xmlns:cache="http://www.springframework.org/schema/cache"?xmlns:p="http://www.springframework.org/schema/p"?? ????xmlns:c="http://www.springframework.org/schema/c"?? ????xsi:schemaLocation="?? ????????http://www.springframework.org/schema/beans?http://www.springframework.org/schema/beans/spring-beans-3.2.xsd?? ????????http://www.springframework.org/schema/context?http://www.springframework.org/schema/context/spring-context-3.2.xsd?? ????????http://www.springframework.org/schema/jee?http://www.springframework.org/schema/jee/spring-jee-3.2.xsd?? ????????http://www.springframework.org/schema/cache??? ????????http://www.springframework.org/schema/cache/spring-cache-3.2.xsd"?? ????default-lazy-init="true">?? ??? ????<context:property-placeholder?location="classpath:redis.properties"?/>?? ??????? ????<!--?自動掃描包?-->?? ????<context:component-scan?base-package="com.layou"?/>?? ??? ??? ????<!--?啟用緩存注解功能,這個是必須的,否則注解不會生效,另外,該注解一定要聲明在spring主配置文件中才會生效?-->?? ????<cache:annotation-driven?cache-manager="cacheManager"?/>?? ??? ????<!--?spring自己的換管理器,這里定義了兩個緩存位置名稱?,既注解中的value?-->?? ????<bean?id="cacheManager"?class="org.springframework.cache.support.SimpleCacheManager">?? ????????<property?name="caches">?? ????????????<set>?? ????????????????<bean?class="com.layou.RedisCache">?? ????????????????????<property?name="redisTemplate"?ref="redisTemplate"?/>?? ????????????????????<property?name="name"?value="default"?/>?? ????????????????</bean>?? ????????????</set>?? ????????</property>?? ????</bean>?? ??? ????<!--?redis?相關配置?-->?? ????<bean?id="poolConfig"?class="redis.clients.jedis.JedisPoolConfig">?? ????????<property?name="maxIdle"?value="${redis.pool.maxIdle}"?/>?? ????????<property?name="maxTotal"?value="${redis.pool.maxActive}"?/>?? ????????<property?name="maxWaitMillis"?value="${redis.pool.maxWait}"?/>?? ????????<property?name="testOnBorrow"?value="${redis.pool.testOnBorrow}"?/>?? ????</bean>?? ??? ????<bean?id="connectionFactory"?? ????????class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">?? ????????<property?name="hostName"?value="${redis.host}"?/>?? ????????<property?name="port"?value="${redis.port}"?/>?? ????????<property?name="password"?value="${redis.password}"?/>?? ????????<property?name="timeout"?value="${redis.timeout}"?/>?? ????????<property?name="usePool"?value="true"?/>?? ????????<property?name="poolConfig"?ref="poolConfig"?/>?? ????</bean>?? ??? ????<bean?id="redisTemplate"?class="org.springframework.data.redis.core.RedisTemplate">?? ????????<property?name="connectionFactory"?ref="connectionFactory"?/>?? ????</bean>?? </beans> |
redis相關配置
Java代碼 ?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #redis.host=localhost?? redis.host=192.168.71.145?? redis.port=6379?? redis.password=?? #客戶端超時時間單位是毫秒?? redis.timeout=100000?? ??? #最大連接數?? redis.pool.maxActive=500???? #最大能夠保持idel狀態的對象數???? redis.pool.maxIdle=200???? #最大建立連接等待時間???? redis.pool.maxWait=2000???? #當調用borrow?Object方法時,是否進行有效性檢查???? redis.pool.testOnBorrow=true |
首先,通過自己實現Cache接口來實現對redis的相關操作
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | package?com.layou;?? ??? import?java.io.ByteArrayInputStream;?? import?java.io.ByteArrayOutputStream;?? import?java.io.IOException;?? import?java.io.ObjectInputStream;?? import?java.io.ObjectOutputStream;?? ??? import?org.springframework.cache.Cache;?? import?org.springframework.cache.support.SimpleValueWrapper;?? import?org.springframework.dao.DataAccessException;?? import?org.springframework.data.redis.connection.RedisConnection;?? import?org.springframework.data.redis.core.RedisCallback;?? import?org.springframework.data.redis.core.RedisTemplate;?? ??? public?class?RedisCache?implements?Cache?{?? ??? ????private?RedisTemplate<String,?Object>?redisTemplate;?? ????private?String?name;?? ??? ????public?RedisTemplate<String,?Object>?getRedisTemplate()?{?? ????????return?redisTemplate;?? ????}?? ??? ????public?void?setRedisTemplate(RedisTemplate<String,?Object>?redisTemplate)?{?? ????????this.redisTemplate?=?redisTemplate;?? ????}?? ??? ????public?void?setName(String?name)?{?? ????????this.name?=?name;?? ????}?? ??? ????@Override?? ????public?String?getName()?{?? ????????return?this.name;?? ????}?? ??? ????@Override?? ????public?Object?getNativeCache()?{?? ????????return?this.redisTemplate;?? ????}?? ??? ????@Override?? ????public?ValueWrapper?get(Object?key)?{?? ????????final?String?keyf?=?(String)?key;?? ????????Object?object?=?null;?? ????????object?=?redisTemplate.execute(new?RedisCallback<Object>()?{?? ????????????public?Object?doInRedis(RedisConnection?connection)?throws?DataAccessException?{?? ??? ????????????????byte[]?key?=?keyf.getBytes();?? ????????????????byte[]?value?=?connection.get(key);?? ????????????????if?(value?==?null)?{?? ????????????????????return?null;?? ????????????????}?? ????????????????return?toObject(value);?? ??? ????????????}?? ????????});?? ????????return?(object?!=?null???new?SimpleValueWrapper(object)?:?null);?? ????}?? ??? ????@Override?? ????public?void?put(Object?key,?Object?value)?{?? ????????final?String?keyf?=?(String)?key;?? ????????final?Object?valuef?=?value;?? ????????final?long?liveTime?=?86400;?? ??? ????????redisTemplate.execute(new?RedisCallback<Long>()?{?? ????????????public?Long?doInRedis(RedisConnection?connection)?throws?DataAccessException?{?? ????????????????byte[]?keyb?=?keyf.getBytes();?? ????????????????byte[]?valueb?=?toByteArray(valuef);?? ????????????????connection.set(keyb,?valueb);?? ????????????????if?(liveTime?>?0)?{?? ????????????????????connection.expire(keyb,?liveTime);?? ????????????????}?? ????????????????return?1L;?? ????????????}?? ????????});?? ????}?? ??? ????@Override?? ????public?void?evict(Object?key)?{?? ????????final?String?keyf?=?(String)?key;?? ????????redisTemplate.execute(new?RedisCallback<Long>()?{?? ????????????public?Long?doInRedis(RedisConnection?connection)?throws?DataAccessException?{?? ????????????????return?connection.del(keyf.getBytes());?? ????????????}?? ????????});?? ????}?? ??? ????@Override?? ????public?void?clear()?{?? ????????redisTemplate.execute(new?RedisCallback<String>()?{?? ????????????public?String?doInRedis(RedisConnection?connection)?throws?DataAccessException?{?? ????????????????connection.flushDb();?? ????????????????return?"ok";?? ????????????}?? ????????});?? ????}?? ??? ????/**? ?????*?描述?:?<Object轉byte[]>.?<br>? ?????*?<p>? ?????*?<使用方法說明>? ?????*?</p>? ?????*?? ?????*?@param?obj? ?????*?@return? ?????*/?? ????private?byte[]?toByteArray(Object?obj)?{?? ????????byte[]?bytes?=?null;?? ????????ByteArrayOutputStream?bos?=?new?ByteArrayOutputStream();?? ????????try?{?? ????????????ObjectOutputStream?oos?=?new?ObjectOutputStream(bos);?? ????????????oos.writeObject(obj);?? ????????????oos.flush();?? ????????????bytes?=?bos.toByteArray();?? ????????????oos.close();?? ????????????bos.close();?? ????????}?catch?(IOException?ex)?{?? ????????????ex.printStackTrace();?? ????????}?? ????????return?bytes;?? ????}?? ??? ????/**? ?????*?描述?:?<byte[]轉Object>.?<br>? ?????*?<p>? ?????*?<使用方法說明>? ?????*?</p>? ?????*?? ?????*?@param?bytes? ?????*?@return? ?????*/?? ????private?Object?toObject(byte[]?bytes)?{?? ????????Object?obj?=?null;?? ????????try?{?? ????????????ByteArrayInputStream?bis?=?new?ByteArrayInputStream(bytes);?? ????????????ObjectInputStream?ois?=?new?ObjectInputStream(bis);?? ????????????obj?=?ois.readObject();?? ????????????ois.close();?? ????????????bis.close();?? ????????}?catch?(IOException?ex)?{?? ????????????ex.printStackTrace();?? ????????}?catch?(ClassNotFoundException?ex)?{?? ????????????ex.printStackTrace();?? ????????}?? ????????return?obj;?? ????}?? } |
使用junit進行測試
package?com.layou;??
??
import?org.junit.Before;??
import?org.junit.Test;??
import?org.springframework.beans.factory.annotation.Autowired;??
import?org.springframework.cache.CacheManager;??
import?org.springframework.test.context.ContextConfiguration;??
import?org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;??
??
@ContextConfiguration(locations?=?{"classpath:applicationContext-cache-redis.xml"})??
public?class?RedisTest?extends?AbstractJUnit4SpringContextTests?{??
????private?RedisCache?cache;??
????@Autowired??
????private?CacheManager?manager;??
??????
????@Before??
????public?void?before()?{??
????????cache?=?(RedisCache)?manager.getCache("default");??
????}??
??????
????@Test??
????public?void?testAdd()?throws?Exception?{??
????????cache.put("test",?1);??
????}??
??????
????@Test??
????public?void?testGet()?throws?Exception?{??
????????System.out.println(cache.get("test").get());??
????}??
??????
????@Test??
????public?void?testEvict()?throws?Exception?{??
????????cache.evict("test");??
????}??
??????
????@Test??
????public?void?testClear()?throws?Exception?{??
????????cache.clear();??
????}??
}
在實際項目中,多是通過Spring的注解完成相關操作
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | package?com.layou;?? ??? import?org.springframework.cache.annotation.CacheEvict;?? import?org.springframework.cache.annotation.Cacheable;?? import?org.springframework.stereotype.Service;?? ??? @Service?? public?class?RedisAnnotation?{?? ??????? ????/**? ?????*?第一次查詢會到數據庫中差尋,第二次就會從緩存中取? ?????*?#id代表使用參數id? ?????*?一定要放在spring容器中管理,不然攔截不到? ?????*?@param?id? ?????*?@return? ?????*/?? ????@Cacheable(key="'name'?+?#id",?value="default")?? ????public?String?getNameById(int?id)?{?? ????????//TODO?進行數據庫查詢并返回查詢數據庫內容?? ????????System.out.println("查詢數據庫");?? ????????return?""?+?id;?? ????}?? ??????? ????/**? ?????*?從緩存中刪除? ?????*?@param?id? ?????*/?? ????@CacheEvict(key="'name'?+?#id",?value="default")?? ????public?void?deleteById(int?id)?{?? ????????System.out.println("數據庫刪除");?? ????}?? } |
使用junit進行相關測試
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | package?com.layou;?? ??? import?org.junit.Test;?? import?org.springframework.beans.factory.annotation.Autowired;?? import?org.springframework.test.context.ContextConfiguration;?? import?org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;?? ??? @ContextConfiguration(locations?=?{"classpath:applicationContext-cache-redis.xml"})?? public?class?RedisAnnotationTest?extends?AbstractJUnit4SpringContextTests?{?? ????@Autowired?? ????private?RedisAnnotation?redis;?? ??????? ????@Test?? ????public?void?testQuery()?{?? ????????//第二次查詢的時候就會從緩存中取?? ????????System.out.println(redis.getNameById(1));?? ????}?? ??????? ????@Test?? ????public?void?testDelete()?{?? ????????redis.deleteById(1);?? ????}?? } ?? |
本文轉自yunlielai51CTO博客,原文鏈接:http://blog.51cto.com/4925054/1741066,如需轉載請自行聯系原作者
總結
以上是生活随笔為你收集整理的spring整合redis缓存的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux 运维shell习题
- 下一篇: [HDU 1427]速度计算24点(DF