Skip to content

wilsonneto-swe/demo-in-memory-cache

Repository files navigation

Steps:

  • configure the memory cache on Services: services.AddMemoryCache();
  • Inject it with DI: [FromServices]IMemoryCache cache
  • Get the cached data or set it with GetOrCreate: (SlidingExpiration or AbsoluteExpirationRelativeToNow to control the cache expiration)
      ...
      var date = cache.GetOrCreate("MyCacheKey", item =>
        {
          item.SlidingExpiration = TimeSpan.FromSeconds(1);
          // entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(1);
          // entry.SetPriority(CacheItemPriority.High);
          return DateTime.Now;
        }
      );
      ...

About

📃 A simple example and reference of how to implements a simple memory cache in a .net core API.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages