对于我的博客,我想使用输出缓存来保存特定帖子的缓存版本大约 10 分钟,这很好...
<%@OutputCache Duration="600" VaryByParam="*" %>
但是,如果有人发表评论,我想清除缓存,以便刷新页面并可以看到评论。
如何在 ASP.Net C# 中执行此操作?
我找到了我正在寻找的答案:
HttpResponse.RemoveOutputCacheItem("/caching/CacheForever.aspx");
如果您知道要清除哪些页面的缓存,以上就可以了。在我的实例(ASP.NET MVC)中,我引用了来自各地的相同数据。因此,当我执行[保存]时,我想清除整个站点的缓存。这对我有用:http://aspalliance.com/668
这是在 OnActionExecuting 过滤器的上下文中完成的。它可以通过重写 BaseController 或其他东西中的 OnActionExecuting 来轻松完成。
HttpContextBase httpContext = filterContext.HttpContext;
httpContext.Response.AddCacheItemDependency("Pages");
设置:
protected void Application_Start()
{
HttpRuntime.Cache.Insert("Pages", DateTime.Now);
}
小调整: 我有一个助手,可以添加“闪现消息”(错误消息、成功消息 - “此项目已成功保存”等)。为了避免在后续的每次 GET 中都出现 Flash 消息,我不得不在写入 Flash 消息后使其失效。
清除缓存:
HttpRuntime.Cache.Insert("Pages", DateTime.Now);
希望这有帮助。
使用 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);
}
这也可以在 ASP.NET MVC 的 OutputCachedPage 中使用。
在母版页加载事件上,请编写以下内容:
Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();
然后在注销按钮中单击:
Session.Abandon();
Session.Clear();
嗯。您可以在 OutputCache 项上指定 VaryByCustom 属性。该值作为参数传递给您可以在 global.asax 中实现的 GetVaryByCustomString 方法。此方法返回的值用作缓存项目的索引 - 例如,如果您返回页面上的评论数量,则每次添加评论时都会缓存一个新页面。
需要注意的是,这实际上并没有清除缓存。如果博客条目的评论使用量很大,则使用此方法您的缓存大小可能会爆炸。
或者,您可以将页面的不可更改部分(导航、广告、实际博客条目)实现为用户控件,并在每个用户控件上实现部分页面缓存。
如果将“*”更改为缓存应变化的参数(PostID?),您可以执行以下操作:
//add dependency
string key = "post.aspx?id=" + PostID.ToString();
Cache[key] = new object();
Response.AddCacheItemDependency(key);
当有人添加评论时...
Cache.Remove(key);
我想即使使用 VaryByParam * 也可以工作,因为所有请求都将绑定到相同的缓存依赖项。
为什么不在 posts 表上使用 sqlcachedependency?
这样您就不会实现自定义缓存清除代码,而只是在数据库中的内容发生变化时刷新缓存?
首先,请确保您已正确设置我们的缓存。您对 ASPX 文件中的 OutputCache 指令的想法是正确的。这一行:
<%@ OutputCache Duration="600" VaryByParam="*" %>
告诉 ASP.NET 将页面缓存 10 分钟(600 秒),但它会根据不同的参数改变缓存,确保页面的每个版本都单独存储在缓存中。
现在,当发布新评论时,您需要清除缓存,以便页面刷新并且每个人都可以看到最新评论。为此,我们将在代码隐藏文件中使用一些 C# 代码(可能类似于
YourPageName.aspx.cs
)。
在代码隐藏中,您需要调用
HttpResponse.RemoveOutputCacheItem
方法来清除特定页面的缓存。每当发布新评论时,您都需要调用此方法。
以下是如何执行此操作的基本示例:
protected void PostCommentButton_Click(object sender, EventArgs e)
{
// Code to save the comment to the database goes here...
// Now, let's clear the cache for the current page
string pageUrl = HttpContext.Current.Request.Url.AbsolutePath;
HttpResponse.RemoveOutputCacheItem(pageUrl);
}
在此代码片段中,
PostCommentButton_Click
是某人提交新评论时的事件处理程序。将评论保存到数据库后(您需要添加该部分),我们使用 HttpContext.Current.Request.Url.AbsolutePath 获取当前页面的 URL。然后,我们使用页面 URL 调用 HttpResponse.RemoveOutputCacheItem 来清除该特定页面的缓存。
HttpRuntime.Close()
..我尝试了所有方法,这是唯一对我有用的