认识Caffeine 本地缓存

官网:https://github.com/ben-manes/caffeine/wiki/Home-zh-CN

Caffeine是一个基于Java8开发的提供了近乎最佳命中率的高性能的缓存库。可以理解成一个高性能的Map结构,Caffeine提供了三种缓存驱逐策略

  • 基于容量:创建Caffeine对象时设置缓存数量的上香
  • 基于时间:创建Caffeine对象时设置缓存的有效期
  • 基于引用:设置缓存为软引用或弱引用,利用GC来回收。性能较差

注意:Caffeine设置的元素过期时,不是立马删除,是等下一次读写操作时或系统空闲时完成对数据的清理!

如果适应github的包,那看看这句话:For Java 11 or above, use 3.x otherwise use 2.x.

        <dependency>
            <groupId>top.javatool</groupId>
            <artifactId>canal-spring-boot-starter</artifactId>
            <version>1.2.1-RELEASE</version>
        </dependency>

试验:Caffeine的API通过先查缓存,缓存没有才查询DB

配置类

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.Duration;

@Configuration
public class CaffeineConfig {

    @Bean
    public Cache<String, String> 交由Spring管理Caffeine对象() {
        return Caffeine.newBuilder()
                .initialCapacity(100) // 设置初始化大小是100
                .maximumSize(10) // 设置缓存最多存储10个
                .expireAfterWrite(Duration.ofSeconds(60)) // 60秒后标记为可清除
                .build();
    }

}

操作代码:Caffeine的API通过先查缓存,缓存没有才查询DB

    @Autowired
    private Cache<String, String> cacheA;

    @Test
    public void Caffeine() {
        String name = cacheA.getIfPresent("name"); // 如果没有name则返回null
        cacheA.put("name", "张三");
        String newName = cacheA.getIfPresent("name"); // 如果没有name则返回null

        // 新用法,如果缓存没有,就会执行Lambda表达式的内容。
        String age = cacheA.get("age", key -> {
            // 模拟DB查询,然后插入数据
            String db_Data = "10";
            return db_Data; // return的数据,就被缓存了,同时返回此数据
        });
        System.out.println("Debug在这里,看一下变量name、newName、age");
    }
特殊说明:
上述文章均是作者实际操作后产出。烦请各位,请勿直接盗用!转载记得标注原文链接:www.zanglikun.com
第三方平台不会及时更新本文最新内容。如果发现本文资料不全,可访问本人的Java博客搜索:标题关键字。以获取全部资料 ❤