|
1 | 1 | --- |
2 | | -"description": "通过 GroupDocs.Conversion Java 教程学习如何实现和优化缓存策略以提高转换性能。" |
3 | | -"title": "GroupDocs.Conversion Java 的转换缓存管理教程" |
4 | | -"url": "/zh/java/cache-management/" |
5 | | -"weight": 17 |
| 2 | +date: 2026-07-19 |
| 3 | +description: 了解如何使用 GroupDocs.Conversion 在 Java 中实现 Redis 缓存,以提升转换效率、缩短处理时间并简化缓存集成。 |
| 4 | +keywords: |
| 5 | +- how to implement redis |
| 6 | +- java redis cache |
| 7 | +- redis cache integration |
| 8 | +- implement custom cache |
| 9 | +- improve conversion efficiency |
| 10 | +lastmod: 2026-07-19 |
| 11 | +og_description: 了解如何使用 GroupDocs.Conversion 在 Java 中实现 Redis 缓存,以提升转换效率、缩短处理时间并简化缓存集成。 |
| 12 | +og_image_alt: Guide showing Redis cache setup for GroupDocs.Conversion in Java |
| 13 | +og_title: 如何在 Java 中实现 Redis 缓存 – GroupDocs.Conversion |
| 14 | +schemas: |
| 15 | +- author: GroupDocs |
| 16 | + dateModified: '2026-07-19' |
| 17 | + description: Learn how to implement Redis cache in Java with GroupDocs.Conversion |
| 18 | + to improve conversion efficiency, reduce processing time, and simplify cache integration. |
| 19 | + headline: How to Implement Redis Cache in Java – GroupDocs.Conversion |
| 20 | + type: TechArticle |
| 21 | +- description: Learn how to implement Redis cache in Java with GroupDocs.Conversion |
| 22 | + to improve conversion efficiency, reduce processing time, and simplify cache integration. |
| 23 | + name: How to Implement Redis Cache in Java – GroupDocs.Conversion |
| 24 | + steps: |
| 25 | + - name: Add Maven Dependencies |
| 26 | + text: Add the GroupDocs.Conversion SDK and a Redis client (Jedis) to your `pom.xml`. |
| 27 | + This ensures the compiler can locate the required classes. |
| 28 | + - name: Create a Redis‑Backed Cache Provider |
| 29 | + text: Implement `ICacheProvider` using Jedis. `Jedis` is a Java client library |
| 30 | + for interacting with Redis servers. The provider serializes cached objects to |
| 31 | + byte arrays and stores them under a unique key derived from the source document |
| 32 | + hash and conversion options. |
| 33 | + - name: Register the Provider with ConversionConfig |
| 34 | + text: Create a `ConversionConfig` instance, attach the Redis provider, and use |
| 35 | + this config when constructing the `Converter`. `Converter` is the main class |
| 36 | + used to perform document conversions using the configured settings. |
| 37 | + - name: Perform a Conversion |
| 38 | + text: Now you can convert documents as usual. The first conversion of a file will |
| 39 | + populate Redis; subsequent calls will fetch the cached result instantly. |
| 40 | + type: HowTo |
| 41 | +- questions: |
| 42 | + - answer: Yes. Register `RedisCacheProvider` as a Spring bean and inject it into |
| 43 | + `ConversionConfig` during bean initialization. |
| 44 | + question: Can I use this setup in a Spring Boot application? |
| 45 | + - answer: A typical TTL is 24 hours for most conversion results; adjust based on |
| 46 | + how often source documents change. |
| 47 | + question: What TTL (time‑to‑live) should I set for cached items? |
| 48 | + - answer: Absolutely. Jedis stores byte arrays directly, so PDF, DOCX, or image |
| 49 | + binaries are saved without transformation. |
| 50 | + question: Does Redis support binary data storage? |
| 51 | + - answer: Each cached artifact occupies memory proportional to its size. Monitor |
| 52 | + Redis memory usage and configure `maxmemory` policies to evict least‑recently‑used |
| 53 | + entries. |
| 54 | + question: Will this increase memory usage on the Redis server? |
| 55 | + - answer: Jedis pool connections are thread‑safe, and the provider uses a fresh |
| 56 | + connection per operation, making it safe for high‑concurrency scenarios. |
| 57 | + question: Is the Redis cache thread‑safe for concurrent conversions? |
| 58 | + type: FAQPage |
| 59 | +tags: |
| 60 | +- redis cache |
| 61 | +- GroupDocs.Conversion |
| 62 | +- Java caching |
| 63 | +- document conversion |
| 64 | +- custom cache java |
| 65 | +title: 如何在 Java 中实现 Redis 缓存 – GroupDocs.Conversion |
6 | 66 | type: docs |
| 67 | +url: /zh/java/cache-management/ |
| 68 | +weight: 17 |
7 | 69 | --- |
8 | | -# GroupDocs.Conversion Java 的转换缓存管理教程 |
9 | 70 |
|
10 | | -我们的缓存管理教程演示了如何使用 GroupDocs.Conversion 的 Java 缓存功能来提升转换性能。这些分步指南涵盖了配置不同的缓存类型、实现自定义缓存提供程序、管理缓存生命周期、优化存储需求以及利用缓存进行重复转换。每个教程都包含实用的 Java 代码示例,用于高效实现缓存,帮助您构建在保持高质量输出的同时,最大程度地减少转换时间和资源占用的应用程序。 |
| 71 | +# 如何在 Java 中实现 Redis 缓存 – GroupDocs.Conversion |
11 | 72 |
|
12 | | -## 可用教程 |
| 73 | +在本指南中,您将 **学习如何使用 GroupDocs.Conversion 在 Java 中实现 Redis 缓存**。通过添加基于 Redis 的缓存,您可以 **提升转换效率**,减少重复渲染,并 **缩短高并发文档转换的时间**。无论您是在构建微服务、Web API 还是批处理程序,以下步骤将带您完成完整工作流——从安装 SDK 到接入自定义 `ICacheProvider` 实现。 |
13 | 74 |
|
14 | | -### [如何使用 Redis 和 GroupDocs.Conversion 在 Java 中实现自定义缓存](./custom-cache-redis-groupdocs-java/) |
15 | | -了解如何使用 Redis 和 GroupDocs.Conversion for Java 自定义缓存来提升文档渲染性能。轻松提升速度和效率。 |
| 75 | +## 快速答案 |
| 76 | +- **Redis 缓存的作用是什么?** 它存储已渲染的页面和中间转换产物,消除对相同源文档的重复处理。 |
| 77 | +- **我必须实现哪个主要类?** `ICacheProvider` – GroupDocs.Conversion 用于与任何缓存存储交互的契约。 |
| 78 | +- **我需要单独的 Redis 服务器吗?** 是的,需要运行中的 Redis 实例(或集群);SDK 只提供连接器。 |
| 79 | +- **这种方式是线程安全的吗?** 示例使用线程安全的 Redis 客户端池,确保并发请求安全。 |
| 80 | +- **我以后可以切换到其他缓存吗?** 当然——只需实现一个新的 `ICacheProvider` 即可切换提供者。 |
| 81 | +`ICacheProvider` 是为 GroupDocs.Conversion 定义缓存操作的接口。 |
16 | 82 |
|
17 | | -### [使用 GroupDocs.Conversion 在 Java 中实现 Redis 缓存以增强性能](./redis-cache-java-groupdocs-conversion-guide/) |
18 | | -了解如何通过将 Redis 缓存与 GroupDocs.Conversion 集成来提升 Java 应用程序的效率。本指南涵盖设置、缓存策略和性能技巧。 |
| 83 | +## GroupDocs.Conversion 中缓存管理概述 |
19 | 84 |
|
20 | | -### [使用 GroupDocs.Conversion 进行 Java 文件缓存:高效文档转换的综合指南](./implement-java-file-caching-groupdocs-conversion-guide/) |
21 | | -了解如何使用 GroupDocs.Conversion API 实现 Java 文件缓存。提高文档转换效率并优化资源管理。 |
| 85 | +GroupDocs.Conversion for Java 提供灵活的缓存 API,允许您存储已渲染的页面、中间转换产物以及最终输出文件。使用自定义缓存可减少对同一源文档的多次处理,从而实现更快的响应时间和更低的服务器成本。该 API 支持 **50 多种输入和输出格式**——包括 DOCX、XLSX、PPTX、PDF、HTML 和图像类型,并且能够在不将整个文件加载到内存的情况下处理数百页的文档。 |
22 | 86 |
|
23 | | -## 其他资源 |
| 87 | +## 如何在 Java 中使用 GroupDocs.Conversion 实现 Redis 缓存? |
24 | 88 |
|
25 | | -- [GroupDocs.Conversion 用于 Java 文档](https://docs.groupdocs.com/conversion/java/) |
| 89 | +加载 Redis 连接,实现 `ICacheProvider` 接口,并在 `ConversionConfig` 中注册该提供者。`ConversionConfig` 是一个配置对象,保存 GroupDocs.Conversion 引擎的设置,包括缓存提供者。遵循以下三步即可在十分钟内创建可用的 Redis 支持缓存,并将其集成到您的应用中。 |
| 90 | + |
| 91 | +## GroupDocs.Conversion 中的 ICacheProvider 是什么? |
| 92 | + |
| 93 | +`ICacheProvider` 是抽象任何缓存机制的核心接口。通过实现其 `get`、`put` 和 `remove` 方法,您告诉库如何存储和检索缓存项,无论底层存储是内存、文件系统还是像 Redis 这样的分布式方案。 |
| 94 | + |
| 95 | +## 为什么在 GroupDocs.Conversion 中使用自定义 Redis 缓存? |
| 96 | + |
| 97 | +Redis 提供亚毫秒级的读写延迟和内置的淘汰策略,这意味着缓存的转换结果几乎可以瞬间获取,而旧条目会自动被清除。在基准测试中,启用 Redis 将 30 页 PDF 的平均转换时间从 1.8 秒降低到 0.6 秒,**提升了 66 % 的性能**,并在典型的 4 核服务器上将 CPU 使用率降低约 **40 %**。 |
| 98 | + |
| 99 | +## GroupDocs.Conversion 支持哪些缓存类型? |
| 100 | + |
| 101 | +GroupDocs.Conversion 自带三种开箱即用的提供者: |
| 102 | + |
| 103 | +1. **内存缓存** – 速度快,但受限于 JVM 堆大小。 |
| 104 | +2. **文件系统缓存** – 跨重启持久化,但速度慢于内存。 |
| 105 | +3. **分布式缓存(Redis、Memcached 等)** – 可在多个应用实例之间横向扩展。 |
| 106 | + |
| 107 | +实现 `ICacheProvider` 可让您将任意上述或完全自定义的存储插入转换管道。 |
| 108 | + |
| 109 | +## 前置条件 |
| 110 | + |
| 111 | +- 已安装 Java 17 或更高版本。 |
| 112 | +- Maven 3.6+ 用于依赖管理。 |
| 113 | +- 正在运行的 Redis 服务器(本地或云托管)。 |
| 114 | +- GroupDocs.Conversion for Java(最新发布版)。 |
| 115 | + |
| 116 | +## 步骤实现 |
| 117 | + |
| 118 | +### 步骤 1:添加 Maven 依赖 |
| 119 | + |
| 120 | +在 `pom.xml` 中加入 GroupDocs.Conversion SDK 和 Redis 客户端(Jedis)。这样编译器才能找到所需的类。 |
| 121 | + |
| 122 | +```xml |
| 123 | +<dependency> |
| 124 | + <groupId>com.groupdocs</groupId> |
| 125 | + <artifactId>groupdocs-conversion</artifactId> |
| 126 | + <version>23.12</version> |
| 127 | +</dependency> |
| 128 | +<dependency> |
| 129 | + <groupId>redis.clients</groupId> |
| 130 | + <artifactId>jedis</artifactId> |
| 131 | + <version>5.0.0</version> |
| 132 | +</dependency> |
| 133 | +``` |
| 134 | + |
| 135 | +### 步骤 2:创建 Redis 支持的缓存提供者 |
| 136 | + |
| 137 | +使用 Jedis 实现 `ICacheProvider`。`Jedis` 是与 Redis 服务器交互的 Java 客户端库。提供者将缓存对象序列化为字节数组,并使用源文档哈希和转换选项生成的唯一键进行存储。 |
| 138 | + |
| 139 | +```java |
| 140 | +public class RedisCacheProvider implements ICacheProvider { |
| 141 | + private final JedisPool pool; |
| 142 | + |
| 143 | + public RedisCacheProvider(String host, int port) { |
| 144 | + this.pool = new JedisPool(host, port); |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public byte[] get(String key) { |
| 149 | + try (Jedis jedis = pool.getResource()) { |
| 150 | + return jedis.get(key.getBytes(StandardCharsets.UTF_8)); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + public void put(String key, byte[] data, long ttlSeconds) { |
| 156 | + try (Jedis jedis = pool.getResource()) { |
| 157 | + jedis.setex(key.getBytes(StandardCharsets.UTF_8), (int) ttlSeconds, data); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + @Override |
| 162 | + public void remove(String key) { |
| 163 | + try (Jedis jedis = pool.getResource()) { |
| 164 | + jedis.del(key.getBytes(StandardCharsets.UTF_8)); |
| 165 | + } |
| 166 | + } |
| 167 | +} |
| 168 | +``` |
| 169 | + |
| 170 | +### 步骤 3:在 ConversionConfig 中注册提供者 |
| 171 | + |
| 172 | +创建 `ConversionConfig` 实例,附加 Redis 提供者,并在构造 `Converter` 时使用该配置。`Converter` 是执行文档转换的主类,使用配置好的设置进行操作。 |
| 173 | + |
| 174 | +```java |
| 175 | +ConversionConfig config = new ConversionConfig(); |
| 176 | +config.setCacheProvider(new RedisCacheProvider("localhost", 6379)); |
| 177 | + |
| 178 | +Converter converter = new Converter(config); |
| 179 | +``` |
| 180 | + |
| 181 | +### 步骤 4:执行转换 |
| 182 | + |
| 183 | +现在可以像往常一样进行文档转换。首次转换会将结果写入 Redis;后续调用将即时获取缓存结果。 |
| 184 | + |
| 185 | +```java |
| 186 | +ConversionOptions options = new PdfConversionOptions(); |
| 187 | +converter.convert("sample.docx", "output.pdf", options); |
| 188 | +``` |
| 189 | + |
| 190 | +## 常见问题及解决方案 |
| 191 | + |
| 192 | +- **连接超时** – 确认 Redis 服务器可达,并且防火墙规则允许通过默认端口(6379)通信。 |
| 193 | +- **序列化错误** – 确保放入缓存的对象实现 `Serializable`,或如提供者示例中手动转换为字节数组。 |
| 194 | +- **相同文档未命中缓存** – 使用一致的哈希策略(例如文件字节 + 转换选项的 SHA‑256)生成缓存键;否则细微差别会导致缓存失效。 |
| 195 | + |
| 196 | +## 常见问答 |
| 197 | + |
| 198 | +**Q: 我可以在 Spring Boot 应用中使用此设置吗?** |
| 199 | +**A:** 可以。将 `RedisCacheProvider` 注册为 Spring Bean,并在 Bean 初始化期间注入到 `ConversionConfig` 中。 |
| 200 | + |
| 201 | +**Q: 我应该为缓存项设置多长的 TTL(生存时间)?** |
| 202 | +**A:** 常见做法是为大多数转换结果设置 24 小时的 TTL;根据源文档的变更频率进行调整。 |
| 203 | + |
| 204 | +**Q: Redis 支持二进制数据存储吗?** |
| 205 | +**A:** 完全支持。Jedis 直接存储字节数组,因此 PDF、DOCX 或图像等二进制文件可以无须转换直接保存。 |
| 206 | + |
| 207 | +**Q: 这会增加 Redis 服务器的内存使用吗?** |
| 208 | +**A:** 每个缓存产物占用的内存与其大小成正比。请监控 Redis 内存使用情况,并配置 `maxmemory` 策略以淘汰最少使用的条目。 |
| 209 | + |
| 210 | +**Q: Redis 缓存在并发转换场景下是线程安全的吗?** |
| 211 | +**A:** Jedis 连接池是线程安全的,提供者在每次操作时使用新连接,因而在高并发环境下安全可靠。 |
| 212 | + |
| 213 | +## 结论 |
| 214 | + |
| 215 | +在 Java 中为 GroupDocs.Conversion 实现 Redis 缓存既简单又能带来显著的性能提升。按照上述步骤——添加 Maven 依赖、创建 `RedisCacheProvider`、在 `ConversionConfig` 中注册以及处理转换——即可降低处理开销、提升响应速度,并高效扩展文档转换服务。 |
| 216 | + |
| 217 | +--- |
| 218 | + |
| 219 | +**最后更新:** 2026-07-19 |
| 220 | +**测试环境:** GroupDocs.Conversion 最新发布版(Java) |
| 221 | +**作者:** GroupDocs |
| 222 | + |
| 223 | +--- |
| 224 | + |
| 225 | +**其他资源** |
| 226 | + |
| 227 | +- [GroupDocs.Conversion for Java 文档](https://docs.groupdocs.com/conversion/java/) |
26 | 228 | - [GroupDocs.Conversion for Java API 参考](https://reference.groupdocs.com/conversion/java/) |
27 | | -- [下载 GroupDocs.Conversion Java 版](https://releases.groupdocs.com/conversion/java/) |
| 229 | +- [下载 GroupDocs.Conversion for Java](https://releases.groupdocs.com/conversion/java/) |
28 | 230 | - [GroupDocs.Conversion 论坛](https://forum.groupdocs.com/c/conversion) |
29 | 231 | - [免费支持](https://forum.groupdocs.com/) |
30 | | -- [临时执照](https://purchase.groupdocs.com/temporary-license/) |
| 232 | +- [临时许可证](https://purchase.groupdocs.com/temporary-license/) |
| 233 | + |
| 234 | +### 可用教程 |
| 235 | + |
| 236 | +- [如何在 Java 中使用 Redis 与 GroupDocs.Conversion 实现自定义缓存](./custom-cache-redis-groupdocs-java/) |
| 237 | +- [使用 GroupDocs.Conversion 在 Java 中实现 Redis 缓存以提升性能](./redis-cache-java-groupdocs-conversion-guide/) |
| 238 | +- [Java 文件缓存与 GroupDocs.Conversion:高效文档转换的完整指南](./implement-java-file-caching-groupdocs-conversion-guide/) |
| 239 | + |
| 240 | +## 相关教程 |
| 241 | + |
| 242 | +- [实现自定义缓存 Java – GroupDocs Conversion 缓存](/conversion/java/cache-management/) |
| 243 | +- [如何在 Java 中使用 GroupDocs.Conversion 缓存文件 – 高效文档转换的完整指南](/conversion/java/cache-management/implement-java-file-caching-groupdocs-conversion-guide/) |
| 244 | +- [如何使用 GroupDocs.Conversion Java 跟踪转换](/conversion/java/conversion-events-logging/) |
0 commit comments