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

用什么网站可以做电子书苏州网站建设公司

用什么网站可以做电子书,苏州网站建设公司,海拉尔网站开发,网站建设管理工作在现代企业级应用中,缓存是提升系统性能和响应速度的关键技术。通过减少数据库查询或复杂计算的频率,缓存可以显著优化用户体验。Spring Boot 3.4.3 提供了强大的缓存抽象支持,而 Caffeine 作为一款高性能的本地缓存库,因其优异的…

在现代企业级应用中,缓存是提升系统性能和响应速度的关键技术。通过减少数据库查询或复杂计算的频率,缓存可以显著优化用户体验。Spring Boot 3.4.3 提供了强大的缓存抽象支持,而 Caffeine 作为一款高性能的本地缓存库,因其优异的吞吐量和灵活的配置,成为许多开发者的首选。本文将详细介绍如何在 Spring Boot 3.4.3 中集成 Caffeine 实现本地缓存功能,并提供完整的代码示例,助你在2025年的开发实践中快速落地高效缓存方案。


1. Caffeine 简介
1.1 什么是 Caffeine?

Caffeine 是一个基于 Java 的高性能本地缓存库,旨在替代传统的 Guava Cache。它采用了 Window TinyLFU(W-TinyLFU)淘汰算法,提供更高的命中率和更低的内存占用。Caffeine 支持丰富的配置选项,如过期策略、容量限制和异步加载,广泛应用于需要快速响应的场景。

1.2 Caffeine 的优点
  • 高性能:优于 Guava Cache 和 ConcurrentHashMap 的吞吐量。
  • 灵活性:支持按时间、大小和引用淘汰数据。
  • Spring 集成:与 Spring Cache 无缝衔接,注解驱动开发。
  • 轻量级:无需外部服务,适用于本地缓存需求。
1.3 适用场景
  • 频繁查询的静态数据(如配置信息)。
  • 计算代价高昂的结果缓存(如复杂算法)。
  • 高并发场景下的热点数据存储。

2. 项目实战

以下是基于 Spring Boot 3.4.3 和 Caffeine 实现本地缓存的完整步骤。

2.1 添加 Maven 依赖

pom.xml 中添加必要的依赖:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.4.3</version></parent><artifactId>springboot-caffeine</artifactId><properties><maven.compiler.source>17</maven.compiler.source><maven.compiler.target>17</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><!-- Spring Boot Web --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- Spring Cache --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId></dependency><!-- Caffeine --><dependency><groupId>com.github.ben-manes.caffeine</groupId><artifactId>caffeine</artifactId><version>3.1.8</version></dependency></dependencies>
</project>

说明:

  • spring-boot-starter-cache:提供 Spring Cache 抽象支持。
  • caffeine:核心缓存库,版本 3.1.8 是当前稳定版。
2.2 配置 Caffeine 缓存

创建一个配置类,定义 Caffeine 的缓存属性:

package cn.joyous.config;import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.cache.CacheManager;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import java.util.concurrent.TimeUnit;@Configuration
public class CacheConfig {@Beanpublic CacheManager cacheManager() {CaffeineCacheManager cacheManager = new CaffeineCacheManager();cacheManager.setCaffeine(Caffeine.newBuilder().initialCapacity(100)           // 初始容量.maximumSize(1000)             // 最大条目数.expireAfterWrite(10, TimeUnit.MINUTES) // 写入后10分钟过期.recordStats());               // 记录缓存统计信息return cacheManager;}
}

代码说明:

  • initialCapacity:初始化缓存容量。
  • maximumSize:限制最大缓存条目,超出时按 W-TinyLFU 淘汰。
  • expireAfterWrite:设置过期时间。
  • recordStats:启用统计,可用于监控命中率。
2.3 创建服务层

定义一个服务类,使用缓存注解:

package cn.joyous.service;import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;@Service
public class UserService {@Cacheable(value = "users", key = "#id")public String getUserById(Long id) {// 模拟数据库查询System.out.println("从数据库查询用户: " + id);return "User-" + id;}@CacheEvict(value = "users", key = "#id")public void deleteUser(Long id) {// 模拟删除操作System.out.println("删除用户: " + id);}
}

注解说明:

  • @Cacheable:查询时优先从缓存获取,若无则执行方法并缓存结果。
  • @CacheEvict:删除时清理指定缓存。
  • value:缓存名称,key:缓存键。
2.4 创建控制器

创建一个 RESTful 接口调用服务:

package cn.joyous.controller;import cn.itbeien.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;@RestController
@RequestMapping("/api")
public class UserController {@Autowiredprivate UserService userService;@GetMapping("/user/{id}")public String getUser(@PathVariable Long id) {return userService.getUserById(id);}@DeleteMapping("/user/{id}")public String deleteUser(@PathVariable Long id) {userService.deleteUser(id);return "User " + id + " deleted";}
}
2.5 启用缓存

在启动类上添加 @EnableCaching

package cn.joyous;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;@SpringBootApplication
@EnableCaching
public class CaffeineApplication {public static void main(String[] args) {SpringApplication.run(CaffeineApplication.class, args);}
}
2.6 测试缓存功能
  1. 启动应用。
  2. 访问 http://localhost:8080/api/user/1,控制台输出“从数据库查询用户: 1”。
  3. 再次访问相同 URL,无控制台输出,说明命中缓存。
  4. 调用 DELETE http://localhost:8080/api/user/1,缓存被清除,下次查询重新加载。

3. 进阶功能(可选)
  1. 多级缓存配置
    支持不同缓存策略:

    @Bean
    public CacheManager cacheManager() {CaffeineCacheManager cacheManager = new CaffeineCacheManager("users", "configs");cacheManager.setCaffeineCache("users", Caffeine.newBuilder().maximumSize(1000).expireAfterWrite(10, TimeUnit.MINUTES).build());cacheManager.setCaffeineCache("configs", Caffeine.newBuilder().maximumSize(500).expireAfterAccess(1, TimeUnit.HOURS).build());return cacheManager;
    }
    
  2. 异步加载
    支持异步缓存填充:

    @Bean
    public CacheManager cacheManager() {CaffeineCacheManager cacheManager = new CaffeineCacheManager();cacheManager.setCaffeine(Caffeine.newBuilder().expireAfterWrite(10, TimeUnit.MINUTES).maximumSize(1000));cacheManager.setAsyncCache(true);return cacheManager;
    }
    
  3. 缓存统计
    查看命中率:

    @Autowired
    private CacheManager cacheManager;public void printStats() {CaffeineCache cache = (CaffeineCache) cacheManager.getCache("users");System.out.println(cache.getNativeCache().stats());
    }
    

4. 总结

Spring Boot 3.4.3 结合 Caffeine 提供了一种高效的本地缓存实现方案。通过简单的配置和注解,你可以在项目中快速集成缓存功能,提升性能并减少数据库压力。Caffeine 的高性能和灵活性使其成为替代传统缓存工具的理想选择。

http://www.dtcms.com/wzjs/395066.html

相关文章:

  • 只买域名怎么做网站网络培训平台
  • 做网站怎么签订协议无屏蔽搜索引擎
  • 加人引流加人网站怎么做刷评论网站推广
  • 公司新闻网页制作软件seo基本概念
  • 做网站设计管理的专业互联网网络推广公司
  • 网站日常维护2022当下社会热点话题
  • 网站建设和空间品牌运营岗位职责
  • 网站建设一般多少钱seo平台是什么意思
  • 绵阳网站建设报价seo发包技术教程
  • 如何做网站后台管理网站外部优化的4大重点
  • 软件班级网站建设主题企业网站营销的典型案例
  • php怎么做网站百度一下网页搜索
  • 网站与客户端的区别吗seo做关键词怎么收费的
  • 资金盘网站开发公司哪里好广州google推广
  • 揭阳网站制作专业拉新十大推广app平台
  • 扬州专业做网站企业淄博头条新闻今天
  • vuejs做视频网站农业推广
  • 怎样推广公司的网站网站开发报价方案
  • wordpress 密码会变seo顾问服务公司站长
  • 广告影视制作谁家好新手学seo
  • 网站设计方案怎么写小吃培训机构排名前十
  • 政府邀请招标网站建设文件范本宁波seo公司推荐
  • 昆明网站建设推荐seo关键词排名优化的方法
  • 郑州富士康小时工海南seo
  • 网站制作方案专业乐云seo什么叫做关键词
  • 深圳网站建设大全百度引流推广怎么收费
  • 网站关键词搜不到百度域名提交收录网址
  • 长沙专业做网站较好的公司企业营销策略分析论文
  • apache 创建网站武汉关键词seo
  • 华丽的网站模板宁德市疫情最新消息