This project is read-only.

How To Configure Caching

Document Toolkit 2.0.8 (June 2011 Update) includes a number of enhancements related to caching of document parts. This tutorial explains and provides details on how to take advantage of the new caching features.

Document Toolkit provides two types of caches; part caches and object caches. The part cache stores the raw byte data of document parts in its backing store. The object cache stores instantiated object such as fonts, image bitmaps, processed page XAML string, etc.

Part Cache

In order to prevent unnecessary requests for document parts that have been requested before, the part cache stores requested parts in its backing store. A part cache is implemented as an IPackageReader and wraps an existing package reader. The part cache reader verifies whether a part exists in its cache and returns it instead of passing the request on to the wrapped package reader.

Currently there are two cache readers available; the MemoryCacheReader and the IsolatedStorageCacheReader. The MemoryCacheReader caches parts in memory for the lifetime of a document, the IsolatedStorageCacheReader save documents parts to the isolated storage. Both readers derive from CachePackageReader, you can easily create your own cache reader by deriving from this class as well.

The following snippet demonstrates how to use the MemoryCacheReader to wrap a DefaultPackageReader.

using FirstFloor.Documents.IO;
using FirstFloor.Documents.IO.Caching;

var reader = new DefaultPackageReader(documentStream);
var cacheReader = new MemoryCacheReader(reader);

this.DataSource.PackageReader = cacheReader;

Note: DataSource is a reference to a DocumentDataSource instance defined in XAML.

The use of a part cache reader makes most sense when the fetching of document parts takes a relatively long time. This is often the case when a part request is issued over a network such as with the WebPackageReader. When a package reader loads its parts from a locally available document, using a part cache will do little to improve performance.

Object Cache

Next to the part cache, Document Toolkit caches fonts, image bitmaps, page XAML and resource dictionary XAML elements in memory. The elements in the object cache has a lifetime that is equal to the lifetime of its owning document. When a document is disposed (by assinging a null value or another package reader to the DocumentDataSource), the object cache is disposed as well. The object cache prevents unnecessary part request and ensures objects are instantiated once and re-used throughout the lifetime of a document.

The object cache is built-in and can be configured by modifying the cache properties of the LoadXpsDocumentSettings class. The following XAML snippet demonstrates how to configure the object cache so that a maximum of 16 images, 16 pages and 16 resource dictionaries are cached in memory.

<doc:DocumentDataSource x:Name="DataSource" LoadError="DataSource_LoadError" >
    <doc:LoadXpsDocumentSettings ImageCacheSize="16"
                                 ResourceDictionaryCacheSize="16" />

Note: there's no option to configure the max size of the font cache, fonts are always cached.

Last edited Jul 4, 2011 at 8:26 PM by kozw, version 4


No comments yet.