C#C
C#3y 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));

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);
}
Learn how to cache data in memory in ASP.NET Core.
Was this page helpful?