Skip to content
Mateusz Bogusz edited this page Sep 10, 2018 · 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, anonymous classes and classes marked with Serializable attribute. Optionally you can extend or change this behavior by providing own implementation of serializer class.

##Features

  • Supports multithreaded and multi-instance 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; }

/// <summary>
/// Gets a reference to the default <see cref="T:System.Runtime.Caching.NFileCache" /> instance.
/// </summary>
/// <returns>The default instance of the cache.</returns>
public static NFileCache Default { get; }

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

/// <summary>
/// Event that will be called when cache size was shrinked.
/// </summary>
public event EventHandler<FileCacheEventArgs> CacheResized;

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

/// <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>
/// Flushes the cache using DateTime.Now as the minimum date.
/// </summary>
public void Flush(string regionName = null);

/// <summary>
/// Flushes the cache based on expiration date, filtered by optional region.
/// </summary>
/// <param name="minDate">Minimum date of cache item last access date.</param>
/// <returns>The amount removed (in bytes).</returns>
public void Flush(DateTime minDate, string regionName = null);

/// <summary>
/// Shrinks the cache until the cache size is less than or equal to the size specified (in bytes).
/// This is a rather expensive operation, so use with discretion.
/// </summary>
/// <param name="newSize">New maximum cache size.</param>
/// <returns>The new size of the cache.</returns>
public long ShrinkCacheToSize(long newSize, string regionName = null)
Clone this wiki locally