一、添加依赖
1
2
3
4
|
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
|
二、配置application.properties
1
2
3
4
5
|
#####################redis做缓存####################
spring.cache.type=redis
spring.redis.host=192.168.175.13
spring.redis.port=6379
spring.redis.password=123456
|
三、使用
3.1、自定义缓存管理器
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
|
package
com.example.demo.config;
import
java.lang.reflect.Method;
import
java.util.HashMap;
import
java.util.Map;
import
org.springframework.cache.CacheManager;
import
org.springframework.cache.annotation.CachingConfigurerSupport;
import
org.springframework.cache.interceptor.KeyGenerator;
import
org.springframework.context.annotation.Bean;
import
org.springframework.context.annotation.Configuration;
import
org.springframework.data.redis.cache.RedisCacheManager;
import
org.springframework.data.redis.core.RedisTemplate;
/**
* redis自定义缓存管理器
*
* @Author: 我爱大金子
* @Description: redis自定义缓存管理器
* @Date: Create in 17:02 2017/7/3
*/
@Configuration
@EnableCaching
public
class
RedisCacheConfiguration
extends
CachingConfigurerSupport {
/**
* 自定义缓存管理器
* @Author: 我爱大金子
* @Description: 自定义缓存管理器
* @Date: 17:06 2017/7/3
* @param redisTemplate
* @return
*/
@Bean
public
CacheManager cacheManager(RedisTemplate<?, ?> redisTemplate) {
RedisCacheManager cacheManager =
new
RedisCacheManager(redisTemplate);
// 设置默认的过期时间,20秒
cacheManager.setDefaultExpiration(
20
);
Map<String, Long> expires =
new
HashMap<String, Long>();
// 单独设置liuyCache的过期时间为60秒
expires.put(
"liuyCache"
, 60L);
cacheManager.setExpires(expires);
return
cacheManager;
}
/**
* 自定义key. 此方法将会根据类名+方法名+所有参数的值生成唯一的一个key,即使@Cacheable中的value属性一样,key也会不一样。
*/
@Override
public
KeyGenerator keyGenerator() {
return
new
KeyGenerator() {
@Override
public
Object generate(Object o, Method method, Object... objects) {
StringBuilder sb =
new
StringBuilder();
sb.append(o.getClass().getName());
sb.append(method.getName());
for
(Object obj : objects) {
sb.append(obj.toString());
}
return
sb.toString();
}
};
}
}
|
3.2、使用做缓存
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.example.demo.cache;
import
com.example.demo.mapper.UserMapper;
import
com.example.demo.pojo.User;
import
org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.cache.annotation.CacheConfig;
import
org.springframework.cache.annotation.CacheEvict;
import
org.springframework.cache.annotation.CachePut;
import
org.springframework.cache.annotation.Cacheable;
import
org.springframework.stereotype.Service;
import
java.util.List;
/**
* 使用redis缓存
*
* @Author: 我爱大金子
* @Description: 使用redis缓存
* @Date: Create in 17:09 2017/7/3
*/
@CacheConfig
(cacheNames =
"liuyCache"
)
@Service
public
class
UserService {
@Autowired
private
UserMapper userMapper;
/**查询用户*/
@Cacheable
(value = {
"user"
})
public
List<User> select(User user){
System.out.println(
"查询功能,缓存找不到,直接读库, user="
+ user);
return
userMapper.select(user);
}
/**添加用户*/
@CacheEvict
(value = {
"user"
}, allEntries =
true
)
public
int
insert(User user) {
int
result = userMapper.insert(user);
System.out.println(
"添加功能,清除缓存,直接写库, id="
+ user.getId());
return
result;
}
/**根据用户id删除*/
@CacheEvict
(value = {
"user"
}, allEntries =
true
)
public
int
deleteById(Integer id){
System.out.println(
"据用户id删除,清除缓存,直接写库, id="
+ id);
return
userMapper.deleteById(id);
}
/**根据用户id更新*/
@CacheEvict
(value = {
"user"
}, allEntries =
true
)
public
int
updateById(User user){
System.out.println(
"根据用户id更新,清除缓存,直接写库, id="
+ user.getId());
return
userMapper.updateById(user);
}
/**根据id查询用户*/
@Cacheable
(value = {
"user"
})
public
User selectByPrimaryKey(Integer id){
System.out.println(
"据id查询用户,缓存找不到,直接读库, id="
+ id);
return
userMapper.selectByPrimaryKey(id);
}
}
|
说明:
@Cacheable:取缓存
@CachePut:修改缓存
@CacheEvict:删除缓存,allEntries:true表示清除value中的全部缓存,默认为false。
四、测试
访问:http://localhost:8989/druid/sql.html
访问:http://localhost:8989/user/index
查看redis:
idea控制台:
第二次访问:http://localhost:8989/user/index后,查看sql监控,如下: