Branch | Build status | Package |
---|---|---|
Master | ||
CI |
It is simple in-process file cache implementation inherited from standard ObjectCache class. FileCache is thread safe (supports multithread/multiprocessing parallel cache processing).
- 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.
- 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.
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))
//...
}
Current implementation has several logically separated parts that can be changed or overridden.
Encapsulates all IO operations. Library contains default implementation DefaultRepository. All methods in default implementation are virtual so can be overridden.
Cache location can be changed via constructor parameter.
To enable/disable cache keys and regions sanitizing use sanitizeNames constructor parameter. Default value is true.
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.
Fast proxy operations. DefaultMemoryCacheFacade you can configure internal items lifetime period via constructor parameter. Default value is 10 minutes.
Controls how cache item will be serialized and deserialized. DefaultDataSerializer have 2 stage serialization:
- Serialize CacheItem value (using BinaryFormatter)
- 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);
}
}