selectUser() {
42 | return dao.selectUser();
43 | }
44 |
45 | /**
46 | * condition 满足条件缓存数据
47 | */
48 | @Cacheable(key = "#id", condition = "#number ge 20") // >= 20
49 | public User selectUserByIdWithCondition(String id, int number) {
50 | return dao.selectUserById(id);
51 | }
52 |
53 | /**
54 | * unless 满足条件时否决缓存数据
55 | */
56 | @Cacheable(key = "#id", unless = "#number lt 20") // < 20
57 | public User selectUserByIdWithUnless(String id, int number) {
58 | return dao.selectUserById(id);
59 | }
60 |
61 | /**
62 | * @CachePut 将返回值保存到缓存中
63 | * @CachePut 一般在新增和修改中使用
64 | */
65 | @CachePut(key = "#user.id")
66 | public User insertUser(User user) {
67 | dao.insertUser(user);
68 | return user;
69 | }
70 |
71 | @CachePut(key = "#user.id", condition = "#user.age ge 20")
72 | public User insertUserWithCondition(User user) {
73 | dao.insertUser(user);
74 | return user;
75 | }
76 |
77 | @CachePut(key = "#user.id")
78 | public User updateUser(User user) {
79 | dao.updateUser(user);
80 | return user;
81 | }
82 |
83 | /**
84 | * 根据key删除缓存区中的数据
85 | */
86 | @CacheEvict(key = "#id")
87 | public void deleteUserById(String id) {
88 | dao.deleteUserById(id);
89 | }
90 |
91 | /**
92 | * allEntries = true :删除整个缓存区的所有值,此时指定的key无效
93 | * beforeInvocation = true :默认false,表示调用方法之后删除缓存数据;true时,在调用之前删除缓存数据(如方法出现异常)
94 | */
95 | @CacheEvict(key = "#id", allEntries = true)
96 | public void deleteUserByIdAndCleanCache(String id) {
97 | dao.deleteUserById(id);
98 | }
99 | }
100 |
101 |
--------------------------------------------------------------------------------
/src/main/java/com/github/redis/RedisConfig.java:
--------------------------------------------------------------------------------
1 | package com.github.redis;
2 |
3 | import com.fasterxml.jackson.annotation.JsonAutoDetect;
4 | import com.fasterxml.jackson.annotation.PropertyAccessor;
5 | import com.fasterxml.jackson.databind.ObjectMapper;
6 | import org.springframework.cache.CacheManager;
7 | import org.springframework.cache.annotation.EnableCaching;
8 | import org.springframework.context.annotation.Bean;
9 | import org.springframework.context.annotation.Configuration;
10 | import org.springframework.data.redis.cache.RedisCacheConfiguration;
11 | import org.springframework.data.redis.cache.RedisCacheManager;
12 | import org.springframework.data.redis.cache.RedisCacheManager.RedisCacheManagerBuilder;
13 | import org.springframework.data.redis.connection.RedisConnectionFactory;
14 | import org.springframework.data.redis.core.*;
15 | import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
16 | import org.springframework.data.redis.serializer.RedisSerializationContext;
17 | import org.springframework.data.redis.serializer.StringRedisSerializer;
18 |
19 | import java.time.Duration;
20 |
21 | /**
22 | * StringBoot 封装了 RedisTemplate 对象来操作 Redis, 并在RedisAutoConfiguration下配置的两个RedisTemplate
23 | * RedisTemplate
24 | * StringRedisTemplate: key(String) --> value(Object)
25 | * RedisTemplate
26 | * StringRedisTemplate: 即RedisTemplate, 若value存储对象时,需要转为string,一般转为JSON格式的字符串
27 | * 可以配置一个 RedisTemplate 的bean,key设置为String格式,value设置自动转为JSON格式
28 | *
29 | * @see org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration
30 | */
31 | @Configuration
32 | @EnableCaching
33 | public class RedisConfig {
34 | /**
35 | * SpringBoot配置redis作为默认缓存工具
36 | * SpringBoot 2.0 以上版本的配置
37 | */
38 | @Bean
39 | public CacheManager cacheManager(RedisTemplate template) {
40 | RedisCacheConfiguration defaultCacheConfiguration =
41 | RedisCacheConfiguration
42 | .defaultCacheConfig()
43 | // 设置key为String
44 | .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(template.getStringSerializer()))
45 | // 设置value 为自动转Json的Object
46 | .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(template.getValueSerializer()))
47 | // 不缓存null
48 | .disableCachingNullValues()
49 | // 缓存数据保存1小时
50 | .entryTtl(Duration.ofHours(1));
51 | RedisCacheManager redisCacheManager =
52 | RedisCacheManagerBuilder
53 | // Redis 连接工厂
54 | .fromConnectionFactory(template.getConnectionFactory())
55 | // 缓存配置
56 | .cacheDefaults(defaultCacheConfiguration)
57 | // 配置同步修改或删除 put/evict
58 | .transactionAware()
59 | .build();
60 | return redisCacheManager;
61 | }
62 |
63 | /**
64 | * retemplate
65 | */
66 | @Bean(name = "template")
67 | public RedisTemplate template(RedisConnectionFactory factory) {
68 | // 创建RedisTemplate对象
69 | RedisTemplate template = new RedisTemplate<>();
70 | // 配置连接工厂
71 | template.setConnectionFactory(factory);
72 | // 定义Jackson2JsonRedisSerializer序列化对象
73 | Jackson2JsonRedisSerializer