Skip to content
Matthew edited this page Oct 28, 2015 · 9 revisions

Welcome to the NFileCache! It is a fork of adamcarter's .NET File Cache with a handful of extra features.

##Getting Started: Basic usage

var cache = new NFileCache();
cache["foo"] = "bar";

Console.WriteLine("Reading foo from cache: {0}", cache["foo"]);

By default NFileCache internally uses .NET binary serializer and supports all value types, streams and classes marked with Serializable attribute. Optionally you can extend or change this behavior by providing own implementation of serializer class.

##Features

  • Supports multithreaded disk access.
  • Item keys independent from file system naming conventions.
  • Objects scattered across multiple physical directories.
  • Allow custom implementation of the serializer / deserializer.

##API

NFileCache implements System.Runtime.Caching.ObjectCache. Additionally, exposes the following methods and properties:

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

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

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

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

/// <summary>
/// Returns the approximate size of the cache. If cache instance was initialized
/// with param "calculateCacheSize" set to false, then returned size maps only
/// items added and removed from time the cache was created.
/// </summary>
public long CurrentCacheSize { get; private set; }

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

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

/// <summary>
/// Returns the policy attached to a given cache item.
/// </summary>
public CacheItemPolicy GetPolicy(string key, string regionName = null)

/// <summary>
/// Calculates the size, in bytes of the cache.
/// </summary>
public long GetCacheSize(string regionName = null);

/// <summary>
/// Flushes the 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);
Clone this wiki locally