前言:
SpringCache 是spring框架自身提供的一个缓存框架。它可以将数据缓存至自身spring程序应用中。同时它还可以整合redis。实现搭配。SpringCache 还整合了AOP实现简单操作的注解级缓存。通过一些简单注解就能实现数据的缓存。
实现了服务与中间件的解耦。
常用的注解:
@EnableCaching //开启缓存注解的功能 一般用于启动类,也可以配置在任意容器管理类上
/*------------------- */
@Cacheable //作用于方法上。如果缓存中有数据则直接返回。无数据则return真实数据,并将数据保存至缓存
/*--------------------*/
@CachePut //作用于方法上,将方法return的值保存到缓存中
/*--------------------*/
@CacheEvict //将数据从缓存中清除
操作基础使用
spring boot + redis + springcache
1、导入依赖
除了基础的springboot starter 依赖还需要如下依赖
<!-- springcache框架-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!-- redis缓存-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2、配置yml配置文件
server:
port: 8080
spring:
cache:
type: redis # 整合redis中间件
redis:
time-to-live: 60000 #缓存存活时间 单位:
key-prefix: CACHE_ #缓存key自定义前缀
cache-null-values: true # 允许缓存null,防止缓存击穿
use-key-prefix: true # 启用key前缀
redis:
host: localhost
port: 6379
database: 7
timeout: 1800
3、编写实践
a、编写config类
,因为我这边整合redis,同时开启@EnableCaching缓存,就不用在启动类上加了。
@Configuration
@EnableConfigurationProperties
@EnableCaching
public class MyRedisCacheConfig {
@Bean
public RedisCacheConfiguration redisCacheConfiguration(CacheProperties cacheProperties) {
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
// 设置缓存key的序列化方式
config =
config.serializeKeysWith(
RedisSerializationContext.SerializationPair.fromSerializer(
new StringRedisSerializer()));
// 设置缓存value的序列化方式(JSON格式)
config =
config.serializeValuesWith(
RedisSerializationContext.SerializationPair.fromSerializer(
new GenericJackson2JsonRedisSerializer()));
CacheProperties.Redis redisProperties = cacheProperties.getRedis();
if (redisProperties.getTimeToLive() != null) {
config = config.entryTtl(redisProperties.getTimeToLive());
}
if (redisProperties.getKeyPrefix() != null) {
config = config.prefixKeysWith(redisProperties.getKeyPrefix());
}
if (!redisProperties.isCacheNullValues()) {
config = config.disableCachingNullValues();
}
if (!redisProperties.isUseKeyPrefix()) {
config = config.disableKeyPrefix();
}
return config;
}
}
b、编写controller层
@RestController
@RequestMapping("/redis/api")
@Slf4j
public class RedisCacheController {
@Autowired
private RedisCacheFacade redisCacheFacade;
@GetMapping("/getCache")
public User getCache(@Param("id") String id){
User user =new User();
user.setId(Long.valueOf(id));
log.info("获取用户信息controller层:ID:{}",id);
return redisCacheFacade.getCache(user);
}
}
c、编写service层
@Service
@Slf4j
public class RedisCacheFacade {
/**
* Cacheable: 缓存有数据则直接返回缓存中的,如果无缓存数据则将本方成功return后的值进行保存
* value:缓存的名称,每个缓存名称下面可以有多个key
* key: 真正显示在redis中的key
* #user.id : 去入参对象中的id
*/
@Cacheable(value = "userCache",key = "#user.id")
public User getCache(User user) {
log.info("进入实现层,模拟从数据库读取数据,查询id:{}",user.getId());
String sqlResult="ygzygz";
User query = new User();
query.setId(user.getId());
query.setName(sqlResult);
query.setAge(17);
return query;
}
}
测试结果:
redis可视化工具查看:
总结:
总的来说springcache可以通过简单注解就实现缓存,还是不错的。推荐在小型的单体应用中使用。存一些访问比较多的实体还是不错的,比如商品的sku等。