Skip to content

About

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

Resources

Stars

0 stars

Watchers

1 watching

Forks

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

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages