springboot整合Caffeine本地缓存


在项目中,有些数据往往读多写少,我们通常放入redis缓存中,但有时感觉大材小用,我们会把部分数据存入本地缓存中,我们前面讲到了,guava cache就是比较优秀的一种,在Spring Boot 2.0中将Caffeine取代Guava,已不再提供guava的支持。说到spring的支持,主要是很方便使用@Cacheable、@CachePut、@CacheEvict、@Caching等注解

主要有两种方式进行配置缓存,个人推荐第二种方法,比较灵活

一 基本配置

application.properties

spring.cache.type:caffeine
spring.cache.caffeine.spec=initialCapacity=10,maximumSize=200,expireAfterWrite=60s

service

@Service
public class StudentService {
    
    

    @Cacheable(value = "data")
    public String getData(String string){
    
    
        System.out.println(String.format("进入servie查询方法 参数:%s", string));
        return string;
    }

    @CacheEvict(value = "data")
    public String deleteData(String string){
    
    
        System.out.println(String.format("进入servie删除方法 参数:%s", string));
        return string;
    }
}

测试类

@RunWith(SpringRunner.class)
@SpringBootTest(classes = DemoApplication.class)
public class AppTest {
    
    

    @Autowired
    public StudentService studentService;

    @Test
    public void test1() throws InterruptedException {
    
    
        for (int i = 0; i < 10; i++) {
    
    
            TimeUnit.SECONDS.sleep(1);
            String data = studentService.getData(String.valueOf(1));
            System.out.println("controller studentService:"+data);
        }

        System.out.println("---------------------------------------------------");
        String data = studentService.getData(String.valueOf(1));
        System.out.println("删除缓存");
        String deleteData = studentService.deleteData(String.valueOf(1));
        System.out.println("删除后再次查询缓存");
        String data1 = studentService.getData(String.valueOf(1));
        System.out.println("controller studentService:"+data);
    }
}

结果

进入servie查询方法 参数:1
controller studentService:1
controller studentService:1
controller studentService:1
controller studentService:1
controller studentService:1
controller studentService:1
controller studentService:1
controller studentService:1
controller studentService:1
controller studentService:1
---------------------------------------------------
删除缓存
进入servie删除方法 参数:1
删除后再次查询缓存
进入servie查询方法 参数:1
controller studentService:1

可以看到getData查询方法只会查询两次,剩下的都是走缓存查询的,删除也会重新查询存入缓存

二 配置类

@Configuration
public class CacheConfig {
    
    

    @Bean
    public CacheManager caffeineCacheManager() {
    
    
        CaffeineCacheManager cacheManager = new CaffeineCacheManager();
        cacheManager.setCaffeine(Caffeine.newBuilder()
                .maximumSize(2)
                .expireAfterAccess(5, TimeUnit.SECONDS));
        return cacheManager;
    }
}

service层

@CacheConfig
public class StudentService {
    
    

    @Cacheable(value = "data")
    public String getData(String string){
    
    
        System.out.println(String.format("进入servie查询方法 参数:%s", string));
        return string;
    }

    @CacheEvict(value = "data")
    public String deleteData(String string){
    
    
        System.out.println(String.format("进入servie删除方法 参数:%s", string));
        return string;
    }
}

详细配置信息跟guava配置相同,请参看钱前面guava cache。
结果同上

猜你喜欢

转载自blog.csdn.net/qq_37904966/article/details/108182412