GroupDocs.Conversion provides built-in caching to the local drive for improved performance. However, for greater flexibility, the library allows developers to implement custom caching mechanisms by extending the ICache interface. This approach enables storing conversion results in alternate storage systems, such as memory caches, distributed caches, or databases.
This guide demonstrates how to implement a custom caching mechanism using Redis, a popular distributed in-memory cache, with GroupDocs.Conversion.
Steps to Implement a Redis-Based Custom Cache
Implement the ICache interface to define the caching logic using Redis as the backend.
Create an instance of the Redis-based implementation.
Use a delegate to instantiate the ConverterSettings class. Configure the cache for ConverterSettings using the setCache method and pass the custom cache instance.
Create a Converter instance, providing the path to the source document and the custom ConverterSettings delegate as parameters.
Call the convert method of the Converter class to process the document.
The following code demonstrates how to implement a custom Redis-based caching mechanism for GroupDocs.Conversion:
importcom.groupdocs.conversion.Converter;importcom.groupdocs.conversion.ConverterSettings;importcom.groupdocs.conversion.options.convert.PdfConvertOptions;importjava.time.Duration;importjava.time.Instant;publicclassCustomCacheImplementation{publicstaticvoidconvert(){// Define cache key prefix
StringcacheKeyPrefix="myPrefix:";// Create an instance of the Redis-based custom cache
RedisCachecache=newRedisCache(cacheKeyPrefix);// Configure the ConverterSettings with custom cache
ConverterSettingssettingsFactory=newConverterSettings();settingsFactory.setCache(cache);// Initialize the Converter with the source document and settings
try(Converterconverter=newConverter("professional-services.pdf",()->settingsFactory)){// Define conversion options for PDF
PdfConvertOptionsoptions=newPdfConvertOptions();// Measure time taken for the first conversion
Instantstart=Instant.now();converter.convert("converted.pdf",options);Instantend=Instant.now();System.out.println(String.format("Time taken on first Convert call: %d ms",Duration.between(start,end).toMillis()));// Measure time taken for the second conversion (cached result)
start=Instant.now();converter.convert("converted-1.pdf",options);end=Instant.now();System.out.println(String.format("Time taken on second Convert call: %d ms",Duration.between(start,end).toMillis()));}}publicstaticvoidmain(String[]args){convert();}}
importcom.groupdocs.conversion.caching.ICache;importredis.clients.jedis.Jedis;importredis.clients.jedis.JedisPool;importredis.clients.jedis.JedisPoolConfig;importredis.clients.jedis.util.Pool;importjava.io.ByteArrayInputStream;importjava.io.ByteArrayOutputStream;importjava.io.InputStream;importjava.util.List;importjava.util.Set;importjava.util.stream.Collectors;publicclassRedisCacheimplementsICache,AutoCloseable{privatefinalStringcacheKeyPrefix;privatefinalPool<Jedis>jedisPool;publicRedisCache(StringcacheKeyPrefix){this.cacheKeyPrefix=cacheKeyPrefix;Stringhost="192.168.222.4";this.jedisPool=newJedisPool(newJedisPoolConfig(),host);}publicvoidset(Stringkey,Objectdata){if(data==null){return;}StringprefixedKey=getPrefixedKey(key);try(ByteArrayOutputStreamoutputStream=newByteArrayOutputStream()){byte[]buf=newbyte[8192];intlength;while((length=((InputStream)data).read(buf))!=-1){outputStream.write(buf,0,length);}try(Jedisjedis=jedisPool.getResource()){jedis.set(prefixedKey.getBytes(),outputStream.toByteArray());}}catch(Exceptione){thrownewRuntimeException("Error setting data to Redis",e);}}publicObjecttryGetValue(Stringkey){StringprefixedKey=getPrefixedKey(key);try(Jedisjedis=jedisPool.getResource()){byte[]data=jedis.get(prefixedKey.getBytes());if(data!=null){returnnewByteArrayInputStream(data);}}catch(Exceptione){thrownewRuntimeException("Error getting data from Redis",e);}returnnull;}publicList<String>getKeys(Stringfilter){try(Jedisjedis=jedisPool.getResource()){Set<String>keys=jedis.keys("*"+filter+"*");returnkeys.stream().map(key->key.replace(cacheKeyPrefix,"")).filter(key->key.toLowerCase().startsWith(filter.toLowerCase())).collect(Collectors.toList());}}privateStringgetPrefixedKey(Stringkey){returncacheKeyPrefix+key;}@Overridepublicvoidclose(){jedisPool.close();}}
This approach provides a robust solution for integrating a custom caching layer, optimizing document conversion workflows in both performance and scalability.
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.