A simple C# cacher. Caching in file.
- Firstly create cache provider
var provider = new BMCacheProvider();| Agrument | type | required | default | description |
|---|---|---|---|---|
| dir | string | - | bmcache | directory where storage cache data |
| defaultExpirationTime | TimeStamp | - | 24 hours | Default expiration time to delete cached file |
- Create cacher by provider. DO NOT create it by yourself (using new BMCacher()).
var cacher = provider.CreateCacher("test");| Agrument | type | required | default | description |
|---|---|---|---|---|
| name | string | + | directory (and cacher) name, where to save cached data | |
| defaultExpirationTime | TimeStamp | - | Provider`s defaultExpirationTime | Default expiration time to delete cached file |
- Cache
// You can cache and get it back via diferrent methods
var variableToCache = "cache me";
cacher.Cache("identifier", variableToCache);
var variableFromCache = cacher.Get<string>("identifier");
// You can use default methods with just value argument.
// This methods will give you cached before value
// or cache it and throw back
var cachedTime = cacher.GetOrCache<DateTime>("dt", DateTime.UtcNow);
var cachedDoub = await cacher.GetOrCacheAsync<double>("double", 123.456);
// Or use value generator
Func<int> intFunction = () => 12345;
var cachedNum = cacher.GetOrCache<int>("num", intFunction);
var cachedNumAsync = await cacher.GetOrCacheAsync<int>("num", intFunction);
// Or async value generator
Func<Task<List<string>>> listAsyncFunc = async () => new List<string>() { "first", "second", "third" };
var cachedList = await cacher.GetOrCacheAsync<List<string>>("list", listAsyncFunc);In addition you can set expiration time for each caching separately
// In example, this row will cache string for 2 hour from now
cacher.Cache("identifier", "string to cache", new TimeStamp(2, 0, 0));*For expiration control, library using UTC time
