HttpRuntime.Cache Equivalent for asp.net 5, MVC 6
You can an IMemoryCache
implementation for caching data. There are different implementations of this including an in-memory cache, redis,sql server caching etc..
Quick and simple implemenation goes like this
Update your project.json
file and add the below 2 items under dependencies
section.
"Microsoft.Extensions.Caching.Abstractions": "1.0.0-rc1-final",
"Microsoft.Extensions.Caching.Memory": "1.0.0-rc1-final"
Saving this file will do a dnu restore and the needed assemblies will be added to your project.
Go to Startup.cs class, enable caching by calling the services.AddCaching()
extension method in ConfigureServices
method.
public void ConfigureServices(IServiceCollection services)
{
services.AddCaching();
services.AddMvc();
}
Now you can inject IMemoryCache
to your class via constructor injection. The framework will resolve a concrete implementation for you and inject it to your class constructor.
public class HomeController : Controller
{
IMemoryCache memoryCache;
public HomeController(IMemoryCache memoryCache)
{
this.memoryCache = memoryCache;
}
public IActionResult Index()
{
var existingBadUsers = new List<int>();
var cacheKey = "BadUsers";
List<int> badUserIds = new List<int> { 5, 7, 8, 34 };
if(memoryCache.TryGetValue(cacheKey, out existingBadUsers))
{
var cachedUserIds = existingBadUsers;
}
else
{
memoryCache.Set(cacheKey, badUserIds);
}
return View();
}
}
Ideally you do not want to mix your caching within your controller. You may move it to another class/layer to keep everything readable and maintainable. You can still do the constructor injection there.
The official asp.net mvc repo has more samples for your reference.
My answer is focused on the "Does anyone know what the equivalent extension is for HttpRuntime as I cant seem to find it anywhere"
You tagged two different frameworks (.net and .net core), and for them there are two completely different caching implementations/solutions. The first one below is the one you were looking for:
1 - System.Runtime.Caching/MemoryCache
2 - Microsoft.Extensions.Caching.Memory/IMemoryCache
System.Runtime.Caching/MemoryCache:
This is pretty much the same as the old day's ASP.Net MVC's HttpRuntime.Cache
. You can use it on ASP.Net CORE without any dependency injection. This is how to use it:
// First install 'System.Runtime.Caching' (NuGet package)
// Add a using
using System.Runtime.Caching;
// To get a value
var myString = MemoryCache.Default["itemCacheKey"];
// To store a value
MemoryCache.Default["itemCacheKey"] = myString;
Microsoft.Extensions.Caching.Memory
This one is tightly coupled with Dependency Injection, and is the recommended way to do it on ASP.Net CORE. This is one way to implement it:
// In asp.net core's Startup add this:
public void ConfigureServices(IServiceCollection services)
{
services.AddMemoryCache();
}
Using it on a controller:
// Add a using
using Microsoft.Extensions.Caching.Memory;
// In your controller's constructor, you add the dependency on the 'IMemoryCache'
public class HomeController : Controller
{
private IMemoryCache _cache;
public HomeController(IMemoryCache memoryCache)
{
_cache = memoryCache;
}
public void Test()
{
// To get a value
string myString = null;
if (_cache.TryGetValue("itemCacheKey", out myString))
{ /* key/value found - myString has the key cache's value*/ }
// To store a value
_cache.Set("itemCacheKey", myString);
}
}