I need an application level cache in my MVC3 project.
I want to use something like this in a controller:
using System.Web.Caching;
protected IMyStuff GetStuff(string stuffkey)
{
var ret = Cache[stuffkey];
if (ret == null)
{
ret = LoadStuffFromDB(stuffkey);
Cache[stuffkey] = ret;
}
return (IMyStuff)ret;
}
This fails because Cache[“foo”] don’t compile as “System.Web.Caching.Cache is a ‘type’ but used like a ‘variable'”.
I see that Cache is a class, but there are quite a few examples on the net when it is used like Session[“asdf”] in the controller like it is a property.
What am I doing wrong?
There is a property named
Sessionin controller but there is no property namedCache.You should use
HttpRuntime.Cachestatic property in order to getCacheobject.For example: