Can't access HttpRuntime.Cache in .NET Standard class library

Viewed 1620

I'm beginning to port our .NET Framework class libraries to .NET Standard 2.0. Right away, I'm running into an issue with not finding an equivalent to:

private readonly System.Web.Caching.Cache _cache = HttpRuntime.Cache;

I understand that a .NET Standard project needs to support multiple platforms so might not be able to handle logic like this from System.Web anymore, but I need a way to access http caching within class libraries. I'm sure this is a very common request as developers port over their applications, any suggestions?

Note: I saw this link about System.Runtime.Caching, is that the new way to handle http caching?

1 Answers

You can use System.Runtime.Caching using nuget packages

Then use following methods to manage cache

To Add:

MemoryCache.Default.Add(key, value, new CacheItemPolicy { SlidingExpiration = new TimeSpan(some hours, some minutes, some seconds) });

To Get:

return MemoryCache.Default.Get(key);

To Remove:

MemoryCache.Default.Remove(key);
Related