C
C#8mo ago
Aaron I

✅ Caching with IMemoryCache, is there a difference?

Is there any difference between this:
var entity = ...;
_cache.CreateEntry(entity.id).SetValue(entity).SetSlidingExpiration(TimeSpan.FromMinutes(ExpirationInMinutes));
var entity = ...;
_cache.CreateEntry(entity.id).SetValue(entity).SetSlidingExpiration(TimeSpan.FromMinutes(ExpirationInMinutes));
and the way that presented in the docs ?:
public IActionResult CacheTryGetValueSet()
{
DateTime cacheEntry;

if (!_cache.TryGetValue(CacheKeys.Entry, out cacheEntry))
{
cacheEntry = DateTime.Now;

var cacheEntryOptions = new MemoryCacheEntryOptions()
.SetSlidingExpiration(TimeSpan.FromSeconds(3));

_cache.Set(CacheKeys.Entry, cacheEntry, cacheEntryOptions);
}

return View("Cache", cacheEntry);
}
public IActionResult CacheTryGetValueSet()
{
DateTime cacheEntry;

if (!_cache.TryGetValue(CacheKeys.Entry, out cacheEntry))
{
cacheEntry = DateTime.Now;

var cacheEntryOptions = new MemoryCacheEntryOptions()
.SetSlidingExpiration(TimeSpan.FromSeconds(3));

_cache.Set(CacheKeys.Entry, cacheEntry, cacheEntryOptions);
}

return View("Cache", cacheEntry);
}
Cache in-memory in ASP.NET Core
Learn how to cache data in memory in ASP.NET Core.
1 Reply
Aaron I
Aaron I8mo ago
nvm