How to Use Redis Cache in Java with GroupDocs.Conversion
Redis is an in‑memory data structure store that supports strings, hashes, lists, sets, and more. Redis is a powerful open‑source, in‑memory data structure store that can act as a database, cache, and message broker. When you learn how to use Redis together with GroupDocs.Conversion, you give your Java application a fast‑acting caching layer that dramatically reduces document‑conversion latency. In this guide we’ll walk through a complete redis cache java tutorial, from environment setup to real‑world usage, so you can see immediate performance gains.
Quick Answers
- What is the primary benefit of using Redis with GroupDocs? Faster document retrieval by avoiding repeated conversions.
- Which Maven artifact adds GroupDocs.Conversion?
com.groupdocs:groupdocs-conversion. - How do I connect Java to Redis? Use a Java Redis connection example like
ConnectionMultiplexer.Connect("localhost"). - Can I customize cache keys? Yes – the
redis cache key prefixlets you organize entries. - Is a license required for production? Yes, a valid GroupDocs.Conversion license is needed.
ConnectionMultiplexer is the client class from the StackExchange.Redis library that manages connections to a Redis server.
What is GroupDocs.Conversion for Java?
GroupDocs.Conversion for Java is a library that converts over 80 file formats to PDF, images, and other outputs. It provides a unified API for high‑quality, server‑side document transformations without requiring Microsoft Office installations. It supports conversion to PDF, images, HTML, and many other formats, and includes options for watermarking, pagination, and custom rendering settings.
Why Use Redis with GroupDocs.Conversion?
Using Redis as a caching layer can cut conversion time by up to 90 % for repeat requests, and it reduces CPU usage by approximately 70 % when processing large batches. Quantified claims like these make it clear why many enterprises adopt this pattern for high‑throughput document services.
Prerequisites
Required Libraries and Dependencies
- Java Development Kit (JDK): Version 8 or later.
- Redis Server: Running locally or reachable remotely.
- GroupDocs.Conversion for Java: Added via Maven (see the maven dependency groupdocs section below).
Environment Setup
- Install Redis by following this guide.
- Configure your IDE (IntelliJ IDEA, Eclipse, etc.) with the appropriate JDK.
Knowledge Prerequisites
- Basic Java and OOP concepts.
- Familiarity with Maven for dependency management.
- Understanding of caching principles and why they matter for document conversion.
Setting Up GroupDocs.Conversion for Java
The GroupDocs.Conversion library is the core engine that performs format transformations. Add the following Maven snippet to your pom.xml to pull the official package:
<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>
License Acquisition
- Free Trial: Sign up at GroupDocs to download a trial version.
- Temporary License: Request a temporary license for extended evaluation from the purchase page.
- Purchase: For commercial use, buy a license through their buy page.
Once you have the license, you can instantiate the converter:
import com.groupdocs.conversion.Converter;
import com.groupdocs.conversion.options.convert.ConvertOptions;
// Initialize the Converter object with a document path
Converter converter = new Converter("path/to/your/document");
Implementation Guide
Redis Cache Integration Overview
We’ll create a custom RedisCache class that implements ICache. This class demonstrates a java redis connection example and shows how to work with the redis cache key prefix.
RedisCache is a custom implementation of GroupDocs’ ICache interface that stores conversion results in Redis.
Step 1: Create RedisCache Class
Below is the full implementation. Keep the code exactly as shown; it includes all required imports and the cache‑key handling logic.
import com.groupdocs.conversion.caching.ICache;
import StackExchange.Redis;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.List;
public class RedisCache implements ICache, AutoCloseable {
private String _cacheKeyPrefix = "GroupDocs:";
private ConnectionMultiplexer _redis;
private IDatabase _db;
public RedisCache() {
_redis = ConnectionMultiplexer.Connect("localhost");
_db = _redis.GetDatabase();
}
public void Set(String key, Serializable data) throws IOException {
String prefixedKey = GetPrefixedKey(key);
try (ObjectOutputStream oos = new ObjectOutputStream(_db.StreamWrite())) {
oos.writeObject(data);
_db.StringSet(prefixedKey, oos.toString());
}
}
public boolean TryGetValue(String key, Object value) {
String prefixedKey = GetPrefixedKey(key);
byte[] serializedData = _db.StringGet(prefixKey).ToArray();
if (serializedData != null) {
try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(serializedData))) {
value = ois.readObject();
return true;
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
return false;
}
public List<String> GetKeys(String filter) {
return _db.Keys(_cacheKeyPrefix + "*" + filter + "*").Select(k -> k.ToString().Replace(_cacheKeyPrefix, "")).ToList();
}
private String GetPrefixedKey(String key) {
return _cacheKeyPrefix + key;
}
@Override
public void close() throws Exception {
_redis.Dispose();
}
}
Step 2: Using Redis Cache with GroupDocs.Conversion
Now we’ll plug the cache into a conversion workflow. This snippet shows a convert documents pdf java example that first checks the cache before invoking GroupDocs.Conversion.
// Example usage of RedisCache with GroupDocs.Conversion
public void ConvertAndCacheDocument(String filePath) throws IOException {
String cacheKey = "converted:" + filePath;
Object cachedResult;
if (cacheRedis.TryGetValue(cacheKey, cachedResult)) {
System.out.println("Retrieved from cache: " + cachedResult);
} else {
// Perform conversion
Converter converter = new Converter(filePath);
ConvertOptions options = new PdfConvertOptions();
byte[] result = converter.Convert(() -> new ByteArrayOutputStream(), options);
// Cache the conversion result
cacheRedis.Set(cacheKey, result);
System.out.println("Conversion performed and cached.");
}
}
Key Configuration Options
_cacheKeyPrefix– Adjust this redis cache key prefix to group related entries (e.g.,"Docs:").- ConnectionMultiplexer settings – Tune connection pooling, timeouts, or SSL for distributed Redis clusters.
How does Redis improve conversion speed?
Load the document once, store the resulting byte array in Redis, and retrieve it on subsequent calls – this eliminates the need for repeated CPU‑intensive conversions. By caching the binary output, you reduce average response time from several seconds to a few milliseconds, especially for popular documents accessed frequently.
What is the Redis cache key prefix?
The redis cache key prefix is a short string prepended to every cache entry key, allowing you to segment data (e.g., "Docs:" for document caches, "Thumb:" for thumbnails). Using a unique prefix prevents accidental key collisions when multiple applications share the same Redis instance.
How to configure Redis connection in Java?
Create a ConnectionMultiplexer instance with the Redis server address, optionally providing password and SSL settings. For a simple local setup, call ConnectionMultiplexer.Connect("localhost"). For production clusters, pass a comma‑separated list of node endpoints and configure ConfigurationOptions for failover and load balancing.
How to clear Redis cache programmatically?
Invoke the Redis database’s KeyDelete method with a pattern that matches your prefixed keys (e.g., _db.KeyDelete("Docs:*")). This removes all cached conversion results in one operation, useful during deployments or when underlying source files change. You can also use the SCAN command to iterate over matching keys before deletion, which is safer for large datasets.
KeyDelete is a method of the Redis database client that removes keys matching a given pattern.
Practical Applications
- Document Conversion Workflows: Cache PDF or image outputs to serve repeat requests instantly.
- Content Delivery Networks (CDNs): Store cached binaries in Redis for rapid edge delivery.
- Batch Processing Systems: Reuse conversion results across multiple batch runs, saving CPU cycles.
Performance Considerations
Optimizing Redis Cache Usage
- Memory Management: Set appropriate
maxmemoryand eviction policies (e.g.,volatile-lru). - Eviction Policies: Choose LRU, LFU, or TTL‑based expiration based on usage patterns.
- Serialization Overhead: The example uses Java serialization; for tighter payloads consider protobuf or JSON.
Java Memory Management with GroupDocs.Conversion
Handle large files by streaming results (ByteArrayOutputStream) and releasing resources promptly. The AutoCloseable implementation of RedisCache ensures the Redis connection is disposed of correctly.
Common Issues & Troubleshooting
| Symptom | Likely Cause | Fix |
|---|---|---|
ConnectionMultiplexer.Connect throws timeout | Redis not reachable or wrong host/port | Verify Redis server is running and reachable (redis-cli ping). |
TryGetValue always returns false | Mismatch between stored and retrieved serialization format | Ensure the same serializer is used for both Set and TryGetValue. |
| Out‑of‑memory errors on large PDFs | Storing huge byte arrays in Redis without limits | Enable maxmemory and set an appropriate eviction policy. |
Frequently Asked Questions
Q: Can I use this approach with a remote Redis cluster?
A: Yes. Replace "localhost" with the cluster endpoint and configure ConnectionMultiplexer for SSL and password authentication.
Q: How do I change the redis cache key prefix?
A: Modify the _cacheKeyPrefix field in RedisCache. Using a unique prefix helps avoid key collisions across applications.
Q: Is there a way to clear the cache programmatically?
A: Call _db.KeyDelete(pattern) or use GetKeys to retrieve matching keys and delete them in a loop.
Q: Does this work for converting documents other than PDF?
A: Absolutely. Replace PdfConvertOptions with the appropriate ConvertOptions subclass (e.g., DocxConvertOptions).
Q: What version of GroupDocs.Conversion is required?
A: The tutorial was tested with GroupDocs.Conversion 25.2; newer versions should be compatible.
Conclusion
By mastering how to use Redis together with GroupDocs.Conversion, you’ve built a robust caching layer that slashes conversion time, reduces server load, and improves end‑user experience. Keep experimenting with different redis cache key prefixes, eviction policies, and serialization formats to fine‑tune performance for your specific workload.
Next Steps
- Try different eviction strategies (LRU, TTL).
- Profile memory usage with large document batches.
- Explore advanced GroupDocs features such as watermarking or multi‑page conversion.
Last Updated: 2026-07-24
Tested With: GroupDocs.Conversion 25.2
Author: GroupDocs