I'm using the Microsoft.Extensions.Caching.Memory (Version 6.1) and the item is removed from the memory cache after around 5 min without being accessed.
I tried to add explicit values to AbsoluteExpirationRelativeToNow and SlidingExpiration.
Locally is working perfectly, but on the AWS Lambda environment, looks like the item is removed after 5 min.
here is part of my code:
using Microsoft.Extensions.Caching.Memory;
...
public myClass(IMemoryCache memoryCache, Repository myRepository, ILogger < myClass > logger) {
_memoryCache = memoryCache ??
throw new ArgumentNullException(nameof(memoryCache));
_myRepository = myRepository ??
throw new ArgumentNullException(nameof(myRepository));
_logger = logger ??
throw new ArgumentNullException(nameof(logger));
}
...
public async Task < bool > isItemInCache(int id) {
bool ret = true;
var key = id;
if (!_memoryCache.TryGetValue < bool > (key, out ret)) {
// query data from MSSQL
string valueFromDB = _myRepository.GetAsync(id, config_id).Result;
_logger.LogInformation(accountId + " Getting config from MSSQL: " + valueFromDB);
Boolean.TryParse(valueFromDB, out ret);
var memoryCacheEntryOptions = new MemoryCacheEntryOptions() {
AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(60),
SlidingExpiration = TimeSpan.FromMinutes(30)
};
_memoryCache.Set < bool > (key, ret, memoryCacheEntryOptions);
}
}
else {
_logger.LogInformation(id + " Getting config from MemoryCache: " + ret.ToString());
}
return ret;
}
/**Here is part of my dependency injection on Startup.CS file:**/
services.AddLogging(loggingBuilder => {
loggingBuilder.ClearProviders();
loggingBuilder.SetMinimumLevel((LogLevel)((int) logLevel));
loggingBuilder.AddSerilog();
})
.AddSingleton(Configuration)
.AddMemoryCache()
.....
I'm wondering if the SlidingExpiration is being overridden by some setting or policy of the AWS environment. Does anyone have any idea why it not keeping the item in memory no more than 5 minutes?
Setting the
IMemoryCacheitem expiration has effect only for the same AWS Lambda execution environment and it is up for AWS to decide when to shutdown/initialize new execution environments:And:
I.e. if new execution environment will be created the new cache will be initialized, so first of all try finding out if the execution environment was shut down or a new one was created.
Read more/Sources: