Spring Boot using Caffeine cache
Caffeine official introduction
demo
Caffeine configuration parameters
Caffeine is Java8 cache rewrite Guava, Guava replace the cache.
Spring Cache comment about the foundation, please see this article
Caffeine official introduction
caffeine official website
Caffeine is based Java8 high performance, near-perfect cache library.
demo
pom.xml
Introducing spring-context-support
com.github.ben-manes.caffeine
caffeine
2.8.0
org.springframework
spring-context-support
Or spring-boot-starter-cache
com.github.ben-manes.caffeine
caffeine
2.8.0
org.springframework.boot
spring-boot-starter-cache
Caffeine configuration parameters
Property Description
initalCapacity initial space
maximumSize the maximum number of cache
The largest cache of heavy weights maximumWeight
After a fixed time expired after the last expireAfterAccess write or visit
expireAfterWrite after the last written after a fixed time expired
refreshAfterWrite create a cache or after a recent update the cache after a fixed time interval to refresh the cache
weakKeys open key of weak references
weakValues open weak reference value of
softValues open soft reference value
recordStats development statistics
note
refreshAfterWrite to instantiate LoadingCache otherwise error
java.lang.IllegalStateException: refreshAfterWrite requires a LoadingCache
application.yml
spring:
cache:
cache-names:
- cache1
- cache2
type: caffeine
caffeine:
spec: Zhengzhou abortions cost http://www.zzzzyy120.com/
maximumSize=500,expireAfterAccess=600s
yml configuration file can also be configured by bean
CacheConfig.java
package com.jsong.wiki.blog.config;
com.github.benmanes.caffeine.cache.CacheLoader amount;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.springframework.cache.CacheManager;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
java.util.concurrent.TimeUnit import;
@Configuration
public class CacheConfig {
@Bean("caffeineCacheManager")
public CacheManager caffeineCacheManager() {
CaffeineCacheManager cacheManager = new CaffeineCacheManager();
cacheManager.setCaffeine(Caffeine.newBuilder()
.maximumSize(10_1000)
.expireAfterAccess(5, TimeUnit.SECONDS));
return cacheManager;
}
/ * If the configuration refreshAfterWrite have this bean, otherwise an error
java.lang.IllegalStateException: refreshAfterWrite requires a LoadingCache*/
@Bean
CacheLoader cacheLoader public () {
CacheLoader cacheLoader CacheLoader = new () {
@Nullable
@Override
public Object load(@NonNull Object key) throws Exception {
return null;
}
};
cacheLoader return;
}
}
CacheService.java
package com.jsong.wiki.blog.service;
import org.springframework.cache.annotation.*;
import org.springframework.stereotype.Component;
@EnableCaching
@CacheConfig(cacheNames = "caffeineCacheManager")
@Component
public class CacheService {
@Cacheable(value = "cache1", key = "#root.target")
public String getName() {
System.out.println("cache1");
return "cache1";
}
}