←Select platform

CreateFromConfigurations Method

Summary

Creates a new cache object from the specified configuration stream and optional additional values.

Syntax
C#
VB
C++
public static ObjectCache CreateFromConfigurations( 
   Stream stream, 
   IDictionary<string, string> additionalValues 
) 
Public Shared Function CreateFromConfigurations( 
   ByVal stream As Stream, 
   ByVal additionalValues As IDictionary(Of String, String) 
) As ObjectCache 
public:  
   static ObjectCache^ CreateFromConfigurations( 
      Stream^ stream, 
      IDictionary<String^, String^>^ additionalValues 
   ) 

Parameters

stream

Stream containing the configuration. This is usually a stream to an XML file or data containing configuration previously saved with SaveConfiguration. This value must not be null.

additionalValues

Dictionary of key/item values containing optional values to use. additionalValues override any values with the same key found in stream.

Return Value

The ObjectCache derived class created by this method if successful. Otherwise, null.

Remarks

ObjectCache and its derived classes (such as FileCache), support loading and saving XML configuration that can be used to re-create the object. This is very useful to minimize the code required in an application to switch between different configurations such as development versus deployment.

The following discussion will refer to FileCache. Start by creating a new FileCache as usual and set it up with the required properties such as FileCache.CacheDirectory and FileCache.DataSerializationMode. Once done, use SaveConfiguration to create an XML file for the configuration. This file now contains all the values necessary to re-create the FileCache object and initialize it to the same values used at the time of SaveConfiguration.

Loading the configuration can be performed in two ways:

  1. Create a new FileCache and use the LoadConfiguration instance method.

  2. All configuration files contain the name of the class. Therefore, the static CreateFromConfigurations method can be used to re-create the FileCache directory without having to create a new instance. If the name in the configuration file does not match the name of the derived class, null is returned.

All the configuration loading methods contain an additional key/value dictionary that can be used to add or override any values if required.

This table describes the methods that can be used to save configurations:

Method Description
SaveConfiguration Instance method that saves the configurations of an ObjectCache derived class to an XML stored in a stream.
GetConfigurationString Instance method that gets the configurations of an ObjectCache derived class as a string.

This table describes the methods that can be used to load configurations:

Method Description
CreateFromConfigurations Static method that creates an ObjectCache derived class from the configuration XML stored in a stream.
CreateFromConfigurationsString Static method that creates an ObjectCache derived class from the configuration XML stored in a string.
LoadConfiguration Instance method that updates an ObjectCache derived class from the configuration XML stored in a stream.
SetConfigurationString Instance method that updates an ObjectCache derived class from the configuration XML stored in a string.

All the load methods will return either true or non-null on success. Otherwise, false or null will be returned. This happens if the configuration does not contain the name of the class used.

Naturally, the result configuration file is standard XML that can also be loaded and modified by any text editor.

Example

This example will create a new FileCache object and set it up. It will then save the configuration to an XML file and use it to re-create the object.

C#
VB
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:\LEADTOOLS21\Resources\Images"; 
} 
Imports Leadtools.Caching 
Imports LeadtoolsCachingExamples.LeadtoolsExamples.Common 
 
Public Shared Sub FileCacheConfigurationExample() 
   ' XML file to save the cache configuration 
   Dim cacheConfigFile As String = Path.Combine(ImagesPath.Path, "filecache.xml") 
 
   Dim cacheDir As String = Path.Combine(ImagesPath.Path, "cache") 
   ' This Is optional And can be null if Not needed 
   Dim cacheVirtualDir As String = "http://localhost/cache" 
 
   ' Create the LEADTOOLS FileCache object And set it up 
 
   Dim cache1 As New FileCache() 
   cache1.CacheDirectory = cacheDir 
   If Not IsNothing(cacheVirtualDir) Then 
      cache1.CacheVirtualDirectory = New Uri(cacheVirtualDir) 
   End If 
   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 
 
   Dim defaultPolicy As New CacheItemPolicy() 
   Dim absoluteExpiration As DateTime = DateTime.Now 
   absoluteExpiration = absoluteExpiration.AddDays(1) 
   defaultPolicy.AbsoluteExpiration = absoluteExpiration 
   cache1.DefaultPolicy = defaultPolicy 
 
   ' Cache Is ready, save its configuration to the file 
   Using stream As Stream = File.Create(cacheConfigFile) 
      cache1.SaveConfiguration(stream, True) 
   End Using 
 
   ' Add an item 
   Dim cacheItem As New CacheItem(Of 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 
   Dim result As CacheItem(Of String) = cache1.GetCacheItem(Of 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) 
End Sub 
 
Private Shared Sub InitFromConfigurationAndCheckCache(cacheConfigFile As String, originalItem As CacheItem(Of String)) 
   ' 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 
   Dim cache2 As ObjectCache = Nothing 
 
   Using stream As Stream = File.OpenRead(cacheConfigFile) 
      cache2 = ObjectCache.CreateFromConfigurations(stream, Nothing) 
   End Using 
 
   Console.WriteLine("cache2 loaded from {0}", cacheConfigFile) 
 
   ' Read the item we previously saved and check its value 
   Dim result As CacheItem(Of String) = cache2.GetCacheItem(Of 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) 
End Sub 
Requirements
Target Platforms
Help Version 21.0.2021.6.30
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2021 LEAD Technologies, Inc. All Rights Reserved.

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