Vetrivel mp
Vetrivel mp

Reputation: 1224

File Dependency Caching in MVC3

I am trying to implement file dependency caching in mvc3. As I am new to MVC, I browsed searched google but I didn't get any help.

Can any of our guys help me out? or what is the work around for this?

I tried same as what we do in asp.net but I get error.

Code I tried:

public ActionResult About()
    {
        Cache.Insert("DropDownData", "", new System.Web.Caching.CacheDependency(Server.MapPath("~/testxml.xml")));

        return View();
    }

Error That i got:

An object reference is required for the non-static field, method, or property 'System.Web.Caching.Cache.Insert(string, object, System.Web.Caching.CacheDependency)

Upvotes: 0

Views: 1475

Answers (3)

smartcaveman
smartcaveman

Reputation: 42256

Your problem is that you are calling the instance method Cache.Insert without a reference to an instance of the cache. ASP.NET MVC does not expose the default Cache in the Controller by Default.

In order to access it from the controller, you need to use HttpContext.Cache.Insert

That being said, this kind of behavior is more appropriately localized in an ActionFilter. Check out the following for more on this: http://www.asp.net/mvc/tutorials/older-versions/controllers-and-routing/improving-performance-with-output-caching-cs

Upvotes: 1

Dennis
Dennis

Reputation: 14475

Try

System.Web.HttpRuntime.Cache.Insert

System.Web.HttpRuntime.Cache is the cache instance of your current application.

Upvotes: 0

Man8Blue
Man8Blue

Reputation: 1197

Have you added reference correctly ?

Try to make an object likewise

CacheDependency CDep = new CacheDependency(fileName /*your file path*/, dt /*Set Start parameter to current date */);

and then use insert as

cache.Insert("DropDownData", "", CDep);

Upvotes: 0

Related Questions