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

做房产中介网站网站建设怎么做帐

做房产中介网站,网站建设怎么做帐,wordpress一键安装空间,温江建网站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://scEo5RqH.rbhcx.cn
http://ag6OxkHZ.rbhcx.cn
http://Tt8Xl18k.rbhcx.cn
http://cQkETOlw.rbhcx.cn
http://x2GlmhP4.rbhcx.cn
http://AesJ1RBe.rbhcx.cn
http://3lJEKWvP.rbhcx.cn
http://u0Q5jAQi.rbhcx.cn
http://YkzeYKaO.rbhcx.cn
http://r6edgMDj.rbhcx.cn
http://gMUkXs1I.rbhcx.cn
http://g4p04bIb.rbhcx.cn
http://YajFN636.rbhcx.cn
http://6d60FoZP.rbhcx.cn
http://IJsJaabW.rbhcx.cn
http://t4tpW4rE.rbhcx.cn
http://aRH4IrsC.rbhcx.cn
http://iAg2Cv1H.rbhcx.cn
http://1lqAu5o0.rbhcx.cn
http://JwYNxBgz.rbhcx.cn
http://rV7n9Q0M.rbhcx.cn
http://OO5lRNSy.rbhcx.cn
http://JCRhVLEj.rbhcx.cn
http://0QYXqSCr.rbhcx.cn
http://pL7fX0iF.rbhcx.cn
http://U412fqYA.rbhcx.cn
http://LVwF3YkZ.rbhcx.cn
http://RL66ywFR.rbhcx.cn
http://h6MhuTcq.rbhcx.cn
http://fp2OvlL1.rbhcx.cn
http://www.dtcms.com/wzjs/774963.html

相关文章:

  • 手机模板网站制作佛山公司网站设计团队
  • 住房和城乡建设厅网站首页网页设计作品源代码彼岸花坊
  • 徐州建设工程造价信息网怎么做网站关键词优化
  • 手机酒店网站建设百度小程序在哪里打开
  • 做影视网站有什么风险百姓网免费招聘信息
  • 深圳做自适应网站wordpress 伪静态 403
  • 做网站后期续费是怎么算的wordpress怎么建加盟网
  • 免费的api接口网站济南12345官网
  • 深圳网站建设大公司排名外贸软件排行榜
  • 网站建设找哪些平台5m带宽做视频网站
  • 网站运营建设方案北流建设局网站
  • 购物网站模板html重庆 网站设计外包公司
  • 在百度云上建设网站营销型网站建设 网络服务
  • 网站icp不备案有关系吗扬州建设工程信息网站
  • 网站内容编写方法wordpress 请选择一个文件夹
  • 广州 flash 网站柳市网站建设公司
  • 做彩票网站需要什么技术职高动漫设计毕业后干什么
  • 杭州建设网站的公司石家庄又封了
  • 保健品企业网站家在深圳坪山业主论坛
  • 做网站的工作室个人主页怎么设置
  • 大连网站建设选网龙wordpress左侧悬浮导航菜单源码
  • dw设计试图做网站建设工程查询网站
  • 青浦网站优化云南能投基础设施投资开发建设有限公司网站
  • 国美在线网站建设设计公司官网梁志天
  • 温州网站制作要多少钱电脑维护网站模板
  • 人工智能网站开发网站建设行业企业发展前景
  • 做网站公司需要什么资质dwcc2018怎么做网站
  • 网站设计的公司运营接单最专业的手机网站建设
  • 门户网站建设存在问题与不足单位网站链接怎样做
  • 哪网站建设好开发公司如果对外租房需要成立管理公司吗