關於redis爲何能做爲緩存這個問題咱們就不說了,直接來講一下redis緩存到底如何在項目中使用吧:java
1.redis緩存如何在項目中配置?node
1.1redis緩存單機版和集羣版配置?(redis的客戶端jedis經常使用)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
|
<?xml version=
"1.0"
encoding=
"UTF-8"
?>
<beans xmlns=
"http://www.springframework.org/schema/beans"
xmlns:context=
"http://www.springframework.org/schema/context"
xmlns:p=
"http://www.springframework.org/schema/p"
xmlns:aop=
"http://www.springframework.org/schema/aop"
xmlns:tx=
"http://www.springframework.org/schema/tx"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http:
//www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http:
//www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http:
//www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http:
//www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 鏈接池配置 -->
<bean id=
"jedisPoolConfig"
class
=
"redis.clients.jedis.JedisPoolConfig"
>
<!-- 最大鏈接數 -->
<property name=
"maxTotal"
value=
"30"
/>
<!-- 最大空閒鏈接數 -->
<property name=
"maxIdle"
value=
"10"
/>
<!-- 每次釋放鏈接的最大數目 -->
<property name=
"numTestsPerEvictionRun"
value=
"1024"
/>
<!-- 釋放鏈接的掃描間隔(毫秒) -->
<property name=
"timeBetweenEvictionRunsMillis"
value=
"30000"
/>
<!-- 鏈接最小空閒時間 -->
<property name=
"minEvictableIdleTimeMillis"
value=
"1800000"
/>
<!-- 鏈接空閒多久後釋放, 當空閒時間>該值 且 空閒鏈接>最大空閒鏈接數 時直接釋放 -->
<property name=
"softMinEvictableIdleTimeMillis"
value=
"10000"
/>
<!-- 獲取鏈接時的最大等待毫秒數,小於零:阻塞不肯定的時間,默認-
1
-->
<property name=
"maxWaitMillis"
value=
"1500"
/>
<!-- 在獲取鏈接的時候檢查有效性, 默認
false
-->
<property name=
"testOnBorrow"
value=
"true"
/>
<!-- 在空閒時檢查有效性, 默認
false
-->
<property name=
"testWhileIdle"
value=
"true"
/>
<!-- 鏈接耗盡時是否阻塞,
false
報異常,ture阻塞直到超時, 默認
true
-->
<property name=
"blockWhenExhausted"
value=
"false"
/>
</bean>
<!-- jedis客戶端單機版 -->
<bean id=
"redisClient"
class
=
"redis.clients.jedis.JedisPool"
>
<constructor-arg name=
"host"
value=
"192.168.146.131"
></constructor-arg>
<constructor-arg name=
"port"
value=
"6379"
></constructor-arg>
<constructor-arg name=
"poolConfig"
ref=
"jedisPoolConfig"
></constructor-arg>
</bean>
<bean id=
"jedisClient"
class
=
"com.taotao.rest.dao.impl.JedisClientSingle"
/>
<!-- jedis集羣版配置 -->
<!-- <bean id=
"redisClient"
class
=
"redis.clients.jedis.JedisCluster"
>
<constructor-arg name=
"nodes"
>
<set>
<bean
class
=
"redis.clients.jedis.HostAndPort"
>
<constructor-arg name=
"host"
value=
"192.168.25.153"
></constructor-arg>
<constructor-arg name=
"port"
value=
"7001"
></constructor-arg>
</bean>
<bean
class
=
"redis.clients.jedis.HostAndPort"
>
<constructor-arg name=
"host"
value=
"192.168.25.153"
></constructor-arg>
<constructor-arg name=
"port"
value=
"7002"
></constructor-arg>
</bean>
<bean
class
=
"redis.clients.jedis.HostAndPort"
>
<constructor-arg name=
"host"
value=
"192.168.25.153"
></constructor-arg>
<constructor-arg name=
"port"
value=
"7003"
></constructor-arg>
</bean>
<bean
class
=
"redis.clients.jedis.HostAndPort"
>
<constructor-arg name=
"host"
value=
"192.168.25.153"
></constructor-arg>
<constructor-arg name=
"port"
value=
"7004"
></constructor-arg>
</bean>
<bean
class
=
"redis.clients.jedis.HostAndPort"
>
<constructor-arg name=
"host"
value=
"192.168.25.153"
></constructor-arg>
<constructor-arg name=
"port"
value=
"7005"
></constructor-arg>
</bean>
<bean
class
=
"redis.clients.jedis.HostAndPort"
>
<constructor-arg name=
"host"
value=
"192.168.25.153"
></constructor-arg>
<constructor-arg name=
"port"
value=
"7006"
></constructor-arg>
</bean>
</set>
</constructor-arg>
<constructor-arg name=
"poolConfig"
ref=
"jedisPoolConfig"
></constructor-arg>
</bean>
<bean id=
"jedisClientCluster"
class
=
"com.taotao.rest.dao.impl.JedisClientCluster"
></bean> -->
</beans>
|
1.2redis的方法定義?spring
接口:數據庫
實現:分集羣和單機版apache
單機版實現方法:json
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
|
package
com.taotao.rest.dao.impl;
import
org.springframework.beans.factory.annotation.Autowired;
import
com.taotao.rest.dao.JedisClient;
import
redis.clients.jedis.Jedis;
import
redis.clients.jedis.JedisPool;
public
class
JedisClientSingle
implements
JedisClient{
@Autowired
private
JedisPool jedisPool;
@Override
public
String get(String key) {
Jedis jedis = jedisPool.getResource();
String string = jedis.get(key);
jedis.close();
return
string;
}
@Override
public
String set(String key, String value) {
Jedis jedis = jedisPool.getResource();
String string = jedis.set(key, value);
jedis.close();
return
string;
}
@Override
public
String hget(String hkey, String key) {
Jedis jedis = jedisPool.getResource();
String string = jedis.hget(hkey, key);
jedis.close();
return
string;
}
@Override
public
long
hset(String hkey, String key, String value) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.hset(hkey, key, value);
jedis.close();
return
result;
}
@Override
public
long
incr(String key) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.incr(key);
jedis.close();
return
result;
}
@Override
public
long
expire(String key,
int
second) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.expire(key, second);
jedis.close();
return
result;
}
@Override
public
long
ttl(String key) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.ttl(key);
jedis.close();
return
result;
}
@Override
public
long
del(String key) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.del(key);
jedis.close();
return
result;
}
@Override
public
long
hdel(String hkey, String key) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.hdel(hkey, key);
jedis.close();
return
result;
}
}
|
集羣版的實現方法:緩存
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
|
package
com.taotao.rest.dao.impl;
import
org.springframework.beans.factory.annotation.Autowired;
import
com.taotao.rest.dao.JedisClient;
import
redis.clients.jedis.JedisCluster;
public
class
JedisClientCluster
implements
JedisClient {
@Autowired
private
JedisCluster jedisCluster;
@Override
public
String get(String key) {
return
jedisCluster.get(key);
}
@Override
public
String set(String key, String value) {
return
jedisCluster.set(key, value);
}
@Override
public
String hget(String hkey, String key) {
return
jedisCluster.hget(hkey, key);
}
@Override
public
long
hset(String hkey, String key, String value) {
return
jedisCluster.hset(hkey, key, value);
}
@Override
public
long
incr(String key) {
return
jedisCluster.incr(key);
}
@Override
public
long
expire(String key,
int
second) {
return
jedisCluster.expire(key, second);
}
@Override
public
long
ttl(String key) {
return
jedisCluster.ttl(key);
}
@Override
public
long
del(String key) {
return
jedisCluster.del(key);
}
@Override
public
long
hdel(String hkey, String key) {
return
jedisCluster.hdel(hkey, key);
}
}
|
配置好後,如何添加到代碼中呢?app
2.redis緩存如何添加到業務邏輯代碼中?ide
redis做爲緩存的做用就是減小對數據庫的訪問壓力,當咱們訪問一個數據的時候,首先咱們從redis中查看是否有該數據,若是沒有,則從數據庫中讀取,將從數據庫中讀取的數據存放到緩存中,下次再訪問一樣的數據的是,仍是先判斷redis中是否存在該數據,若是有,則從緩存中讀取,不訪問數據庫了。
舉個例子:根據內容分類id訪問內容:
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
|
package
com.taotao.rest.service.impl;
import
java.util.ArrayList;
import
java.util.List;
import
org.apache.commons.lang3.StringUtils;
import
org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.beans.factory.annotation.Value;
import
org.springframework.stereotype.Service;
import
com.taotao.commonEntity.JsonUtils;
import
com.taotao.commonEntity.TaotaoResult;
import
com.taotao.mapper.TbContentMapper;
import
com.taotao.pojo.TbContent;
import
com.taotao.pojo.TbContentExample;
import
com.taotao.pojo.TbContentExample.Criteria;
import
com.taotao.rest.dao.JedisClient;
import
com.taotao.rest.service.ContentService;
import
redis.clients.jedis.Jedis;
//首頁大廣告位的獲取服務層信息
@Service
public
class
ContentServiceImpl
implements
ContentService {
@Value
(
"${CONTENTCATEGORYID}"
)
private
String CONTENTCATEGORYID;
@Autowired
private
TbContentMapper contentMapper;
@Autowired
private
JedisClient jedisClient;
@Override
public
List<TbContent> getContentList(Long categoryId) {
/*通常第一次訪問的時候先從數據庫讀取數據,而後將數據寫入到緩存,再次訪問同一內容的時候就從緩存中讀取,若是緩存中沒有則從數據庫中讀取
因此咱們添加緩存邏輯的時候,從數據庫中將內容讀取出來以後,先set入緩存,而後再從緩存中添加讀取行爲,若是緩存爲空則從數據庫中進行讀取
*/
//從緩存中獲取值
String getData = jedisClient.hget(CONTENTCATEGORYID, categoryId+
""
);
if
(!StringUtils.isBlank(getData)) {
List<TbContent> resultList= JsonUtils.jsonToList(getData, TbContent.
class
);
return
resultList;
}
TbContentExample example=
new
TbContentExample();
Criteria criteria = example.createCriteria();
criteria.andCategoryIdEqualTo(categoryId);
List<TbContent> list = contentMapper.selectByExample(example);
//向緩存中放入值
String jsonData = JsonUtils.objectToJson(list);
jedisClient.hset(CONTENTCATEGORYID, categoryId+
""
,jsonData);
return
list;
}
}
|
因此這裏就是寫邏輯代碼的時候,在業務功能處,從緩存中讀取-----從db中讀取----將數據寫入緩存。
3.針對上面出現的問題:
當咱們後臺數據庫中內容修改以後,由於緩存中的內容沒有修改,咱們訪問的時候都是先訪問緩存,因此即便數據庫中的內容修改了,可是頁面的顯示仍是不會改變的。由於緩存沒有更新,因此這就涉及到緩存同步的問題:即數據庫修改了內容與緩存中對應的內容同步。
緩存同步的原理:就是將redis中的key進行刪除,下次訪問的時候,redis中沒有改數據,則從DB進行查詢,再次更新到redis中。
咱們能夠寫一個緩存同步的服務:
緩存同步除了查詢是沒有涉及到同步問題,增長刪除修改都會涉及到同步問題。
只須要在後臺進行CRUD的地方添加調用該緩存同步的服務便可:
5.redis客戶端jedis的使用: