Gat*_*ler 50 c# asp.net outputcache
对于我的博客,我想使用输出缓存来保存一个特定帖子的缓存版本大约10分钟,这很好......
<%@OutputCache Duration="600" VaryByParam="*" %>
Run Code Online (Sandbox Code Playgroud)
但是,如果有人发表评论,我想清除缓存,以便刷新页面并查看评论.
我如何在ASP.Net C#中做到这一点?
Gat*_*ler 48
我找到了我想要的答案:
HttpResponse.RemoveOutputCacheItem("/caching/CacheForever.aspx");
Run Code Online (Sandbox Code Playgroud)
Kev*_*vin 40
如果您知道要清除缓存的页面,则上述情况很好.在我的实例(ASP.NET MVC)中,我引用了来自全世界的相同数据.因此,当我做[保存]时,我想清除缓存网站.这对我有用:http://aspalliance.com/668
这是在OnActionExecuting过滤器的上下文中完成的.它可以通过在BaseController中覆盖OnActionExecuting或其他东西来轻松完成.
HttpContextBase httpContext = filterContext.HttpContext;
httpContext.Response.AddCacheItemDependency("Pages");
Run Code Online (Sandbox Code Playgroud)
建立:
protected void Application_Start()
{
HttpRuntime.Cache.Insert("Pages", DateTime.Now);
}
Run Code Online (Sandbox Code Playgroud)
Minor Tweak:我有一个助手添加"flash消息"(错误消息,成功消息 - "此项已成功保存"等).为了避免闪存消息出现在每个后续的GET上,我必须在写入flash消息后失效.
清除缓存:
HttpRuntime.Cache.Insert("Pages", DateTime.Now);
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助.
小智 5
使用Response.AddCacheItemDependency清除所有输出缓存.
public class Page : System.Web.UI.Page
{
protected override void OnLoad(EventArgs e)
{
try
{
string cacheKey = "cacheKey";
object cache = HttpContext.Current.Cache[cacheKey];
if (cache == null)
{
HttpContext.Current.Cache[cacheKey] = DateTime.UtcNow.ToString();
}
Response.AddCacheItemDependency(cacheKey);
}
catch (Exception ex)
{
throw new SystemException(ex.Message);
}
base.OnLoad(e);
}
}
// Clear All OutPutCache Method
public void ClearAllOutPutCache()
{
string cacheKey = "cacheKey";
HttpContext.Cache.Remove(cacheKey);
}
Run Code Online (Sandbox Code Playgroud)
这也可以在ASP.NET MVC的OutputCachedPage中使用.
归档时间: |
|
查看次数: |
91864 次 |
最近记录: |