依赖
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<version>2.6.2</version>
</dependency>
配置类
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
@Configuration
public class CaffeineConfig {
@Bean
public Cache<Long, Demo> demoCache(){
return Caffeine.newBuilder()
.initialCapacity(100)
.maximumSize(10_000)
.build();
}
}
处理
@RestController
@RequestMapping("demo")
public class DemoController {
@Resource
private IDemoService demoService;
@Resource
private Cache<Long, Demo> demoCache;
@GetMapping("/{id}")
public Demo findById(@PathVariable("id") Long id) {
return demoCache.get(id, key -> demoService.query()
.eq("id", key)
.one()
);
}
}