By default, GroupDocs.Viewer implements caching to local. But you can cache rendering result in your own way. To do this, use the Cache interface implementation.
Implementing Memory Cache
In this section we build the custom MemoryCache class that is based on HashMap and demonstrate how to use it in GroupDocs.Viewer.
The complete listing of the MemoryCache.java file is below:
importcom.groupdocs.viewer.Viewer;importcom.groupdocs.viewer.ViewerSettings;importcom.groupdocs.viewer.options.HtmlViewOptions;// ...
MemoryCachememoryCache=newMemoryCache("cache-key-prefix-");ViewerSettingsviewerSettings=newViewerSettings(memoryCache);try(Viewerviewer=newViewer("sample.docx",viewerSettings)){HtmlViewOptionsoptions=HtmlViewOptions.forEmbeddedResources();longcurrentTimeMillis=System.currentTimeMillis();viewer.view(options);currentTimeMillis=System.currentTimeMillis()-currentTimeMillis;System.out.println("Time taken on first call to View method "+currentTimeMillis+" (ms).");currentTimeMillis=System.currentTimeMillis();viewer.view(options);currentTimeMillis=System.currentTimeMillis()-currentTimeMillis;System.out.println("Time taken on second call to View method "+currentTimeMillis+" (ms).");}
After running the code above the similar output will be printed in a console:
Using Custom Model Classes for Caching
Default Viewer models implements the serializable interface. But you may want to use other library for serialization. Lots of them want you to annotate classes and fields of classes that should be serialized.
You can create custom models with appropriate fields and configure GroupDocs.Viewer to use these models.
To do this, follow these steps:
Create custom models, implementing Viewer model’s interfaces, like ViewInfo, FileInfo, and so on (see full list of interfaces in the CacheableFactory class).
Create custom model factory (CustomFactory) extending class CacheableFactory. Each method in the class must be overridden to return the instance of your cache model.
Configure GroupDocs.Viewer to use the CustomFactory using the CacheableFactory.setInstance(new CustomFactory()) method before creating the Viewer object.
Configure the GroupDocs.Viewer cache as usual. GroupDocs.Viewer put your model’s objects into the set method of the cache implementation so you can serialize it in any way.
publicclassJacksonCacheimplementsCache{//...
@Overridepublicvoidset(Stringkey,Objectvalue){finalbyte[]bytes;if(valueinstanceofInputStream){bytes=IOUtils.toByteArray((InputStream)value);}else{// Serialize in any way
bytes=jacksonMapper.writeValueAsBytes(value);}// Here bytes could be written to file or sent somewhere
mData.put(key,bytes);}//...
@Overridepublic<T>Tget(Stringkey,Class<T>clazz){finalbyte[]bytes=mData.get(key);if(InputStream.class.equals(clazz)){return(T)newByteArrayInputStream(bytes);}else{// Deserialize in any way
returnjacksonMapper.readValue(bytes,clazz);}}//...
}
importcom.groupdocs.viewer.Viewer;importcom.groupdocs.viewer.ViewerSettings;importcom.groupdocs.viewer.caching.Cache;importcom.groupdocs.viewer.caching.extra.CacheableFactory;// ...
CacheableFactory.setInstance(newCustomFactory());Cachecache=newJacksonCache();ViewerSettingssettings=newViewerSettings(cache);try(Viewerviewer=newViewer("document.doc",settings)){// Do work
}
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.