Skip to content

Commit

Permalink
Updated advanced usage caching example
Browse files Browse the repository at this point in the history
  • Loading branch information
muqarrab-aspose committed Mar 12, 2024
1 parent 5015dc6 commit efbf270
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 41 deletions.
3 changes: 2 additions & 1 deletion content/english/net/advanced-usage-caching/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ url: /net/advanced-usage-caching/
---

## Advanced Usage (Caching) Tutorials
### [Enable Caching for Faster Document Processing](./enable-caching/)
### [Enable Caching for Faster Document Processing](./enable-caching/)
Enhance document processing speed in .NET apps with GroupDocs.Viewer by leveraging caching. Optimize performance effortlessly.
109 changes: 69 additions & 40 deletions content/english/net/advanced-usage-caching/enable-caching/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,85 @@
title: Enable Caching for Faster Document Processing
linktitle: Enable Caching for Faster Document Processing
second_title: GroupDocs.Viewer .NET API
description:
description: Enhance document processing speed in .NET apps with GroupDocs.Viewer by leveraging caching. Optimize performance effortlessly.
type: docs
weight: 10
url: /net/advanced-usage-caching/enable-caching/
---
## Introduction
In the realm of .NET document processing, optimizing performance is paramount. Imagine a scenario where you need to render multiple document pages swiftly. This is where caching comes into play. In this tutorial, we'll delve into leveraging caching to enhance the processing speed of documents using GroupDocs.Viewer for .NET.
## Prerequisites
Before diving into the implementation, ensure you have the following prerequisites in place:
1. GroupDocs.Viewer for .NET SDK: Download and install the SDK from the [GroupDocs.Viewer website](https://releases.groupdocs.com/viewer/net/).
2. Development Environment: Set up your preferred .NET development environment, such as Visual Studio.
3. Sample Document: Have a sample document ready for testing purposes.

## Complete Source Code
## Importing Namespaces
To begin, import the necessary namespaces:
```csharp
using System;
using System.Diagnostics;
using System.IO;
using GroupDocs.Viewer.Caching;
using GroupDocs.Viewer.Options;
```

namespace GroupDocs.Viewer.Examples.CSharp.AdvancedUsage.Caching
{
/// <summary>
/// This example demonstrates how to enable cache when render document.
/// </summary>
class UseCacheWhenProcessingDocuments
{
public static void Run()
{
string outputDirectory = "Your Document Directory";
string cachePath = Path.Combine(outputDirectory, "cache");
string pageFilePathFormat = Path.Combine(outputDirectory, "page_{0}.html");

FileCache cache = new FileCache(cachePath);
ViewerSettings settings = new ViewerSettings(cache);

using (Viewer viewer = new Viewer(TestFiles.SAMPLE_DOCX, settings))
{
HtmlViewOptions options = HtmlViewOptions.ForEmbeddedResources(pageFilePathFormat);

Stopwatch stopWatch = Stopwatch.StartNew();
viewer.View(options);
stopWatch.Stop();

Console.WriteLine("Time taken on first call to View method {0} (ms).", stopWatch.ElapsedMilliseconds);

stopWatch.Restart();
viewer.View(options);
stopWatch.Stop();

Console.WriteLine("Time taken on second call to View method {0} (ms).", stopWatch.ElapsedMilliseconds);
}

Console.WriteLine($"\nSource document rendered successfully.\nCheck output in {outputDirectory}.");
}
}
}

## Step 1: Define Output Directory and Cache Path
```csharp
string outputDirectory = "Your Document Directory";
string cachePath = Path.Combine(outputDirectory, "cache");
```
Here, we define the output directory where the rendered pages will be saved, along with the cache path.
## Step 2: Initialize File Cache
```csharp
FileCache cache = new FileCache(cachePath);
```
Initialize a file cache using the specified cache path.
## Step 3: Configure Viewer Settings
```csharp
ViewerSettings settings = new ViewerSettings(cache);
```
Configure viewer settings, passing the initialized cache.
## Step 4: Initialize Viewer Instance
```csharp
using (Viewer viewer = new Viewer(TestFiles.SAMPLE_DOCX, settings))
```
Initialize the viewer instance with the sample document and configured settings.
## Step 5: Define HTML View Options
```csharp
HtmlViewOptions options = HtmlViewOptions.ForEmbeddedResources(pageFilePathFormat);
```
Define HTML view options for embedded resources, specifying the page file path format.
## Step 6: Render Document and Measure Performance
```csharp
Stopwatch stopWatch = Stopwatch.StartNew();
viewer.View(options);
stopWatch.Stop();
```
Render the document using the specified options and measure the time taken.
## Step 7: Reuse Cached Data for Faster Rendering
```csharp
stopWatch.Restart();
viewer.View(options);
stopWatch.Stop();
```
Re-render the document using cached data to observe the performance improvement.
## Step 8: Output Rendered Document
```csharp
Console.WriteLine($"\nSource document rendered successfully.\nCheck output in {outputDirectory}.");
```
Notify the user about the successful rendering and the location of the output directory.

## Conclusion
Caching plays a vital role in optimizing document processing performance in .NET applications. By following the steps outlined in this tutorial, you can efficiently enable caching in GroupDocs.Viewer for .NET, thereby accelerating document rendering.
## FAQ's
### Why is caching important for document processing?
Caching reduces the need to regenerate data, thus improving processing speed.
### Can caching be customized in GroupDocs.Viewer for .NET?
Yes, GroupDocs.Viewer offers flexibility in configuring caching settings according to specific requirements.
### Is GroupDocs.Viewer suitable for handling large documents?
Absolutely, GroupDocs.Viewer is designed to efficiently handle documents of varying sizes, ensuring optimal performance.
### Does GroupDocs.Viewer support multiple document formats?
Yes, GroupDocs.Viewer supports a wide range of document formats, including DOCX, PDF, PPTX, and more.
### How can I obtain temporary licenses for GroupDocs.Viewer?
You can acquire temporary licenses for GroupDocs.Viewer from the [website](https://purchase.groupdocs.com/temporary-license/).

0 comments on commit efbf270

Please sign in to comment.