4

I am using Asp.net 5 and MVC 6 targeting the .Net framework 4.5.2 I want to use the following code:

Cache["test"] = "test";

or

HttpContext.Cache["test"] = "test";

But both get the following error that Cache doesn't exist in this context. What am I missing??

Edit:

As answered below, you can cache using the IMemoryCache interface by injecting it into your controller. This seems to be new in asp.net 5 RC1.

Tom el Safadi
  • 6,164
  • 5
  • 49
  • 102
  • Possible duplicate of [How can I cache objects in ASP.NET MVC?](http://stackoverflow.com/questions/445050/how-can-i-cache-objects-in-asp-net-mvc) – teo van kot Jan 18 '16 at 14:41
  • Where do you access the cache? – Win Jan 18 '16 at 14:43
  • I could not replicate it. `public ActionResult Index() {HttpContext.Cache["Name"] = 123; var value = HttpContext.Cache["Name"]; return View();}` Could you create a new ASP.Net project and try again? – Win Jan 18 '16 at 14:45
  • Yes I created a new project it still says that HttpContext doesn't contain a definition for Cache, thats very strange, do I have to add some packages or directives? – Tom el Safadi Jan 18 '16 at 14:47
  • 1
    try `HttpRuntime.Cache`? – terbubbs Jan 18 '16 at 15:02
  • Ok, if I use cache as well as session in asp.net 4 it works. But somehow this doesn't work in Asp.net 5 – Tom el Safadi Jan 18 '16 at 15:04
  • http://stackoverflow.com/questions/28269072/how-to-cache-resources-in-mvc-6 maybe this will help? – terbubbs Jan 18 '16 at 15:06
  • Try `Context.HttpContext.Cache.Get("Name");` or `Context.HttpContext.Cache.Insert("Name", values);` – usselite Jan 18 '16 at 15:39

2 Answers2

4

Update your startup.cs to have this inside ConfigureServices:

services.AddCaching();

Then update the controller to have a dependency of IMemoryCache:

public class HomeController : Controller
{
    private IMemoryCache cache;

    public HomeController(IMemoryCache cache)
    {
        this.cache = cache;
    }

Then you can use it in your actions like:

    public IActionResult Index()
    {
        // Set Cache
        var myList = new List<string>();
        myList.Add("lorem");
        this.cache.Set("MyKey", myList, new MemoryCacheEntryOptions());
        return View();
    }

And

    public IActionResult About()
    {
        ViewData["Message"] = "Your application description page.";

        // Read cache
        var myList= this.cache.Get("MyKey");

        // Use value

        return View();
    }

Much more detail on MemoryCache on dotnet.today.

NikolaiDante
  • 18,469
  • 14
  • 77
  • 117
2

In MVC 6, you can cache using the IMemoryCache interface by injecting it into your controller.

using Microsoft.Extensions.Caching.Memory;

public class HomeController
{
    private readonly IMemoryCache _cache;

    public HomeController(IMemoryCache cache)
    {
        if (cache == null)
            throw new ArgumentNullException("cache");
        _cache = cache;
    }

    public IActionResult Index()
    {
        // Get an item from the cache
        string key = "test";
        object value;
        if (_cache.TryGetValue(key, out value))
        {
            // Reload the value here from wherever
            // you need to get it from
            value = "test";

            _cache.Set(key, value);
        }

        // Do something with the value

        return View();
    }
}
NightOwl888
  • 55,572
  • 24
  • 139
  • 212