简体   繁体   English

C#在MVC3项目中使用OutputCache

[英]C# Using OutputCache in MVC3 Project

I am using MCV3 OutputCache to decrease the loading times of a page with a table full of data. 我正在使用MCV3 OutputCache来减少包含数据表的页面的加载时间。 I use ajax methods to update information and manipulate the DOM to show the user that their change has been succesful. 我使用ajax方法来更新信息并操纵DOM,以向用户显示其更改已成功。 This is fine until they load the page and the cached dataset is loaded instead of the updated one. 这很好,直到他们加载页面并加载缓存的数据集而不是更新的数据集。

When the an Update method is called I would like to clear the cache or remove it, so that it is recreated on reload of the page, with the new updated data. 当调用Update方法时,我想清除或删除缓存,以便在页面重新加载时使用新的更新数据重新创建它。

My code is as follows: 我的代码如下:

[OutputCache(CacheProfile = "VideoIndexView")]
public ActionResult Index()
{
    ...
    return View(model);
}

当您想从缓存中清除一些URL时,可以调用RemoveOutputCacheItem静态方法。

You could use your Index action result to load a template of the screen and use AJAX to get and load the actual data. 您可以使用“ Index操作结果来加载屏幕模板,并使用AJAX来获取和加载实际数据。

[OutputCache(CacheProfile = "VideoIndexView")]
public ActionResult Index()
{
    ...
    return View(model);  // Really only return a model that is okay to be cached
}

public ActionResult LoadData ()
{
    var Result = // Load the data
    ...
    return Json(Result);  // Don't forget to allow GET here if you're using HTTPGET
}

// Or...

public ActionResult LoadData ()
{
    var Result = // Load the data
    ...
    return PartialView (Result);
}

This way, Index can be cached just fine and the data will be loaded and injected into the page after the page has been served to the user. 这样,可以很好地缓存Index并且在将页面提供给用户之后,数据将被加载并注入到页面中。 If you are going to use something like jQuery, be sure to tell it not to use cached results if you're using GET. 如果要使用类似jQuery的内容,请确保告诉它不要使用GET缓存的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM