博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringBoot项目开发 - Caffeine本地缓存
阅读量:2438 次
发布时间:2019-05-10

本文共 3239 字,大约阅读时间需要 10 分钟。

为什么需要本地缓存?在系统中,有些数据,访问十分频繁(例如数据字典数据、国家标准行政区域数据),往往把这些数据放入分布式缓存中,但为了减少网络传输,加快响应速度,缓存分布式缓存读压力,会把这些数据缓存到本地JVM中,大多是先取本地缓存中,再取分布式缓存中的数据

而Caffeine是一个高性能Java 缓存库,使用Java8对Guava缓存重写版本,在Spring Boot 2.0中将取代Guava。

使用spring.cache.cache-names属性可以在启动时创建缓存
例如,以下application配置创建一个foo和bar缓存,最大数量为500,存活时间为10分钟

spring.cache.cache-names=foo,barspring.cache.caffeine.spec=maximumSize=500,expireAfterAccess=600s

还有一种代码实现方式,我在项目中就是使用代码方式,如何使用,请往下看…

1. 引入依赖

org.springframework.boot
spring-boot-starter-cache
com.github.ben-manes.caffeine
caffeine
2.6.2

2.添加一个CaffeineConfig配置类,开启缓存@EnableCaching

@Configuration@EnableCaching //开启缓存public class CaffeineConfig {
public static final int DEFAULT_MAXSIZE = 10000; public static final int DEFAULT_TTL = 600; /** * 定义cache名称、超时时长(秒)、最大容量 * 每个cache缺省:10秒超时、最多缓存50000条数据,需要修改可以在构造方法的参数中指定。 */ public enum Caches{
getUserById(600), //有效期600秒 listCustomers(7200,1000), //有效期2个小时 , 最大容量1000 ; Caches() {
} Caches(int ttl) {
this.ttl = ttl; } Caches(int ttl, int maxSize) {
this.ttl = ttl; this.maxSize = maxSize; } private int maxSize=DEFAULT_MAXSIZE; //最大數量 private int ttl=DEFAULT_TTL; //过期时间(秒) public int getMaxSize() {
return maxSize; } public int getTtl() {
return ttl; } } /** * 创建基于Caffeine的Cache Manager * @return */ @Bean @Primary public CacheManager caffeineCacheManager() {
SimpleCacheManager cacheManager = new SimpleCacheManager(); ArrayList
caches = new ArrayList
(); for(Caches c : Caches.values()){
caches.add(new CaffeineCache(c.name(), Caffeine.newBuilder().recordStats() .expireAfterWrite(c.getTtl(), TimeUnit.SECONDS) .maximumSize(c.getMaxSize()) .build()) ); } cacheManager.setCaches(caches); return cacheManager; }}

3.创建一个控制器,使用本地缓存,注意@Cacheable,value与上面配置的值对应,key为参数,sync=true表示同步,多个请求会被阻塞

@RestController@RequestMapping("cache")public class CacheController {
@RequestMapping("listCustomers") @Cacheable( value = "listCustomers" , key = "#length", sync = true) public List
listCustomers(Long length){
List
customers = new ArrayList<>(); for(int i=1; i <= length ; i ++){
Customer customer = new Customer(i, "zhuyu"+i, 20 + i, false); customers.add(customer); } return customers; }}

4.启动项目,访问上面的方法,效果如下,第一次处理时间为 110ms ,再刷新几次页面只要 1ms,说明后面的请求从本地缓存中获取数据,并返回了

在这里插入图片描述
在这里插入图片描述

使用本地缓存可以加快页面响应速度,缓存分布式缓存读压力,大量、高并发请求的网站比较适用

Caffeine配置说明:

initialCapacity=[integer]: 初始的缓存空间大小
maximumSize=[long]: 缓存的最大条数
maximumWeight=[long]: 缓存的最大权重
expireAfterAccess=[duration]: 最后一次写入或访问后经过固定时间过期
expireAfterWrite=[duration]: 最后一次写入后经过固定时间过期
refreshAfterWrite=[duration]: 创建缓存或者最近一次更新缓存后经过固定的时间间隔,刷新缓存
recordStats:开发统计功能

注意:

expireAfterWrite和expireAfterAccess同时存在时,以expireAfterWrite为准。

转载地址:http://hggmb.baihongyu.com/

你可能感兴趣的文章
科学的清理 Windows 98 注册表(转)
查看>>
Windows 98 桌面主题和用户管理(转)
查看>>
Windows 98 注册表妙用(转)
查看>>
自行添加欢迎对话框中的文本(转)
查看>>
Win2K Terminal Service使用经验(转)
查看>>
Windows 98 注册表应用的30个实例(转)
查看>>
为 Windows 98 的注册表数据库减肥(转)
查看>>
同时最小化多个Windows窗口(转)
查看>>
Windows Vista 内建管理员帐号被禁用(转)
查看>>
深度体验:Windows Vista最酷的五大功能(转)
查看>>
用Tweak UI改变你的 Windows 98 桌面设置(转)
查看>>
Geforce 4 MX 440强制Vista 开启玻璃效果(转)
查看>>
关于Vista Build 5536发布的官方技术问答(转)
查看>>
激活Vista 启动Logo画面(转)
查看>>
Windows Vista Beta2 中文版优化归类(转)
查看>>
功能大幅提高 Vista主要特性揭密(转)
查看>>
Win Vista使用感受:和经典菜单告别(转)
查看>>
SQL概述(转)
查看>>
用SQL删除数据(转)
查看>>
用SQL进行嵌套查询(转)
查看>>