Caching Results
In some cases document rendering may be a time-consuming operation (dependent on source document content, structure and complexity). For such situations caching can be a solution - rendered document representation is stored into cache (for example at the local drive) and in a case of repetitive rendering of the document, GroupDocs.Viewer uses cached representation. Caching helps to avoid the processing of the same document again and again.
To enable caching you have to:
- Instantiate desired cache object (for example FileCache will store document rendering results at the local drive);
- Instantiate ViewerSettings object with cache object;
- Pass ViewerSettings object to constructor of a Viewer class.
- Call view() method of Viewer class.
Here is a code that demonstrates how to enable caching for GroupDocs.Viewer for Java.
String cachePath = "cache";
FileCache cache = new FileCache(cachePath);
ViewerSettings settings = new ViewerSettings(cache);
try (Viewer viewer = new Viewer("sample.docx", settings)) {
HtmlViewOptions options = HtmlViewOptions.forEmbeddedResources();
long currentTimeMillis = 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 to the console:
Time taken on first call to View method 5233 (ms).
Time taken on second call to View method 5 (ms).