, ... , IMemoryCache, , ActionFilterAttribute .
( .Net core 2.1 Microsoft docs + my ):
1- services.AddMemoryCache(); ConfigureServices Startup.cs.
2- :
public class HomeController : Controller
{
private IMemoryCache _cache;
public HomeController(IMemoryCache memoryCache)
{
_cache = memoryCache;
}
3- ( ) , :
public static class CacheKeys
{
public static string SomeKey { get { return "someKey"; } }
public static string AnotherKey { get { return "anotherKey"; } }
... list could be goes on based on your needs ...
enum:
public enum CacheKeys { someKey, anotherKey,...}
3- actionMethods, :
: _cache.TryGetValue(CacheKeys.SomeKey, out someValue)
, TryGetValue :
_cache.Set(CacheKeys.SomeKey,
newCachableValue,
new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromSeconds(60)));
.