# pom 文件

1
2
3
4
5
6
7
8
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

# yml 配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
spring:
redis:
database: 0
host: localhost
port: 6379
jedis:
pool:
max-active: 8
max-wait: 1ms
max-idle: 8
min-idle: 0
timeout: 5000
cache:
type: redis

# redisConfig 配置

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
@Configuration
public class RedisConfig extends CachingConfigurerSupport {
@Bean
public CacheManager cacheManager(RedisConnectionFactory factory) {
//对象的序列化
RedisSerializationContext.SerializationPair valueSerializationPair
= RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer());
//全局redis缓存过期时间
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
.serializeValuesWith(valueSerializationPair);

Set<String> cacheNames = new HashSet<>();
cacheNames.add("testCache");
cacheNames.add("testCache2");

ConcurrentHashMap<String, RedisCacheConfiguration> configMap = new ConcurrentHashMap<>();
configMap.put("testCache", redisCacheConfiguration.entryTtl(Duration.ofSeconds(30)));//有效期30秒
configMap.put("testCache2", redisCacheConfiguration);//永久
return RedisCacheManager.builder(factory).initialCacheNames(cacheNames).withInitialCacheConfigurations(configMap).build();
}

private Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer() {
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
jackson2JsonRedisSerializer.setObjectMapper(objectMapper());
return jackson2JsonRedisSerializer;
}

private ObjectMapper objectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
return objectMapper;
}

@Override
@Bean //在没有指定缓存Key的情况下,key生成策略
public KeyGenerator keyGenerator() {
return new KeyGenerator() {
@Override
public Object generate(Object target, Method method, Object... params) {
StringBuilder sb = new StringBuilder();
sb.append(target.getClass().getName());
sb.append("#" + method.getName());
for (Object obj : params) {
sb.append(obj.toString());
}
return sb.toString();
}
};
}

}

# service 层用法

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
@Service
@CacheConfig(cacheNames = "testCache")
public class CachingServiceImpl implements CachingService {

@Override
@Cacheable(key = "#i")
public int getId(int i) {
System.out.println("执行 getId i = " + i);
return i*1000;
}

@Override
@CachePut(key = "#i")
public int update(int i) {
System.out.println("执行 update i = " + i);
return i*3000;
}

@Override
@CacheEvict(key = "#i")
public int delete(int i) {
return i;
}
}