Skip to content

Latest commit

 

History

History
107 lines (74 loc) · 5.75 KB

README.md

File metadata and controls

107 lines (74 loc) · 5.75 KB
Branch Build status Package
Master Build status NuGet version (PS.Memory.Cache)
CI Build status MyGet version (PS.Memory.Cache)

Description

It is simple in-process file cache implementation inherited from standard ObjectCache class. FileCache is thread safe (supports multithread/multiprocessing parallel cache processing).

Capabilities

Support

  • AbsoluteExpirations: The ability to automatically remove cache entries at a specific date and time.
  • SlidingExpirations: The ability to automatically remove cache entries that have not been accessed in a specified time span.
  • CacheRegions: The ability to partition its storage into cache regions, and supports the ability to insert cache entries into those regions and to retrieve cache entries from those regions.
  • InMemoryProvider: Current implementation uses [MemoryCache] internally as fast proxy.

Not support

  • CacheEntryChangeMonitors: The ability to create change monitors that monitor entries.
  • CacheEntryRemovedCallback: Can raise a notification that an entry has been removed from the cache.
  • CacheEntryUpdateCallback: Can raise a notification that an entry is about to be removed from the cache. This setting also indicates that a cache implementation supports the ability to automatically replace the entry that is being removed with a new cache entry.

How to use

Generic usage is the same as MemoryCache usage.

To setup cache location use DefaultRepository as FileCache constructor parameter. Default location is <app folder>\Cache\

using (var repository = new DefaultRepository(Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N"))))
{
    var cache = new FileCache(repository))
   //...
}

Settings

Current implementation has several logically separated parts that can be changed or overridden.

IRepository

Encapsulates all IO operations. Library contains default implementation DefaultRepository. All methods in default implementation are virtual so can be overridden.

Cache location

Cache location can be changed via constructor parameter.

Key and Region name sanitizing

To enable/disable cache keys and regions sanitizing use sanitizeNames constructor parameter. Default value is true.

Cleanup settings

Default implementation supports automatic repository cleaning within a fixed period of time (2 seconds by default) on instance dispose and via manual DefaultRepository.Cleanup() call. Clean operation means delete all files that were marked as deleted or expired.

To prevent file access issues there is delay options (5 seconds by default) which means file is allowed for deletion only after specified period.

var cleanupSettings = new CleanupSettings //Default settings defined in static CleanupSettings.Default property
{
    GuarantyFileLifetimePeriod = TimeSpan.FromSeconds(5),
    CleanupPeriod = TimeSpan.FromSeconds(2)
};

using (var repository = new DefaultRepository(cleanupSettings: cleanupSettings))
{
   var cache = new FileCache(repository)
   //...
}

Also CleanupSettings.Infinite static property can be used to disable cleanup functionality.

IMemoryCacheFacade

Fast proxy operations. DefaultMemoryCacheFacade you can configure internal items lifetime period via constructor parameter. Default value is 10 minutes.

IDataSerializer

Controls how cache item will be serialized and deserialized. DefaultDataSerializer have 2 stage serialization:

  1. Serialize CacheItem value (using BinaryFormatter)
  2. Serialize CacheItem itself (using binary reader and writer).

Cache values serialization can be easily modified.

Json data serialization/deserialization using Json.NET

class JsonDataSerializer : DefaultDataSerializer
{
    protected override object DeserializeData(Type type, byte[] data)
    {
        var json = Encoding.UTF8.GetString(data);
        return JsonConvert.DeserializeObject(json, type);
    }

    protected override byte[] SerializeData(Type type, object data)
    {
        var json = JsonConvert.SerializeObject(data);
        return Encoding.UTF8.GetBytes(json);
    }
}