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:
usingSystem;usingSystem.Diagnostics;usingSystem.IO;usingGroupDocs.Viewer;usingGroupDocs.Viewer.Caching;usingGroupDocs.Viewer.Options;// ...stringoutputDirectory=@"C:\output";stringcachePath=Path.Combine(outputDirectory,"cache");stringpageFilePathFormat=Path.Combine(outputDirectory,"page_{0}.html");// Create a cache.FileCachecache=newFileCache(cachePath);ViewerSettingssettings=newViewerSettings(cache);using(Viewerviewer=newViewer(@"C:\sample.docx",settings)){// Create an HTML file.HtmlViewOptionsoptions=HtmlViewOptions.ForEmbeddedResources(pageFilePathFormat);// Render and display the rendering time.StopwatchstopWatch=Stopwatch.StartNew();viewer.View(options);stopWatch.Stop();Console.WriteLine("Time taken on first call to View method {0} (ms).",stopWatch.ElapsedMilliseconds);// Get cached results and display the time.stopWatch.Restart();viewer.View(options);stopWatch.Stop();Console.WriteLine("Time taken on second call to View method {0} (ms).",stopWatch.ElapsedMilliseconds);}
ImportsSystemImportsSystem.DiagnosticsImportsSystem.IOImportsGroupDocs.ViewerImportsGroupDocs.Viewer.CachingImportsGroupDocs.Viewer.Options' ...
ModuleProgramSubMain(argsAsString())DimoutputDirectoryAsString="C:\output"DimcachePathAsString=Path.Combine(outputDirectory,"cache")DimpageFilePathFormatAsString=Path.Combine(outputDirectory,"page_{0}.html")' Create a cache.
DimcacheAsFileCache=NewFileCache(cachePath)DimsettingsAsViewerSettings=NewViewerSettings(cache)UsingviewerAsViewer=NewViewer("C:\sample.docx",settings)' Create an HTML file.
DimoptionsAsHtmlViewOptions=HtmlViewOptions.ForEmbeddedResources(pageFilePathFormat)' Render and display the rendering time.
DimstopWatchAsStopwatch=Stopwatch.StartNew()viewer.View(options)stopWatch.[Stop]()Console.WriteLine("Time taken on first call to View method {0} (ms).",stopWatch.ElapsedMilliseconds)' Get cached results and display the time.
stopWatch.Restart()viewer.View(options)stopWatch.[Stop]()Console.WriteLine("Time taken on second call to View method {0} (ms).",stopWatch.ElapsedMilliseconds)EndUsingEndSubEndModule
CONVERSIONERROR:ConversionforGlobalStatementnotimplemented,pleasereportthisissuein'string outputDirectory = @"...' at character 160
CONVERSIONERROR:ConversionforGlobalStatementnotimplemented,pleasereportthisissuein'string cachePath = Path.Com...' at character 200
CONVERSIONERROR:ConversionforGlobalStatementnotimplemented,pleasereportthisissuein'string pageFilePathFormat =...' at character 260
CONVERSIONERROR:ConversionforGlobalStatementnotimplemented,pleasereportthisissuein'FileCache cache = new FileC...' at character 357
CONVERSIONERROR:ConversionforGlobalStatementnotimplemented,pleasereportthisissuein'ViewerSettings settings = n...' at character 402
CONVERSIONERROR:ConversionforGlobalStatementnotimplemented,pleasereportthisissuein'using (Viewer viewer = new ...' at character 458
The following image shows a sample console output: