How to Use Redis Cache in Java with GroupDocs.Conversion
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.
Introduction
Imagine a high‑traffic portal that serves PDFs generated from Word or Excel files on demand. Without caching, each request forces a fresh conversion, consuming CPU and I/O. By learning how to use Redis, you can store the converted byte arrays once and serve them instantly on subsequent requests. This not only speeds up response times but also reduces server load, leading to a smoother user experience.
What You’ll Learn
- Setting up Redis cache in Java
- Implementing a custom
RedisCacheclass (the java redis connection example) - Using GroupDocs.Conversion to convert documents and cache the results
- Tweaking the redis cache key prefix for better organization
- Performance‑tuning tips for production environments
Let’s get started with the prerequisites.
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
First, add the GroupDocs.Conversion library to your project. This Maven snippet is the official maven dependency groupdocs you need.
<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.
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.
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.
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-01-26
Tested With: GroupDocs.Conversion 25.2
Author: GroupDocs