Caffeine是一种基于Java的高性能缓存库,它提供了可配置、快速、灵活的缓存实现。Caffeine具有以下特点:
Caffeine缓存不涉及任何序列化,因此目标缓存对象不需要实现Serializable接口。若涉及多级缓存或者多种缓存共用,其它需要网络传输或者持久化的缓存需要序列化,Caffeine尽管也使用实现序列化的实体类,但是不做序列化操作。
不需要序列化,降低了缓存使用难度。
- <dependency>
- <groupId>com.github.ben-manes.caffeine</groupId>
- <artifactId>caffeine</artifactId>
- </dependency>
- spring:
- cache:
- type: caffeine
配置缓存管理器:多CacheName配置。
- public interface CacheNameTimeConstant {
- String CACHE_DEFAULT = "CACHE_DEFAULT";
- String CACHE_10SECS = "CACHE_10SECS";
- String CACHE_60SECS = "CACHE_60SECS";
- }
同一个CacheManager配置多个CacheName,此处仅配置过期时间的差异,其余配置可自由增加。
- import com.example.demo.util.CacheNameTimeConstant;
- import com.github.benmanes.caffeine.cache.Caffeine;
- import org.springframework.cache.CacheManager;
- import org.springframework.cache.caffeine.CaffeineCache;
- import org.springframework.cache.support.SimpleCacheManager;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
-
- import java.util.ArrayList;
- import java.util.List;
- import java.util.concurrent.TimeUnit;
-
- @Configuration
- public class CaffeineConfig{
- @Bean
- public CacheManager caffeineCacheManager() {
- SimpleCacheManager cacheManager = new SimpleCacheManager();
- List
caches = new ArrayList<>(); - caches.add(new CaffeineCache(CacheNameTimeConstant.CACHE_5SECS,
- Caffeine.newBuilder().expireAfterWrite(5, TimeUnit.SECONDS).build()));
- caches.add(new CaffeineCache(CacheNameTimeConstant.CACHE_10SECS,
- Caffeine.newBuilder().expireAfterWrite(10, TimeUnit.SECONDS).build()));
- caches.add(new CaffeineCache(CacheNameTimeConstant.CACHE_30SECS,
- Caffeine.newBuilder().expireAfterWrite(30, TimeUnit.SECONDS).build()));
- cacheManager.setCaches(caches);
- return cacheManager;
- }
- }
-
- @RestController
- @RequestMapping("/test")
- public class TestController {
-
-
- @Resource
- private StuMapper stuMapper;
-
- /**
- * 添加缓存
- */
- @GetMapping("/selectStu/{id}")
- @Cacheable(value = CacheNameTimeConstant.CACHE_30SECS,key="#id")
- public Student selectStu(@PathVariable Integer id){
- return stuMapper.selectById(id);
- }
-
- /**
- * 更新缓存
- */
- @PostMapping("/updateStu")
- @CachePut(value = CacheNameTimeConstant.CACHE_30SECS,key = "#student.id")
- public Student updateStu(Student student){
- if (stuMapper.updateById(student) > 0) {
- return stuMapper.selectById(student.getId());
- }
- return null;
- }
- /**
- * 删除缓存
- */
- @PostMapping("/deleteStu/{id}")
- @CacheEvict(value = CacheNameTimeConstant.CACHE_30SECS,key = "#id")
- public String deleteStu(@PathVariable Integer id){
- return stuMapper.deleteById(id) > 0 ? "删除成功" : "删除失败";
- }
-
-
-
- }
添加@EnableCaching注解
- @SpringBootApplication
- @EnableTransactionManagement
- @EnableCaching
- public class DemoApplication {
-
- public static void main(String[] args) {
- SpringApplication.run(DemoApplication.class, args);
- System.out.println("启动成功");
- }
- @Bean
- public MessageConverter jsonMessageConverter(){
- return new Jackson2JsonMessageConverter();
- }
-
- }
第一次查询,是走数据库的

第二次查询不走数据库,直接返回缓存,但是30s后过期
更新缓存

再次查询数据时,从更新的缓存获取
