前言
做過大型軟件系統的同學都知道,隨著系統數據越來越龐大,越來越復雜,隨之帶來的問題就是系統性能越來越差,尤其是頻繁操作數據庫帶來的性能損耗更為嚴重。很多業績大牛為此提出了眾多的解決方案和開發了很多框架以優化這種頻繁操作數據庫所帶來的性能損耗,其中,尤為突出的兩個緩存服務器是Memcached和Redis。今天,我們不講Memcached和Redis本身,這里主要為大家介紹Spring與Redis整合使用的相關內容,下面話不多說了,來一起看看詳細的介紹吧。
方法如下
第一步,在項目中加入redis的pom代碼:
1
2
3
4
5
|
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version> 2.6 . 0 </version> </dependency> |
第二步,spring中加載redis配置文件:applicationContext-redis.xml,內容如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<bean id= "poolConfig" class = "redis.clients.jedis.JedisPoolConfig" > <property name= "maxTotal" value= "${redis.maxTotal}" /> </bean> <bean class = "redis.clients.jedis.ShardedJedisPool" > <constructor-arg index= "0" ref= "poolConfig" /> <constructor-arg index= "1" > <list> <bean class = "redis.clients.jedis.JedisShardInfo" > <constructor-arg index= "0" value= "${redis.node1.host}" /> <constructor-arg index= "1" value= "${redis.node1.port}" /> </bean> </list> </constructor-arg> </bean> </beans> |
第三步,編寫連接redis服務端的屬性文件:redis.properties
1
2
3
|
redis.maxTotal=100 redis.node1.host=127.0.0.1 redis.node1.port=6379 |
第四步,編寫redis的相關操作方法類,Function類和RedisService類:
Funcrion類:
1
2
3
4
5
6
7
8
9
10
11
|
package xx.service; /** * 為了抽取相同的操作代碼 * @author yeying *<p>Description:</p> *<p>Company:</p> * @date:2017年12月5日 下午9:02:44 */ public interface Function<T,E> { public T callback(E e); } |
RedisService類:
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
|
package com.taotao.common.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import redis.clients.jedis.ShardedJedis; import redis.clients.jedis.ShardedJedisPool; /** * redis的相關操作 * @author yeying *<p>Description:</p> *<p>Company:</p> * @date:2017年12月3日 下午2:11:47 */ @Service public class RedisService { @Autowired (required= false ) //需要再注入進去 private ShardedJedisPool shardedJedisPool; private <T> T execute(Function<T, ShardedJedis> fun){ ShardedJedis shardedJedis = null ; try { // 從連接池中獲取到jedis分片對象 shardedJedis = shardedJedisPool.getResource(); // 從redis中獲取數據 return fun.callback(shardedJedis); } catch (Exception e) { e.printStackTrace(); } finally { if ( null != shardedJedis) { // 關閉,檢測連接是否有效,有效則放回到連接池中,無效則重置狀態 shardedJedis.close(); } } return null ; } /** * 執行set操作 * @param key * @param value * @return */ public String set( final String key, final String value){ return this .execute( new Function<String, ShardedJedis>() { @Override public String callback(ShardedJedis e) { return e.set(key, value); } }); } /** * 執行set操作,并設置生存時間,單位為秒 * @param key * @param value * @param seconds * @return */ public String set( final String key, final String value, final Integer seconds){ return this .execute( new Function<String, ShardedJedis>() { @Override public String callback(ShardedJedis e) { String str =e.set(key, value); e.expire(key, seconds); return str; } }); } /** * 執行get操作 * @param key * @return */ public String get( final String key){ return this .execute( new Function<String, ShardedJedis>() { @Override public String callback(ShardedJedis e) { return e.get(key); } }); } /** * 執行set操作 * @param key * @return */ public Long del( final String key){ return this .execute( new Function<Long, ShardedJedis>() { @Override public Long callback(ShardedJedis e) { return e.del(key); } }); } /** * 設置生存時間,單位為秒 * @param key * @param seconds * @return */ public Long expire( final String key, final Integer seconds) { return this .execute( new Function<Long, ShardedJedis>() { @Override public Long callback(ShardedJedis e) { return e.expire(key, seconds); } }); } } |
第五步,啟動redis服務,redis-server.exe,雙擊打開:
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。
原文鏈接:http://www.cnblogs.com/yeyingyx/p/8547286.html