需求描述:
定时从数据库加载数据,其他从本地缓存读取。缓存有效期60s。
数据库加载与从缓存中加载。
技术选型:spring Boot @Cacheable caffeine.
具体实现:
- pom 引用
<dependency> <groupId>com.github.ben-manes.caffeine</groupId> <artifactId>caffeine</artifactId> <version>2.8.4</version> </dependency>
-
配置文件修改:
spring.cache.type=caffeine spring.cache.caffeine.spec=maximumSize=5000,expireAfterWrite=60s
-
业务代码
@Cacheable("api_ais_push_area") public List<ApiAisPushArea> getAreas() { return this.apiAisPushAreaMapper.getApiAisPushAreas(); }