FileCache Documentation


Using the file cache is fairly straightforward.  After adding FileCache and System.Runtime.Caching references to your project, add the appropriate using statement:

using System.Runtime.Caching;

Note that I’ve placed my FileCache code inside the same namespace as the default .NET caching namespace for simplicity.  Below are two examples of how to use FileCache:

Basic Example

//basic example
FileCache simpleCache = new FileCache();
string foo = "bar";
simpleCache["foo"] = foo;
Console.WriteLine("Reading foo from simpleCache: {0}", simpleCache["foo"]);

Serializing Custom Objects

Below is an example that allows the caching of custom objects. First, place the following class in the assembly that contains the objects that need to be serialized:

/// You should be able to copy & paste this code into your local project to enable caching custom objects.
public sealed class ObjectBinder : System.Runtime.Serialization.SerializationBinder
   public override Type BindToType(string assemblyName, string typeName)
      Type typeToDeserialize = null;
      String currentAssembly = Assembly.GetExecutingAssembly().FullName;
      // In this case we are always using the current assembly
      assemblyName = currentAssembly;

      // Get the type using the typeName and assemblyName
      typeToDeserialize = Type.GetType(String.Format("{0}, {1}",
      typeName, assemblyName));

      return typeToDeserialize;

Next, pass in the custom ObjectBinder into the FileCache's constructor:

//example with custom data binder (needed for caching user defined classes)
FileCache binderCache = new FileCache(new ObjectBinder());

Now, use the cache like normal:

GenericDTO dto = new GenericDTO()
      IntProperty = 5,
      StringProperty = "foobar"
binderCache["dto"] = dto;
GenericDTO fromCache = binderCache["dto"] as GenericDTO;
   "Reading DTO from binderCache:\n\tIntProperty:\t{0}\n\tStringProperty:\t{1}", 

Complete API

FileCache implements System.Runtime.Caching.ObjectCache. For the complete base API, see the MSDN article on ObjectCache.  Additionally, FileCache exposes the following methods and properties:

/// <summary>
/// Used to store the default region when accessing the cache via [] 
/// calls
/// </summary>
public string DefaultRegion { get; set; }

/// <summary>
/// Used to set the default policy when setting cache values via [] 
/// calls
/// </summary>
public CacheItemPolicy DefaultPolicy { get; set; }

/// <summary>
/// Used to determine how long the FileCache will wait for a file to 
/// become available.  Default (00:00:00) is indefinite.  Should the 
/// timeout be reached, an exception will be thrown.
/// </summary>
public TimeSpan AccessTimeout { get; set; }

/// <summary>
/// Returns a list of keys for a given region.  
/// </summary>
/// <param name="regionName" /></param>
/// <returns></returns>
public string[] GetKeys(string regionName = null)

/// <summary>
/// Returns the policy attached to a given cache item.  
/// </summary>
/// <param name="key" />The key of the item</param>
/// <param name="regionName" />The region in which the key exists</param>
/// <returns></returns>
public CacheItemPolicy GetPolicy(string key, string regionName = null)

/// <summary>
/// Used to specify the disk size, in bytes, that can be used by the File Cache.  Defaults to long.MaxValue
/// </summary>
public long MaxCacheSize { get; set; }

/// <summary>
/// Returns the approximate size of the file cache
/// </summary>
public long CurrentCacheSize { get; private set; }

/// <summary>
/// Event that will be called when  is reached.
/// </summary>
public event EventHandler MaxCacheSizeReached = delegate { };

/// <summary>
/// Calculates the size, in bytes of the file cache
/// </summary>
/// <param name="regionName" />The region to calculate.  If NULL, will return total size.</param>
public long GetCacheSize(string regionName = null);

/// <summary>
/// Flushes the file cache using DateTime.Now as the minimum date
/// </summary>
public void Flush(string regionName = null);

/// <summary>
/// Flushes the cache based on last access date, filtered by optional region
/// </summary>
public void Flush(DateTime minDate, string regionName = null);

Last edited Feb 24, 2014 at 1:42 AM by adamcarter, version 18


adamcarter Mar 24, 2016 at 1:58 AM 

I'm not sure. I took the SerializationBinder from an online example (source lost). Regarding your first question, you can tell FC where to locate during construction: E.g. var myFc = new FileCache("/path/to/dir");

YawarKhuwaja Oct 16, 2015 at 10:06 AM 
By default it saves the file in the bin folder. Is there anyway we can override this behaviour?

YawarKhuwaja Oct 16, 2015 at 8:19 AM 
To cache custom type we can also use Serializable attribute on that type and cache it as we do primitive types.

public class Customer
public string Name { get; set; }

public int Id { get; set; }

public DateTime DateOfBirth { get; set; }

FileCache cache = new FileCache();

Customer c = new Customer();
c.Name = "SuperName";
c.Id = 1245;
c.DateOfBirth = DateTime.Now;

// saving customer object into cache
cache["Customer"] = c;

// retrieving customer object from cache.
Customer customerFromCache = (Customer)cache["Customer"];

Above code works fine. So what is the difference between this approach and the one used by inheriting "SerializationBinder"?