Springboot integrates Caffeine local cache


In the project, some data is often read and write less. We usually put it in the redis cache, but sometimes it feels overkill. We will store part of the data in the local cache. As we mentioned earlier, guava cache is one of the better ones. Caffeine replaced Guava in Spring Boot 2.0, and guava support is no longer provided. Speaking of spring support, it is mainly convenient to use @Cacheable, @CachePut, @CacheEvict, @Caching and other annotations

There are two main ways to configure the cache, I personally recommend the second method, which is more flexible

A basic configuration

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;
    }
}

Test class

@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);
    }
}

result

进入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

It can be seen that the getData query method will only query twice, and the rest will be cached, and delete will also be re-queried and stored in the cache.

Two configuration class

@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 layer

@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;
    }
}

The detailed configuration information is the same as the guava configuration, please refer to the guava cache in front of the money.
Same result as above

Guess you like

Origin blog.csdn.net/qq_37904966/article/details/108182412