当前位置: 首页 > wzjs >正文

网站海外推广建设网站设计公司报价

网站海外推广建设,网站设计公司报价,wordpress怎么安装ssl,长沙网站搭建首选智投未来Caffeine 是一个高性能的本地缓存库,广泛用于 Java 应用程序中。它支持多种缓存淘汰策略(如基于容量、时间、引用等),并且性能优于 Guava Cache,因此成为许多开发者的首选。 以下是一个完整的 Caffeine 本地缓存实例的…

Caffeine 是一个高性能的本地缓存库,广泛用于 Java 应用程序中。它支持多种缓存淘汰策略(如基于容量、时间、引用等),并且性能优于 Guava Cache,因此成为许多开发者的首选。

以下是一个完整的 Caffeine 本地缓存实例的实现:

1. 添加依赖

如果你使用 Maven 构建项目,请在 pom.xml 中添加以下依赖:

<dependency><groupId>com.github.ben-manes.caffeine</groupId><artifactId>caffeine</artifactId><version>3.1.8</version> <!-- 版本号可以根据需要更新 -->
</dependency>

2. 示例代码

以下是一个完整的示例,展示如何使用 Caffeine 创建和使用本地缓存。

(1) 基本缓存操作

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;import java.util.concurrent.TimeUnit;public class CaffeineCacheExample {public static void main(String[] args) throws InterruptedException {// 创建缓存实例Cache<String, String> cache = Caffeine.newBuilder().maximumSize(100) // 设置最大容量为 100.expireAfterWrite(1, TimeUnit.MINUTES) // 写入后 1 分钟过期.build();// 向缓存中添加数据cache.put("key1", "value1");cache.put("key2", "value2");// 获取缓存中的值System.out.println("key1: " + cache.getIfPresent("key1")); // 输出: value1System.out.println("key3: " + cache.getIfPresent("key3")); // 输出: null// 模拟过期Thread.sleep(65000); // 等待 65 秒(超过 1 分钟)System.out.println("key1 after expiration: " + cache.getIfPresent("key1")); // 输出: null}
}

(2) 使用加载函数

Caffeine 支持在缓存未命中时自动加载数据。可以通过 CacheLoader 或 LoadingCache 实现。

import com.github.benmanes.caffeine.cache.CacheLoader;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;import java.util.concurrent.TimeUnit;public class CaffeineLoadingCacheExample {public static void main(String[] args) {// 创建 LoadingCache 实例LoadingCache<String, String> cache = Caffeine.newBuilder().maximumSize(100) // 设置最大容量为 100.expireAfterWrite(10, TimeUnit.MINUTES) // 写入后 10 分钟过期.build(new CacheLoader<>() {@Overridepublic String load(String key) {return "Default Value for " + key; // 缓存未命中时加载默认值}});// 获取缓存中的值(如果不存在则调用 load 方法)System.out.println(cache.get("key1")); // 输出: Default Value for key1System.out.println(cache.get("key2")); // 输出: Default Value for key2// 手动放入值cache.put("key1", "Custom Value for key1");System.out.println(cache.get("key1")); // 输出: Custom Value for key1}
}

(3) 异步加载缓存

Caffeine 支持异步加载缓存,适合耗时的数据加载场景。

import com.github.benmanes.caffeine.cache.AsyncCache;
import com.github.benmanes.caffeine.cache.Caffeine;import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;public class CaffeineAsyncCacheExample {public static void main(String[] args) {// 创建异步缓存实例AsyncCache<String, String> cache = Caffeine.newBuilder().maximumSize(100).expireAfterWrite(10, TimeUnit.MINUTES).buildAsync((key, executor) -> {// 模拟异步加载数据return CompletableFuture.supplyAsync(() -> "Async Value for " + key);});// 异步获取缓存值cache.get("key1").thenAccept(value -> {System.out.println("key1: " + value); // 输出: Async Value for key1});}
}

(4) 统计功能

Caffeine 提供了丰富的统计功能,可以帮助你监控缓存的命中率、加载时间等。

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;public class CaffeineStatsExample {public static void main(String[] args) {// 创建带有统计功能的缓存Cache<String, String> cache = Caffeine.newBuilder().maximumSize(100).recordStats() // 开启统计功能.build();// 添加数据cache.put("key1", "value1");// 查询数据cache.getIfPresent("key1"); // 命中cache.getIfPresent("key2"); // 未命中// 打印统计信息var stats = cache.stats();System.out.println("Hit Count: " + stats.hitCount()); // 输出: 1System.out.println("Miss Count: " + stats.missCount()); // 输出: 1}
}

3. 常见配置选项

4. 总结

  • 优点:
    • 高性能,适合高并发场景。
    • 功能丰富,支持多种淘汰策略和加载方式。
    • 易于集成 Spring Cache。
  • 适用场景:
    • 数据频繁读取但不经常变化的场景。
    • 需要快速响应的本地缓存需求。


文章转载自:

http://0FRCSIHL.phcqk.cn
http://yV2Ypbjr.phcqk.cn
http://Ac0PrKFs.phcqk.cn
http://DfbzlRMZ.phcqk.cn
http://0QI61aWn.phcqk.cn
http://JSJK7iYs.phcqk.cn
http://35exMiRm.phcqk.cn
http://6seXvVdS.phcqk.cn
http://IDVJt0wl.phcqk.cn
http://GcLu7nbi.phcqk.cn
http://mvS7lLhF.phcqk.cn
http://sXTOEa1f.phcqk.cn
http://kKhMour5.phcqk.cn
http://2BEfhrxL.phcqk.cn
http://JMoIwHQ7.phcqk.cn
http://hko35Om2.phcqk.cn
http://324EhHUv.phcqk.cn
http://i3Q3VSA5.phcqk.cn
http://6ZGt2K3Z.phcqk.cn
http://wLRgnijk.phcqk.cn
http://kDM7rzOY.phcqk.cn
http://QMomIAIY.phcqk.cn
http://6zNURh16.phcqk.cn
http://cXeJOVLk.phcqk.cn
http://yRfUALzs.phcqk.cn
http://DMbRn3Hl.phcqk.cn
http://Ko7nlHoQ.phcqk.cn
http://TZIv3bO4.phcqk.cn
http://uBFzZzG6.phcqk.cn
http://f1JAgMts.phcqk.cn
http://www.dtcms.com/wzjs/704009.html

相关文章:

  • 网站规划的原则项目外包+网站开发
  • 国际物流东莞网站建设成都广告推广策划宣传公司
  • php做用户登录网站百度推广和优化哪个好
  • 网站开发合作意向书在哪个网站做一照一码
  • 做网站的实验报告网站编辑如何做原创
  • 网站建设应该注意哪些原则wordpress ftp下载
  • 建设网站的目的和意义西安网站推广慧创
  • 上饶市建设监督网站1688的网站特色
  • 优化型网站建设的基本要求网站自建设需要买什么时候开始
  • 做网站的html代码格式网站建设如何选择服务器
  • 白城网站开发免费网站空间服务器
  • 增城网站怎么做seo免费一键生成证件照
  • 陕西有没有做网站普查公司互联网创业项目怎么推广
  • 数据库跟网站网站设计样例
  • 网站制作公司哪家正规珠海集团网站建设外包
  • 网站开发需要准备什么优秀网名
  • 品牌网站开发设计小米发布会在哪看
  • 江苏中兴建设有限公司网站艺术家网站源码
  • 旗袍网站架构浉河网站建设
  • 东莞常平做网站网站建设的相关论文
  • 作一个网站要多少钱wordpress架设主机
  • 帝国cms做下载网站ps网页入口设计步骤
  • 广州行业网站建设wordpress主题 网络公司
  • 有没有做高仿的网站徐州公共资源建设交易平台
  • 外贸网站价格网站建设项目策划书范文
  • 阿里云iot网站开发提交网站收录
  • 建筑工程招标网站如何建微信商城网站
  • 图片 网站源码wordpress菜单背景6
  • wap网站建设方案幸运快三的网站怎么做
  • 在国内做敏感网站国企建筑公司有哪些