zoukankan      html  css  js  c++  java
  • coffeine的基于内存缓存的使用

    1.今天我们来学习一下Coffeine,一个高性能的 Java 缓存库。基于内存的,都说性能很高,来尝试一下吧。

    首先我们创建一个springboot项目,引入相关依赖

    <dependencies>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    </dependency>
    <!--caffeine基于内存的缓存 开始-->
    <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.2</version>
    </dependency>
    <!--caffeine基于内存的缓存 结束-->

    <!--Aop 日志收集 开始-->
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>
    <!--Aop 日志收集 结束-->

    在一个就是配置文件的编写

    spring.cache.cache-names=foo,bar
    spring.cache.caffeine.spec=maximumSize=5,expireAfterAccess=60s
    #Caffeine配置说明:
    #initialCapacity=1000
    #初始的缓存空间大小
    #maximumSize=3
    #缓存的最大条数
    #maximumWeight=[long]:
    #缓存的最大权重
    #expireAfterAccess=60s
    #最后一次写入或访问后经过固定时间过期
    #expireAfterWrite=[duration]:
    #最后一次写入后经过固定时间过期
    #refreshAfterWrite=60s
    #创建缓存或者最近一次更新缓存后经过固定的时间间隔,刷新缓存

    #注意:
    #expireAfterWrite和expireAfterAccess同时存在时,以expireAfterWrite为准。
    #maximumSize和maximumWeight不可以同时使用
    控制器层

    @RestController
    public class CacheController {

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

    @RequestMapping("remove")
    @CacheEvict(value = "listCustomers" ,key = "#length")
    public void remove(Long length) {
    System.out.println("删除了id、key为" + length + "的数据缓存");
    //这里不做实际删除操作
    }
    }
    最关键的是在启动类上加
    @EnableCaching //使缓存生效

    在贴出java代码的配置(可以不用)

    //package com.ceshi.coffeine.config;
    //
    //import com.github.benmanes.caffeine.cache.Caffeine;
    //import org.springframework.cache.CacheManager;
    //import org.springframework.cache.annotation.EnableCaching;
    //import org.springframework.cache.caffeine.CaffeineCache;
    //import org.springframework.cache.support.SimpleCacheManager;
    //import org.springframework.context.annotation.Bean;
    //import org.springframework.context.annotation.Configuration;
    //import org.springframework.context.annotation.Primary;
    //
    //import java.util.ArrayList;
    //import java.util.concurrent.TimeUnit;
    //
    ///**
    // * @Description 效果等同于在配置文件里的配置一样,所以在此注释掉,不用了
    // * @ClassName CaffeineConfig
    // * @Authetr CZQ
    // * @Date 2019/7/2 16:48
    // * @VERSION 1.0
    // **/
    //@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(60), //有效期600秒
    // listCustomers(60,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<CaffeineCache> caches = new ArrayList<CaffeineCache>();
    // 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;
    // }
    //}

    运行想不测试吧。

    
    
    
    
    
    
    
  • 相关阅读:
    支持stl容器的gdb自定义命令
    Thrift辅助类,用于简化Thrift编程
    Linux上获取CPU Core个数的实现
    第54课 被遗弃的多重继承(下)
    第53课 被遗弃的多重继承(上)
    第52课 C++中的抽象类和接口
    第51课 C++对象模型分析(下)
    第50课 C++对象模型分析(上)
    第49课 多态的概念和意义
    第48课 同名覆盖引发的问题
  • 原文地址:https://www.cnblogs.com/guagua-join-1/p/11125320.html
Copyright © 2011-2022 走看看