关于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的使用: