java redis caching: Cache Docs in Java with Redis
In modern web applications, serving the same converted document repeatedly can waste CPU cycles and inflate response times. java redis caching solves this problem by storing the conversion output in a fast, in‑memory data store, so subsequent requests are served instantly. In this tutorial you’ll learn how to wire Redis into a GroupDocs.Conversion workflow, configure TTLs, and measure the performance gains you can expect.
Quick Answers
- What does this tutorial cover? A complete java redis caching tutorial that integrates Redis with GroupDocs.Conversion.
- Why use Redis? It delivers sub‑millisecond latency, supports TTL expiration, and scales horizontally across multiple app instances.
- Do I need a GroupDocs license? A trial or temporary license is fine for testing; a full license is required for production deployments.
- What are the main steps? Add Maven dependencies, configure a
JedisPool, build cache helper methods, and plug the cache into the conversion pipeline. - Which Java version is supported? Java 8+ (compatible with the latest GroupDocs.Conversion releases).
What is caching documents with Redis?
Caching documents with Redis means persisting the binary output of a conversion (e.g., a PDF byte array) in Redis so that identical future requests can retrieve the cached bytes instead of re‑running the conversion engine. This eliminates redundant CPU work, reduces network bandwidth, and delivers a smoother end‑user experience.
Why implement Redis cache in Java?
Load your document once, store the result, and serve it instantly on repeat hits. Redis‑backed caching can cut conversion time by up to 90 % for frequently accessed files, lower infrastructure costs by reducing CPU usage, and provide a single source of truth for all application nodes in a clustered environment.
Prerequisites
- GroupDocs.Conversion – version 25.2 or newer (supports 120+ input and output formats).
- Jedis (the official Redis client for Java).
- A running Redis instance (local development can use the default
localhost:6379). - Maven for dependency management.
- Basic familiarity with Java exception handling and I/O streams.
Setting Up GroupDocs.Conversion for Java
GroupDocs.Conversion is a Java library that converts and renders documents to a wide range of formats, handling layout preservation, font embedding, and image extraction automatically.
Add the GroupDocs repository and dependency to your pom.xml:
<repositories>
<repository>
<id>groupdocs-maven</id>
<url>https://repo.groupdocs.com/maven</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.groupdocs</groupId>
<artifactId>groupdocs-conversion</artifactId>
<version>25.2.0</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>4.2.3</version>
</dependency>
</dependencies>
License acquisition
You can start with a Free Trial, request a Temporary License for evaluation, or purchase a full License for production use.
Initialize GroupDocs.Conversion in your Java code:
import com.groupdocs.conversion.*;
ConversionConfig config = new ConversionConfig();
config.setLicensePath("path/to/license/file.lic");
ConversionApi conversionApi = new ConversionApi(config);
Implementation Guide
Creating a Custom Cache Using Redis
Overview
A custom Redis cache holds rendered document bytes, allowing instant retrieval on repeat requests.
Setting Up JedisPool
JedisPool is a thread‑safe pool of reusable Redis connections that minimizes socket overhead and improves throughput.
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxTotal(20); // maximum active connections
poolConfig.setMaxIdle(10); // maximum idle connections
poolConfig.setMinIdle(2); // minimum idle connections
JedisPool jedisPool = new JedisPool(poolConfig, "localhost", 6379);
Storing and Retrieving Cached Data
The helper methods below serialize a byte array to a Base64 string for safe storage and retrieve it back into a byte array.
import java.util.Base64;
import redis.clients.jedis.Jedis;
public class RedisCacheHelper {
private final JedisPool pool;
private final int ttlSeconds; // time‑to‑live for cached entries
public RedisCacheHelper(JedisPool pool, int ttlSeconds) {
this.pool = pool;
this.ttlSeconds = ttlSeconds;
}
public void put(String key, byte[] data) {
try (Jedis jedis = pool.getResource()) {
String encoded = Base64.getEncoder().encodeToString(data);
jedis.setex(key, ttlSeconds, encoded); // configure redis ttl
}
}
public byte[] get(String key) {
try (Jedis jedis = pool.getResource()) {
String encoded = jedis.get(key);
return encoded != null ? Base64.getDecoder().decode(encoded) : null;
}
}
}
Integration with GroupDocs.Conversion
Now tie the cache into the conversion workflow. The method checks the cache first; if a miss occurs, it performs the conversion, stores the result, and returns the bytes.
import com.groupdocs.conversion.options.convertoptions.PdfConvertOptions;
public class DocumentService {
private final ConversionApi conversionApi;
private final RedisCacheHelper cacheHelper;
public DocumentService(ConversionApi conversionApi, RedisCacheHelper cacheHelper) {
this.conversionApi = conversionApi;
this.cacheHelper = cacheHelper;
}
public byte[] convertToPdf(String sourcePath, PdfConvertOptions options) throws Exception {
// Build a deterministic cache key
String cacheKey = "pdf:" + sourcePath + ":" + options.hashCode();
// Attempt to fetch from Redis
byte[] cached = cacheHelper.get(cacheKey);
if (cached != null) {
// Cache hit – return stored bytes
return cached;
}
// Cache miss – perform conversion
byte[] result = conversionApi.convert(sourcePath, options).toByteArray();
// Store result for future calls
cacheHelper.put(cacheKey, result);
return result;
}
}
How to implement java redis caching?
ConversionApi is the primary class in GroupDocs.Conversion that executes document conversion operations.
Load your source document, generate a deterministic cache key, look it up in Redis, and only invoke ConversionApi when the key is absent. This pattern guarantees that each unique conversion is performed once, then served from cache for the duration of the configured TTL.
Troubleshooting Tips
- Verify the Redis server is reachable (
redis-cli pingshould returnPONG). - Ensure
JedisPoolhost and port match your Redis deployment. - Wrap cache calls in try‑catch blocks to handle connectivity hiccups without breaking the conversion flow.
- Monitor Redis memory (
INFO memory) and setmaxmemorypolicies (e.g.,volatile-lru) to evict old entries gracefully. - If you encounter
OutOfMemoryErroron the JVM, increase heap size or enable-XX:+UseCompressedOops.
Practical Applications
- High‑traffic portals – Serve frequently requested PDFs (catalogs, whitepapers) instantly.
- Enterprise DMS – Reduce load when users repeatedly view the same contracts or policy documents.
- E‑commerce – Cache generated invoices or product catalogs to speed up checkout.
- Learning platforms – Deliver lecture notes and e‑books without re‑rendering on every student request.
- Legal services – Accelerate distribution of case files while keeping storage costs low.
Performance Considerations
- Tune Redis – Adjust
maxmemory, choose an eviction policy likeallkeys-lru, and set appropriatetimeoutvalues based on your traffic pattern. - Track cache hit/miss ratios – Use
INFO statsor Redis’skeyspace_hits/keyspace_missescounters to fine‑tune TTLs. - JVM heap sizing – Ensure the heap can accommodate GroupDocs buffers; a rule of thumb is 1 GB heap for every 100 MB of concurrent conversion payload.
- Batch conversions – When converting many files, reuse a single
Jedisinstance per thread to minimise socket churn.
Frequently Asked Questions
Q: Can I use this approach with other GroupDocs output formats?
A: Absolutely. The same caching pattern works for DOCX, HTML, images, and more – just change the ConvertOptions type.
Q: How do I choose a good cache key?
A: Combine the source file path, conversion options, and any version identifiers. This guarantees uniqueness per configuration.
Q: What if a document changes after it’s cached?
A: Invalidate the cache manually (e.g., delete the key) or use a shorter TTL so stale data expires quickly.
Q: Is Redis the only option for caching?
A: No, but Redis offers low latency, built‑in TTL, and wide Java client support, making it a popular choice for this scenario.
Q: Does this increase memory usage on the application server?
A: Minimal. The heavy lifting is done by Redis; the app only holds short‑lived connections via Jedis.
Conclusion
You now have a complete java redis caching tutorial that shows how to cache documents using Redis and GroupDocs.Conversion. By persisting rendered output in Redis, you’ll boost rendering performance, reduce conversion time, and provide a smoother experience to end users. Experiment with different TTL values, monitor cache metrics, and extend the pattern to other document formats as your application grows.
Last Updated: 2026-07-19
Tested With: GroupDocs.Conversion 25.2, Jedis 4.2.3
Author: GroupDocs
<repositories>
<repository>
<id>repository.groupdocs.com</id>
<name>GroupDocs Repository</name>
<url>https://releases.groupdocs.com/conversion/java/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.groupdocs</groupId>
<artifactId>groupdocs-conversion</artifactId>
<version>25.2</version>
</dependency>
</dependencies>
import com.groupdocs.conversion.Converter;
import com.groupdocs.conversion.options.convert.PdfConvertOptions;
public class DocumentConversion {
public static void main(String[] args) {
// Initialize the Converter with a document path
Converter converter = new Converter("input.docx");
// Set up conversion options for PDF
PdfConvertOptions options = new PdfConvertOptions();
converter.convert("output.pdf", options);
}
}
import redis.clients.jedis.JedisPool;
public class CacheManager {
private static JedisPool jedisPool = new JedisPool("localhost", 6379);
public static void main(String[] args) {
// Additional cache setup code here
}
}
import redis.clients.jedis.Jedis;
public class CacheManager {
public static void storeDocument(String key, String documentContent) {
try (Jedis jedis = jedisPool.getResource()) {
// Set the content in Redis cache with an expiration time of one hour
jedis.setex(key, 3600, documentContent);
}
}
public static String retrieveDocument(String key) {
try (Jedis jedis = jedisPool.getResource()) {
return jedis.get(key); // Retrieve cached content if available
}
}
}
public class DocumentConversion {
public static void convertWithCache(String inputPath, String outputPath) {
Converter converter = new Converter(inputPath);
PdfConvertOptions options = new PdfConvertOptions();
// Generate a cache key based on the document path and conversion settings
String cacheKey = "doc:" + inputPath;
// Check if the converted document is already cached
String cachedDocument = CacheManager.retrieveDocument(cacheKey);
if (cachedDocument != null) {
System.out.println("Using cached version of the document.");
// Save cached content to output file
Files.write(Paths.get(outputPath), cachedDocument.getBytes());
} else {
// Perform conversion and cache the result
converter.convert(output -> {
String documentContent = new String(output.toByteArray());
CacheManager.storeDocument(cacheKey, documentContent);
Files.write(Paths.get(outputPath), output.toByteArray());
}, options);
}
}
public static void main(String[] args) {
convertWithCache("input.docx", "output.pdf");
}
}