Abstract class to provide cache support.
[DefaultMemberAttribute("Item")]public abstract class ObjectCache
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.
LEADTOOLS provides the FileCache object that implements ObjectCache.
using Leadtools.Caching;public void FileCacheConfigurationExample(){// XML file to save the cache configurationstring cacheConfigFile = Path.Combine(LEAD_VARS.ImagesDir, "filecache.xml");string cacheDir = Path.Combine(LEAD_VARS.ImagesDir, "cache");// This is optional and can be null if not neededstring cacheVirtualDir = "http://localhost/cache";// Create the LEADTOOLS FileCache object and set it upvar cache1 = new FileCache();cache1.CacheDirectory = cacheDir;if (cacheVirtualDir != null)cache1.CacheVirtualDirectory = new Uri(cacheVirtualDir);cache1.PolicySerializationMode = CacheSerializationMode.Json;cache1.DataSerializationMode = CacheSerializationMode.Json;cache1.DefaultRegion = "defaultRegion";cache1.AccessTimeout = TimeSpan.FromSeconds(5);Console.WriteLine("cache1 ready");// Set the policy, expires in 1 dayvar defaultPolicy = new CacheItemPolicy();var absoluteExpiration = DateTime.Now;absoluteExpiration = absoluteExpiration.AddDays(1);defaultPolicy.AbsoluteExpiration = absoluteExpiration;cache1.DefaultPolicy = defaultPolicy;// Cache is ready, save its configuration to the fileusing (var stream = File.Create(cacheConfigFile))cache1.SaveConfiguration(stream, true);// Add an itemCacheItem<string> cacheItem = new CacheItem<string>("key1", "value1", "region1");Console.WriteLine("Adding {0}.{1} with value:{2} to cache1", cacheItem.RegionName, cacheItem.Key, cacheItem.Value);cache1.Add(cacheItem, new CacheItemPolicy());// Check itCacheItem<string> result = cache1.GetCacheItem<string>("key1", "region1");Assert.AreEqual(cacheItem.Value, result.Value);// Now, call a separate function with just the configuration file to load and setup the cache and read the item backInitFromConfigurationAndCheckCache(cacheConfigFile, cacheItem);}private static void InitFromConfigurationAndCheckCache(string cacheConfigFile, CacheItem<string> originalItem){// Create the cache from the configuration file// Note that we can use the base ObjectCache object here, we do not need to know the exact typeObjectCache cache2 = null;using (var stream = File.OpenRead(cacheConfigFile))cache2 = ObjectCache.CreateFromConfigurations(stream, null);Console.WriteLine("cache2 loaded from {0}", cacheConfigFile);// Read the item we previously saved and check its valueCacheItem<string> result = cache2.GetCacheItem<string>(originalItem.Key, originalItem.RegionName);Console.WriteLine("Reading {0}.{1} with value:{2} to cache1", result.RegionName, result.Key, result.Value);Assert.AreEqual(originalItem.Value, result.Value);}static class LEAD_VARS{public const string ImagesDir = @"C:\LEADTOOLS22\Resources\Images";}