Spring Boot集成咖啡因Caffeine缓存
Caffeine咖啡因是使用Java8重写的Guava缓存版本,将在SpringBoot2.0中取代Guava。如果咖啡因发生,咖啡因缓存管理器Caffeine Cache Manager
将自动配置。使用spring.cache.cache-names
属性,可以在启动时创建缓存,并通过如下配置(按顺序)对其进行自定义:
spring.cache.caffee.spec
:定义了特殊缓存com.github.benmanes.caffeine.cache.CaffeineSpec
:bean定义com.github.benmanes.caffee.cache.caffee
:bean定义
例如,以下配置创建一个foo
和bar
缓存,最大数量为500,生存时间为10分钟:
spring.cache.cache-names=foo,bar
spring.cache.caffeine.spec=maximumSize=500,expireAfterAccess=600s
此外,如果定义了com.github.benmanes.caffee.cache.CacheLoader
,它将自动与caffee
缓存管理器关联。由于CacheLoader
关联由缓存管理器管理的所有缓存,因此必须将其定义为CacheLoader<Object,Object>
,并且自动配置将忽略所有泛型类型。
引入依赖关系
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<version>2.6.0</version>
</dependency>
支持打开缓存
使用@EnableCaching
注解来启用Spring Boot以支持缓存
@SpringBootApplication
@EnableCaching// Open the cache and specify what needs to be displayed
public class SpringBootStudentCacheCaffeineApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootStudentCacheCaffeineApplication.class, args);
}
}
配置文件
添加了缓存的特殊配置,如最大容量、过期时间等。
spring.cache.cache-names=people
spring.cache.caffeine.spec=initialCapacity=50,maximumSize=500,expireAfterWrite=10s,refreshAfterWrite=5s
如果使用refreshAfterWrite
配置,还必须指定CacheLoader
,例如:
/**
* You must specify the Bean, refreshAfterWrite=5s for the configuration property to take effect.
*
* @return
*/
@Bean
public CacheLoader<Object, Object> cacheLoader() {
CacheLoader<Object, Object> cacheLoader = new CacheLoader<Object, Object>() {
@Override
public Object load(Object key) throws Exception {
return null;
}
// Rewriting this method returns the oldValue value back to refresh the cache
@Override
public Object reload(Object key, Object oldValue) throws Exception {
return oldValue;
}
};
return cacheLoader;
}
配置说明:
Initial Capacity=[integer]
:初始缓存空间大小Maximum Size=[long]
:最大缓存数Maximum Weight=[long]
:缓存的最大重量expireAfterAccess=[duration]
:在上次写入或访问后的固定时间后过期expireAfterWrite=[duration]
:在最后一次写入之后,它将在固定时间内过期refreshAfterWrite=[duration]
:在创建或上次更新缓存之后,以固定的间隔刷新缓存weakKeys
:打开对key的弱引用Weak Values
:打开对值的弱引用softValues
:打开对值的软引用recordStats
:开发统计函数
注意项:
- 存在
expireAfterWrite
和expireAfterAccess
同事时,以expireAfterWrite
为准。 Maximum Size
和Maximum Weight
不能同时使用Weak Values
和softValues
不能同时使用
示例代码
/**
* @author yuhao.wang
*/
@Service
public class PersonServiceImpl implements PersonService {
private static final Logger logger = LoggerFactory.getLogger(PersonServiceImpl.class);
@Autowired
PersonRepository personRepository;
@Override
@CachePut(value = "people", key = "#person.id")
public Person save(Person person) {
Person p = personRepository.save(person);
logger.info("by id,key by:" + p.getId() + "Data is cached");
return p;
}
@Override
@CacheEvict(value = "people")//2
public void remove(Long id) {
logger.info("Deleted id,key by" + id + "Data caching");
//No actual deletion is done here.
}
/**
* Cacheable
* value: Cache the prefix of key.
* key: Cache the suffix of key.
* sync: Set if the cache expires and only one request is placed to request the database, other requests are blocked, the default is false.
*/
@Override
@Cacheable(value = "people", key = "#person.id", sync = true)
public Person findOne(Person person, String a, String[] b, List<Long> c) {
Person p = personRepository.findOne(person.getId());
logger.info("by id,key by:" + p.getId() + "Data is cached");
return p;
}
@Override
@Cacheable(value = "people1")//3
public Person findOne1() {
Person p = personRepository.findOne(2L);
logger.info("by id,key by:" + p.getId() + "Data is cached");
return p;
}
@Override
@Cacheable(value = "people2")//3
public Person findOne2(Person person) {
Person p = personRepository.findOne(person.getId());
logger.info("by id,key by:" + p.getId() + "Data is cached");
return p;
}
}
完整代码地址:https://github.com/wyh-spring-ecosystem-student/spring-boot-student/tree/releases
除特别注明外,本站所有文章均为老K的Java博客原创,转载请注明出处来自https://javakk.com/2081.html
暂无评论