pom

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!--想要使用redis做缓存 还需要引入redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<exclusions>
<exclusion>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</exclusion>
</exclusions>
</dependency>

配置

1
2
3
4
5
6
7
8
9
10
11
12
13
spring:
cache:
#类型
type: redis
redis:
time-to-live: 3600000
key-prefix: CACHE_
use-key-prefix: true
cache-null-values: true
redis:
host: 192.168.16.223
# password:
port: 6379

自定义配置类

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
package com.jhj.gulimall.product.config;

import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import javax.annotation.Resource;

/**
* @Description MyCacheConfig
* @Author jhj
* @Date 2022/6/3015:49
**/
@EnableConfigurationProperties(CacheProperties.class)
@EnableCaching
@Configuration
public class MyCacheConfig {


@Bean
RedisCacheConfiguration redisCacheConfiguration(CacheProperties cacheProperties){

//key value 序列化
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration
.defaultCacheConfig()
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));

CacheProperties.Redis redis = cacheProperties.getRedis();
//有效期
if (redis.getTimeToLive()!=null){

redisCacheConfiguration = redisCacheConfiguration.entryTtl(redis.getTimeToLive());
}
//前缀
if (redis.getKeyPrefix()!=null){

redisCacheConfiguration = redisCacheConfiguration.prefixKeysWith(redis.getKeyPrefix());
}
//是否缓存空置
if (!redis.isCacheNullValues()){

redisCacheConfiguration = redisCacheConfiguration.disableCachingNullValues();
}
//是否使用前缀
if (!redis.isUseKeyPrefix()){

redisCacheConfiguration = redisCacheConfiguration.disableKeyPrefix();
}

return redisCacheConfiguration;
}
}

使用

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
/**
* 失效模式 清除多个缓存
* @param category
*/
@Caching(evict = {

@CacheEvict(value = "category",key = "'level1Category'"),
@CacheEvict(value = "category",key = "'getCatalogJson'")
})
// @CacheEvict(value = "category",allEntries = true) 删除该分区下所有数据
//双写模式 写库写缓存
//@CachePut
@Transactional
@Override
public void updateCascade(CategoryEntity category) {

this.updateById(category);
categoryBrandRelationService.updateCategory(category.getCatId(),category.getName());
}

//每个需要缓存的数据需要制定放到哪个名字的缓存{缓存分区 可以写多个}
/**
* 自动生成key value使用jdk序列化机制 默认时间为-1
*
* 指定缓存的key key属性 可以写spel表达式(可参考官网) #root.method.name(方法名)
* 时间 在配置文件中使用 spring.cache.redis.time-to-live= 毫秒值
* 将数据保存为json格式
* @return
*/
@Cacheable(value={
"category"},key = "'level1Category'",sync = true) //代表当前方法的结果需要缓存 如果缓存中有方法不调用 如果没有 方法调用 最后将结果放入缓存 sync加本地锁
@Override
public List<CategoryEntity> getLevel1Categorys() {

List<CategoryEntity> entities = baseMapper.selectList(new QueryWrapper<CategoryEntity>().eq("parent_cid", 0));
return entities;
}```


## 作者声明
```handlebars
如有问题,欢迎指正!