The LEADTOOLS ObjectCache object responsible for managing cache items and expiration policies.
public static ObjectCache Cache { get; set; }
The cache object responsible for managing cache items and expiration policies. Default value is null.
Note: This property will be deprecated in the next version of LEADTOOLS, instead, initialize a cache object and use it directly with the various DocumentFactory methods that require it.
A large portion of the Documents class library requires the use of a cache system. This includes almost all the write support. Documents can contain large number of pages and huge amount of data. Storing all this data in the physical memory is not feasible in most situations. Therefore, the Document library was designed to use an external caching system to store the modified parts of the document.
Using a large will also increase the performance when getting page data such as an image or text. The result will be parsed once from the physical file on disk, processed and then stored in the cache. Subsequent calls to the same data will simply retrieve it from the cache without any extra processing.
The LEADTOOLS Document Web Service requires the use of a cache. Web methods are session-less by nature and with cache support, the service can use SaveToCache and LoadFromCache to save/load the document state in-between calls without the need for session state. DocumentFactory can be used to remove a document from the cache when it is no longer needed.
In almost all cases, the cache object is initialized and set into Cache at the start of the application and then never touched. The DocumentFactory and LEADDocument classes will use this global value for all the caching needs.
The default LEADTOOLS implementation of ObjectCache is FileCache object and setting it up is easy. The only input required is the path to a directory on disk or network share where the cache items are stored.
Refer to Document Toolkit and Caching for more information on how the cache is used with the LEADTOOLS Document library and how to set up optional custom cache provider.
Generally, the cache is used in two ways depending on the type of the application:
The application uses the cache to speed up getting data from existing documents or to create new documents. The data is not shared with other applications, nor is required to persist between sessions. An example is the LEADTOOLS Document Viewer Demo. It obtains a temporary folder and sets it as the cache directory to use for CacheDirectory objects that do not expire, and the document parts are cached and used. The value of AutoSaveToCache is set to false: each document will delete its own cache entry when it is disposed and no longer needed.
The application requires the document state to persist between sessions. An example is the LEADTOOLS Document Web Service. It contains a global cache directory stored in the web.config
file. The cache is constructed with each session (web method) by setting the same value in CacheDirectory. When a document is loaded, it is saved in the cache using SaveToCache before the web method returns, and then re-loaded when a new session (web method) starts using LoadFromCache. These types of applications usually store a policy setting in the web.config
as well. This will control how long to store items in the cache before they expire. The FileCache.CheckPolicies method can be used by an external process in the application to periodically run and purge expired items.
using Leadtools;
using Leadtools.Caching;
using Leadtools.Document;
public void DocumentExample()
{
var cache = GetCache();
var policy = new CacheItemPolicy();
policy.AbsoluteExpiration = DateTime.Now + new TimeSpan(0, 0, 1);
policy.SlidingExpiration = new TimeSpan(0, 0, 1);
var options = new LoadDocumentOptions();
options.CachePolicy = policy;
options.Cache = cache;
if(options.Cache == null)
{
options.Cache = DocumentFactory.Cache;
}
string documentId = null;
using (var document = DocumentFactory.LoadFromFile(Path.Combine(LEAD_VARS.ImagesDir, "Leadtools.pdf"), options))
{
document.GetDocumentFileName();
document.IsReadOnly = false;
document.AutoDeleteFromCache = false;
// DocumentImages reference
document.Images.DefaultBitsPerPixel = 24;
Console.WriteLine(document.Images.IsResolutionsSupported);
Console.WriteLine(document.Images.IsSvgSupported);
// Check if the document has a stream in memory
if (document.HasStream)
{
// Get the document stream
document.GetDocumentStream();
}
// Indicate whether the document is using the cache or not
Console.WriteLine(document.HasCache);
//Indicate if the document was downloaded
Console.WriteLine(document.IsDownloaded);
// Gets a value that determines whether the document structure is supported
Console.WriteLine(document.IsStructureSupported);
// Output metadata values (DocumentMetadata reference)
Console.WriteLine(document.Metadata.Values.Count);
// Get the Mime type of the document
Console.WriteLine(document.MimeType);
// Parse document structure data (DocumentStructure reference)
foreach(DocumentBookmark bookmark in document.Structure.Bookmarks)
{
bookmark.Title = null;
bookmark.FontStyles = DocumentFontStyles.Normal;
document.Structure.Bookmarks.Add(bookmark);
Console.WriteLine(bookmark.Children);
Console.WriteLine(bookmark.Target);
Console.WriteLine(document.Structure.Bookmarks.Count);
Console.WriteLine(document.Structure.IsParsed);
Console.WriteLine(document.Structure.ParseBookmarks);
}
document.Structure.Parse();
// Get the document URI
Console.WriteLine(document.Uri);
// Get each DocumentPage object (DocumentPage & DocumentPages reference)
foreach (DocumentPage page in document.Pages)
{
// Get the page as a raster image at the specified resolution
page.GetImage(0);
// Get the page as an Svg with specified options
page.GetSvg(null);
// Flip this page horizontally
page.Reverse();
// Use this method to add an array of links for this page
page.SetLinks(null);
page.IsLinksModified = false;
page.Resolution = 0;
page.ViewPerspective = RasterViewPerspective.TopLeft;
page.SetLinks(page.GetLinks());
Console.WriteLine($"Page Number: {page.PageNumber}, Original PageNumber: {page.OriginalPageNumber}, Size of the page: {page.Size}");
}
PrintOutDocumentInfo(document);
documentId = document.DocumentId;
document.SaveToCache();
}
System.Threading.Thread.Sleep(2000);
var loadFromCacheOptions = new LoadFromCacheOptions();
loadFromCacheOptions.Cache = cache;
loadFromCacheOptions.DocumentId = documentId;
using (var document = DocumentFactory.LoadFromCache(loadFromCacheOptions))
{
if (null == document)
{
Console.WriteLine("Cached document was expired and deleted!");
}
}
}
static class LEAD_VARS
{
public const string ImagesDir = @"C:\LEADTOOLS23\Resources\Images";
}