←Select platform

FileCache Class

Summary

Represents LEADTOOLS support for a file cache.

Syntax
C#
C++/CLI
Java
Python
[DefaultMemberAttribute("Item")] 
public class FileCache : ObjectCache 
public [DefaultMemberAttribute(L"Item")] 
   ref class FileCache : ObjectCache 
public class FileCache extends ObjectCache 
class FileCache(ObjectCache): 
Remarks

FileCache implements ObjectCache and is the LEADTOOLS implementation of a file cache. It is used by default by the LEADTOOLS Document classes when caching is required to store the document data.

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.

To use a FileCache, set the CacheDirectory property to a valid directory in your system or in a network share. then use the various methods and properties to add and retrieve cache items with optional expiration policies. The cache will store the items and their policies into disk using serialization and retrieve them using de-serialization.

Since the cache uses the global file system, it can be re-used after the application is terminated. FileCache does not store any state data and all the items are stored in disk. For instance, the LEADTOOLS Document Web Service creates an instance of FileCache with every method call. All instances point to the same CacheDirectory and can therefore use the same items added in previous calls. The cache is thread and process safe.

FileCache provide support for regions which is used to group cache items belonging to the same logical object. These regions are created as sub-directories in the cache directory on disk. LEADTOOLS Document classes use the unique document identifier as the region name and hence can delete all the items belonging to a single document by deleting the region.

Items are purged from the cache if they have an absolute or sliding expiration policy. This is done when the item is accessed (it will be deleted if expired) or by the user by using CheckPolicy or CheckPolicies.

Since cache items that are not used will stay on disk, it is preferable to create a process for purging expired policies manually. This can be done by calling CheckPolicies which will check the whole cache and delete any items that are expired.

GetItemExternalResource returns the physical path of any item currently stored in the cache. This gives the user the ability to read the data of the item directly or pass the file to another part of the system that may require a physical file on disk.

GetItemVirtualDirectoryUrl returns a URI to any item currently stored in the cache. CacheVirtualDirectory must be setup to use this method. Obtaining a URI to a cache item can be useful in a Web application if the client code is JavaScript for example.

This cache implementation also supports data items with values set to null. Thus it is always preferable to use Contains or GetCacheItem<T> instead of relying to Get and checking for null.

The CacheItemExpiring and CacheItemExpired events occur when an item is expired and is about to be removed from the cache.

Use GetStatistics to get the number of regions, items and expired items currently in the cache.

Example
C#
Java
using Leadtools.Caching; 
 
 
public void FileCacheConfigurationExample() 
{ 
   // XML file to save the cache configuration 
   string 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 needed 
   string cacheVirtualDir = "http://localhost/cache"; 
 
   // Create the LEADTOOLS FileCache object and set it up 
 
   var 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 day 
 
   var 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 file 
   using (var stream = File.Create(cacheConfigFile)) 
      cache1.SaveConfiguration(stream, true); 
 
   // Add an item 
   CacheItem<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 it 
   CacheItem<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 back 
   InitFromConfigurationAndCheckCache(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 type 
   ObjectCache 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 value 
   CacheItem<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:\LEADTOOLS23\Resources\Images"; 
} 
 
import static org.junit.Assert.assertTrue; 
 
import java.io.File; 
import java.io.IOException; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.util.Calendar; 
 
import org.junit.*; 
import org.junit.runner.JUnitCore; 
import org.junit.runner.Result; 
import org.junit.runner.notification.Failure; 
 
import leadtools.*; 
import leadtools.caching.*; 
 
 
public void fileCacheConfigurationExample() throws MalformedURLException { 
   final String LEAD_VARS_IMAGES_DIR = "C:\\LEADTOOLS23\\Resources\\Images"; 
 
   // XML file to save the cache configuration 
   String cacheConfigFile = combine(LEAD_VARS_IMAGES_DIR, "filecache.xml"); 
   String cacheDir = combine(LEAD_VARS_IMAGES_DIR, "cache"); 
 
   // This is optional and can be null if not needed 
   String cacheVirtualDir = "http://localhost/cache"; 
 
   // Create the LEADTOOLS FileCache object and set it up 
   FileCache cache1 = new FileCache(); 
   cache1.setCacheDirectory(cacheDir); 
   if (cacheVirtualDir != null) 
      cache1.setCacheVirtualDirectory(new URL(cacheVirtualDir)); 
   cache1.setPolicySerializationMode(CacheSerializationMode.JSON); 
   cache1.setDataSerializationMode(CacheSerializationMode.JSON); 
   cache1.setDefaultRegion("defaultRegion"); 
   cache1.setAccessTimeout(5000); 
 
   System.out.println("cache1 set up and ready"); 
 
   // Set the policy, expires in 1 day 
   CacheItemPolicy defaultPolicy = new CacheItemPolicy(); 
   Calendar calendar = Calendar.getInstance(); 
   calendar.add(Calendar.DATE, 1); 
 
   defaultPolicy.setAbsoluteExpiration(calendar.getTime()); 
   cache1.setDefaultPolicy(defaultPolicy); 
 
   // Cache is ready, save its configuration to the file 
   ILeadStream stream = LeadStreamFactory.create(cacheConfigFile); 
 
   cache1.saveConfiguration(stream, true); 
 
   // Add an item 
   CacheItem<String> cacheItem = new CacheItem<String>("key1", "value1", "region1"); 
   System.out.printf("Adding %s.%s with value: %s to cache1%n", cacheItem.getRegionName(), cacheItem.getKey(), 
         cacheItem.getValue()); 
   cache1.add(cacheItem, String.class, new CacheItemPolicy()); 
 
   // Now, call a separate function with just the configuration file to load and 
   // setup the cache and read the item back 
   initFromConfigurationAndCheckCache(cacheConfigFile, cacheItem); 
   stream.dispose(); 
} 
 
private 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 type 
   ObjectCache cache2 = null; 
   ILeadStream stream = LeadStreamFactory.create(cacheConfigFile); 
   cache2 = ObjectCache.createFromConfigurations(stream, null); 
   stream.dispose(); 
 
   System.out.println("cache2 loaded from " + cacheConfigFile); 
 
   // Read the item we previously saved and check its value 
   CacheItem<String> result = cache2.getCacheItem(originalItem.getKey(), String.class, originalItem.getRegionName()); 
   System.out.printf("Reading %s.%s with value: %s to cache1%n", result.getRegionName(), result.getKey(), 
         result.getValue()); 
   assertTrue("Saved and loaded values are not the same", originalItem.getValue().equals(result.getValue())); 
} 
Requirements

Target Platforms

Help Version 23.0.2024.2.29
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2024 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.Caching Assembly
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.