前言:
目前公司項(xiàng)目在上一個(gè)技術(shù)架構(gòu)的處理,已經(jīng)搭建好了Redis,但redis只用在了做session的管理,然而 后臺(tái)的對(duì)象緩存沒有用上
1. redis 和 ehcache的區(qū)別:
簡單了解了下,個(gè)人覺得 從部署上而言,redis更適合分布式部署,ehcache是在每臺(tái)應(yīng)用服務(wù)器上開辟一塊內(nèi)存做緩存,集群時(shí)還得考慮緩存的情況, redis就不需要考慮緩存了、單獨(dú)部署在一臺(tái)服務(wù)器中(也可以是在某一臺(tái)應(yīng)用服務(wù)器中)
2. 項(xiàng)目配置(spring mvc+maven+mybaits+redis),這里只講Spring 集成 redis:
a. 配置 pom.xml 文件 (若不是maven管理項(xiàng)目,下載2個(gè)jar 即可 )
1
2
3
4
5
6
7
8
9
10
11
12
|
<!-- redis cache related.....start --> < 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 > </ dependency > <!-- redis cache related.....end --> |
b.配置 applicationContext.xml文件
先在<beans>中加入 cache緩存
1
2
3
|
xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation="http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.2.xsd" |
在Spring加載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
|
<!-- ******************** redis緩存 **********************--> <!-- 注解一定要配置,不然不起作用 --> < cache:annotation-driven /> <!-- jedis 配置 --> < bean id = "poolConfig" class = "redis.clients.jedis.JedisPoolConfig" > < property name = "maxIdle" value = "${redis.maxIdle}" /> <!--<property name="maxWaitMillis" value="${redis.maxWait}" />--> < property name = "testOnBorrow" value = "${redis.testOnBorrow}" /> </ bean > <!-- redis服務(wù)器中心 --> < bean id = "connectionFactory" class = "org.springframework.data.redis.connection.jedis.JedisConnectionFactory" > < property name = "poolConfig" ref = "poolConfig" /> < property name = "port" value = "${redis.port}" /> < property name = "hostName" value = "${redis.hostname}" /> <!-- <property name="password" value="${redis.password}" /> --> < property name = "timeout" value = "${redis.timeout}" ></ property > </ bean > < bean id = "redisTemplate" class = "org.springframework.data.redis.core.RedisTemplate" > < property name = "connectionFactory" ref = "connectionFactory" /> < property name = "keySerializer" > < bean class = "org.springframework.data.redis.serializer.StringRedisSerializer" /> </ property > < property name = "valueSerializer" > < bean class = "org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" /> </ property > </ bean > <!-- 配置緩存 --> < constructor-arg ref = "redisTemplate" /> </ bean > <!-- ******************** redis緩存 **********************--> |
c.配置 application.properties 資源文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
#redis config #redis.hostname=192.168.242.131 redis.hostname=localhost redis.port=6379 redis.timeout=2000 redis.usePool=true redis.default.db=0 #\u6700\u5927\u5206\u914D\u7684\u5BF9\u8C61\u6570 redis.maxTotal=600 #\u6700\u5927\u80FD\u591F\u4FDD\u6301idel\u72B6\u6001\u7684\u5BF9\u8C61\u6570 redis.maxIdle=300 #\u591A\u957F\u65F6\u95F4\u68C0\u67E5\u4E00\u6B21\u8FDE\u63A5\u6C60\u4E2D\u7A7A\u95F2\u7684\u8FDE\u63A5 redis.timeBetweenEvictionRunsMillis=30000 #\u7A7A\u95F2\u8FDE\u63A5\u591A\u957F\u65F6\u95F4\u540E\u4F1A\u88AB\u6536\u56DE redis.minEvictableIdleTimeMillis=30000 #\u5F53\u8C03\u7528borrow Object\u65B9\u6CD5\u65F6\uFF0C\u662F\u5426\u8FDB\u884C\u6709\u6548\u6027\u68C0\u67E5 redis.testOnBorrow=true ########reids\u7F16\u7801\u683C\u5F0F redis.encode=utf-8 ######\u7F13\u5B58\u8FC7\u671F\u65F6\u95F4 \u79D2 1000*60*60*24*7 \u4E03\u5929 redis.expire=604800000 ####\u662F\u5426\u5F00\u542FRedis\u670D\u52A1\u5E94\u7528 redis.unlock=false |
3. 測試
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
@Service ( "testService" ) public class TestServiceImpl implements ITestService { @Resource private ITestDao testDao; @Cacheable (value= "testId" ,key= "'id_'+#id" ) public Test getTestById( int id) { return this .testDao.getObjById(id); } @CacheEvict (value= "testId" ,key= "'id_'+#id" ) public void removeTestById( int id) { } } |
結(jié)果:
第一次 進(jìn)入Service方法
第二次 不進(jìn)入service方法 也得到了值
注: 有朋友會(huì)問,啟動(dòng)訪問時(shí)保錯(cuò), 那是因?yàn)楸镜匚磫?dòng)redis服務(wù), 下載win32/win64版的,啟動(dòng) 再訪問就不會(huì)報(bào)錯(cuò)
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://blog.csdn.net/ouyhong123/article/details/52162951