Java Redis Cache Voorbeeld met GroupDocs.Conversion Gids

Redis is een in‑memory data‑store die kan fungeren als database, cache en message broker. Wanneer je het combineert met GroupDocs.Conversion voor Java, krijg je een krachtige combinatie die document‑conversiewerkbelastingen dramatisch versnelt. In deze tutorial zie je een java redis cache example die laat zien hoe je Redis instelt, het koppelt aan GroupDocs.Conversion, en de cache fijn‑afstemt met een redis cache key prefix. Aan het einde begrijp je waarom dit patroon belangrijk is en hoe je het in real‑world projecten kunt toepassen.

Quick Answers

  • What is the primary benefit? Reduces redundant document conversions and cuts response time.
  • Do I need a license? Yes, GroupDocs.Conversion requires a valid license for production use.
  • Which Redis client is used? The example relies on the StackExchange.Redis library (shown in code).
  • Can I run Redis locally? Absolutely—install it on your dev machine or use a remote instance.
  • Is the cache thread‑safe? The provided RedisCache class handles connections safely for typical web scenarios.

Introduction

Stel je een portal met veel verkeer voor dat gebruikers PDF’s laat bekijken die zijn gegenereerd uit Word-, Excel‑ of PowerPoint‑bestanden. Zonder caching dwingt elke aanvraag GroupDocs.Conversion om hetzelfde bron‑document opnieuw te verwerken, wat CPU‑cycli verbruikt en de latentie verhoogt. Door een java redis cache example in de conversiepijplijn te plaatsen, sla je de resulterende byte‑array één keer op en serveer je deze direct bij volgende aanvragen. Dit verbetert niet alleen de gebruikerservaring, maar verlaagt ook de infrastructuurkosten.

What is a java redis cache example?

Een java redis cache example toont hoe Java‑code kan communiceren met een Redis‑server om objecten op te slaan en op te halen — in ons geval de output van een documentconversie. Het patroon omvat meestal:

  1. Het genereren van een unieke cache‑sleutel (vaak gebaseerd op bestandsnaam, conversie‑opties en een redis cache key prefix).
  2. Controleren of Redis al een bestaande entry heeft voordat de conversie‑engine wordt aangeroepen.
  3. Het conversieresultaat terug opslaan in Redis voor toekomstige hits.

Why use Redis with GroupDocs.Conversion?

  • Speed: In‑memory reads zijn orders of magnitude sneller dan schijf‑I/O.
  • Scalability: Meerdere applicatie‑instances kunnen dezelfde cache delen, waardoor horizontale schaalbaarheid mogelijk is.
  • Flexibility: Redis ondersteunt eviction‑policies (LRU, TTL) die de cache‑grootte onder controle houden.

Prerequisites

Required Libraries and Dependencies

  1. Java Development Kit (JDK): Versie 8 of later.
  2. Redis Server: Draait lokaal (localhost:6379) of is extern toegankelijk.
  3. GroupDocs.Conversion for Java: Toegevoegd via Maven (zie volgende sectie).

Environment Setup

  • Installeer Redis door de this guide te volgen.
  • Configureer je IDE (IntelliJ IDEA, Eclipse, etc.) met de juiste JDK.

Knowledge Prerequisites

  • Basis Java‑ en OOP‑concepten.
  • Vertrouwdheid met Maven voor dependency‑beheer.
  • Begrip van caching‑fundamentals.

Setting Up GroupDocs.Conversion for Java

Maven Setup

Add the repository and dependency to your pom.xml:

<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

  1. Free Trial: Sign up at GroupDocs to download a trial version.
  2. Temporary License: Request a temporary license for extended evaluation from the purchase page.
  3. Purchase: For commercial use, purchase a license through their buy page.

Initializing 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 will handle serialization, key management (including the redis cache key prefix), and basic CRUD operations against Redis.

Step 1: Create RedisCache Class

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

// 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.");
    }
}

Configuring redis cache key prefix

The _cacheKeyPrefix field lets you group related entries (e.g., "GroupDocs:"). Adjust this value to match your naming conventions or multi‑tenant requirements.

Key Configuration Options

  • _cacheKeyPrefix: Customize to organize cache keys efficiently.
  • ConnectionMultiplexer settings: Tune for connection pooling, SSL, or distributed Redis clusters.

Practical Applications

  1. Document Conversion Workflows: Cache converted PDFs, images, or HTML to avoid repeated processing.
  2. Content Delivery Networks (CDNs): Serve cached documents from edge locations for faster user access.
  3. Batch Processing Systems: Store intermediate results, enabling resume‑able pipelines.

Performance Considerations

Optimizing Redis Cache Usage

  • Memory Management: Set maxmemory and appropriate eviction policies (e.g., volatile-lru).
  • Eviction Policies: Choose LRU, LFU, or TTL based on your usage pattern.
  • Serialization Overhead: Consider binary serializers (e.g., Kryo) for large payloads.

Java Memory Management with GroupDocs.Conversion

Handle large files by streaming conversions directly to ByteArrayOutputStream and disposing of the Converter promptly to free native resources.

Frequently Asked Questions

Q: What if the Redis server goes down?
A: The code falls back to performing a fresh conversion when TryGetValue returns false, ensuring continuity.

Q: Can I use a different Redis client library?
A: Yes, the RedisCache class is a simple example; you can replace StackExchange.Redis with Lettuce, Jedis, or any other Java Redis client.

Q: How do I set an expiration time for cached items?
A: Use Redis’ StringSet overload that accepts a TimeSpan/Duration to define TTL per entry.

Q: Is the cache thread‑safe in a web application?
A: The underlying ConnectionMultiplexer is designed for concurrent use, making the cache safe for typical servlet containers.

Q: Do I need to serialize objects manually?
A: The example uses Java’s built‑in serialization. For production, consider more efficient formats like Protocol Buffers or JSON.

Conclusion

You’ve now built a java redis cache example that integrates Redis with GroupDocs.Conversion, learned how to configure a redis cache key prefix, and explored best practices for memory and performance tuning. This pattern can be extended to other conversion formats, multi‑tenant architectures, or cloud‑native deployments.

Next Steps

  • Experiment with different eviction policies and TTL values.
  • Profile your application to identify further bottlenecks.
  • Explore Redis Cluster for high‑availability scenarios.

Last Updated: 2025-12-17
Tested With: GroupDocs.Conversion 25.2
Author: GroupDocs