Open Package Manager Console and run:
Install-Package EasyMemoryCache
First, register the component in your Application:
.AddEasyCache(new CacheSettings() { ... })
//setup our DI
var serviceProvider = new ServiceCollection()
.AddEasyCache(new CacheSettings() { ... })
.BuildServiceProvider();
var caching = serviceProvider.GetService<ICaching>();
return caching;
services.AddEasyCache(Configuration.GetSection("CacheSettings").Get<CacheSettings>());
"CacheSettings": {
"CacheProvider": "Redis",
"IsDistributed": true,
"RedisConnectionString": "localhost:6379,password=xxx=,ssl=False,abortConnect=False"
}
For MemoryCache
"IsDistributed": false,
"CacheProvider": "MemoryCache",
InMemory cache will be used instead of Redis
private readonly ICaching _caching;
private string UserKeyCache => "UserKey";
public UserService(ICaching caching)
{
_caching = caching;
}
var lstStringFromAsync = await _caching.GetOrSetObjectFromCacheAsync(CacheKeyNameForAsync, 20, ReturnListOfStringAsync);
var lstStringFromAsync = await _caching.GetOrSetObjectFromCacheAsync(CacheKeyNameForAsync, 20, () => ReturnListOfStringAsync(param));
var lstString = _caching.GetOrSetObjectFromCache(CacheKeyName, 20, ReturnListOfString);