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:
importtimeimportgroupdocs.viewerasgvimportgroupdocs.viewer.optionsasgvo# Define a path to cachecache_path="cache"cache=gv.FileCache(cache_path)settings=gv.ViewerSettings(cache)# Define a Viewer for a "sample.docx" filewithgv.Viewer("sample.docx",settings)asviewer:options=gvo.HtmlViewOptions.for_embedded_resources()# Define the execution time of the first callstart_time=time.time()viewer.view(options)elapsed_time=time.time()-start_timeprint(f"Time taken on first call to View method {elapsed_time*1000:.0f} (ms).")# Define the execution time of the second callstart_time=time.time()viewer.view(options)elapsed_time=time.time()-start_timeprint(f"Time taken on second call to View method {elapsed_time*1000:.0f} (ms).")
The following image shows a sample console output:
For details, please refer to the following pages:
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.