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 IMemoryCache
item 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:
After the function and all extensions have completed, Lambda maintains the execution environment for some time in anticipation of another function invocation. In effect, Lambda freezes the execution environment. When the function is invoked again, Lambda thaws the environment for reuse. When you write your function code, do not assume that Lambda automatically reuses the execution environment for subsequent function invocations. Other factors may dictate a need for Lambda to create a new execution environment
And:
Shutdown phase is triggered if the Lambda Function does not receive any invocations for a period of time. That period of time is dynamic since Lambda service can determine to reallocate unused execution environment resources if needed elsewhere.
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: