© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
1 reply
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.
Cache in-memory in ASP.NET Core
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

✅ IMemoryCache missing Contains?
C#CC# / help
2y ago
How to properly use IMemoryCache?
C#CC# / help
10mo ago
❔ Is there any difference between `[Route("")]` and `[Route("/")]`?
C#CC# / help
3y ago
✅ Is there a difference when we use { get; init; } vs { get; } ?
C#CC# / help
11mo ago