Skip to content

Latest commit

 

History

History
16 lines (16 loc) · 618 Bytes

File metadata and controls

16 lines (16 loc) · 618 Bytes

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;
        }
      );
      ...