IMemoryCache and GetOrCreateForTimeSpanAsync
One thing that ASP.Net Core really got right was caching. IMemoryCache is a caching implementation that does just what I want.
TimeSpan, TimeSpan Expiration Y'all
To make usage of the IMemoryCache even more lovely I've written an extension method. I follow pretty much one cache strategy: SetAbsoluteExpiration and I just vary the expiration by an amount of time. This extension method implements that in a simple way; I call it GetOrCreateForTimeSpanAsync - catchy right? It looks like this:
Usage looks like this:
Where _cache is an instance of IMemoryCache that can be dependency injected into your class. This helper allows the consumer to provide three things:
- The key key for the item to be cached with
- A itemGetterAsync which is the method that is used to retrieve a new value if an item cannot be found in the cache
- A timeToCache which is the period of time that an item should be cached
If an item can't be looked up by the itemGetterAsync then nothing will be cached and a the default value of the expected type will be returned. This is important because lookups can fail, and there's nothing worse than a lookup failing and you caching null as a result.
Go on, ask me how I know.
This is a simple, clear and helpful API which makes interacting with IMemoryCache even more lovely than it was.
Discussion in the ATmosphere