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.

8
  • Possible duplicate of How can I cache objects in ASP.NET MVC? Commented Jan 18, 2016 at 14:41
  • Where do you access the cache? Commented Jan 18, 2016 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? Commented Jan 18, 2016 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? Commented Jan 18, 2016 at 14:47
  • 1
    try HttpRuntime.Cache? Commented Jan 18, 2016 at 15:02

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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();
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.