杠精派
派聪明
0/512

热门评论

不见山谷 2026年03月21日 16:22
引用原文:Caffeine
@派聪明,在技术派项目是如何整合Caffine技术的
点赞
回复3 取消回复
派聪明 2026年03月21日 16:22
Caffeine在技术项目中主要通过以下方式整合使用: 1. 添加依赖: ```xml <dependency> <groupId>com.github.ben-manes.caffeine</groupId> <artifactId>caffeine</artifactId> </dependency> ``` 2. 配置缓存策略: - 基于容量策略:maximumSize/maximumWeight - 基于时间策略:expireAfterAccess/expireAfterWrite/expireAfter - 基于引用策略:weakKeys/weakValues/softValues 3. 实现四种缓存模式: - 手动加载:使用getIfPresent/get方法 - 自动加载:通过CacheLoader指定加载规则 - 异步手动加载:返回CompletableFuture - 异步自动加载:异步执行CacheLoader 4. 核心操作: - 查询:getIfPresent/get/getAll - 添加:put - 清除:invalidate/invalidateAll Caffeine能显著提高服务吞吐率和QPS,降低响应时间,是本地缓存的首选方案。
点赞
回复 取消回复
展开剩余 2 条回复

8 条评论

不见山谷 2026年03月21日 16:22
引用原文:Caffeine
@派聪明,在技术派项目是如何整合Caffine技术的
点赞
回复3 取消回复
派聪明 2026年03月21日 16:22
Caffeine在技术项目中主要通过以下方式整合使用: 1. 添加依赖: ```xml <dependency> <groupId>com.github.ben-manes.caffeine</groupId> <artifactId>caffeine</artifactId> </dependency> ``` 2. 配置缓存策略: - 基于容量策略:maximumSize/maximumWeight - 基于时间策略:expireAfterAccess/expireAfterWrite/expireAfter - 基于引用策略:weakKeys/weakValues/softValues 3. 实现四种缓存模式: - 手动加载:使用getIfPresent/get方法 - 自动加载:通过CacheLoader指定加载规则 - 异步手动加载:返回CompletableFuture - 异步自动加载:异步执行CacheLoader 4. 核心操作: - 查询:getIfPresent/get/getAll - 添加:put - 清除:invalidate/invalidateAll Caffeine能显著提高服务吞吐率和QPS,降低响应时间,是本地缓存的首选方案。
点赞
回复 取消回复
展开剩余 2 条回复
不见山谷 2026年03月21日 16:22
引用原文:Caffeine
@派聪明,在技术派项目是如何整合Caffine技术的
点赞
回复1 取消回复
派聪明 2026年03月21日 16:22
在技术派项目中整合Caffeine技术主要通过以下步骤: 1. 添加Maven依赖: ```xml <dependency> <groupId>com.github.ben-manes.caffeine</groupId> <artifactId>caffeine</artifactId> </dependency> ``` 2. 选择缓存策略: - 手动加载:使用`Caffeine.newBuilder()`构建,通过`getIfPresent`和`get`方法操作 - 自动加载:通过`build(new CacheLoader<>)`指定加载规则 - 异步手动/自动加载:使用`buildAsync()`或`buildAsync(new CacheLoader<>)` 3. 配置缓存参数: - 容量限制:`.maximumSize(100)` - 过期策略:`.expireAfterWrite(5, TimeUnit.MINUTES)` - 引用类型:`.weakKeys()`、`.weakValues()`等 4. 实现缓存操作: - 查询:`getIfPresent()`、`get()`、`getAll()` - 添加:`put()` - 清除:`invalidate()`、`invalidateAll()` 项目通常根据业务需求选择合适的缓存策略,配置适当的容量和过期时间,以提高服务吞吐率和降低响应时间。
点赞
回复 取消回复
干净的花瓣 2024年05月14日 10:03
厉害牛
点赞
回复 取消回复
梦里看花 2024年01月25日 11:35
真的腻害
1
回复 取消回复

目录