Caching saves rendering results to a specified location, such as a local disk. When you re-request the rendering results, GroupDocs.Viewer does not render again, but uses the cached results.
As a document can take a long time to render, use caching if you expect to reuse the rendering results.
To enable caching, follow these steps:
Instantiate the desired cache object (for example, FileCache to store document rendering results at the local drive)
Instantiate the ViewerSettings object. Specify the cache object as a parameter of the constructor.
Instantiate the Viewer object. Specify the ViewerSettings object as a parameter of the constructor.
The following code snippet shows how to enable caching and displays the difference between rendering a file and getting the cached results:
importcom.groupdocs.viewer.Viewer;importcom.groupdocs.viewer.ViewerSettings;importcom.groupdocs.viewer.caching.FileCache;importcom.groupdocs.viewer.options.HtmlViewOptions;// ...
StringcachePath="cache";FileCachecache=newFileCache(cachePath);ViewerSettingssettings=newViewerSettings(cache);try(Viewerviewer=newViewer("sample.docx",settings)){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).");}
The following image shows a sample console output: