<p>For my blog I am wanting to use the Output Cache to save a cached version of a perticular post for around 10 minutes, and thats fine...</p>

<pre><code>&lt;%@OutputCache Duration="600" VaryByParam="*" %&gt;
</code></pre>

<p>However, if someone posts a comment, I want to clear the cache so that the page is refreshed and the comment can be seen.</p>

<p>How do I do this in ASP.Net C#?</p>

[{'answer_id': 11611, 'author': 'John Christensen', 'author_id': 1194, 'author_profile': 'https://Stackoverflow.com/users/1194', 'pm_score': 1, 'selected': False, 'text': '<p>Hmm. You can specify a VaryByCustom attribute on the OutputCache item. The value of this is passed as a parameter to the GetVaryByCustomString method that you can implement in global.asax. The value returned by this method is used as an index into the cached items - if you return the number of comments on the page, for instance, each time a comment is added a new page will be cached.</p>\n\n<p>The caveat to this is that this does not actually clear the cache. If a blog entry gets heavy comment usage, your cache could explode in size with this method.</p>\n\n<p>Alternatively, you could implement the non-changeable bits of the page (the navigation, ads, the actual blog entry) as user controls and implement partial page caching on each of those user controls.</p>\n'}, {'answer_id': 11621, 'author': 'palmsey', 'author_id': 521, 'author_profile': 'https://Stackoverflow.com/users/521', 'pm_score': 1, 'selected': False, 'text': '<p>If you change "*" to just the parameters the cache should vary on (PostID?) you can do something like this:</p>\n\n<pre><code>//add dependency\nstring key = "post.aspx?id=" + PostID.ToString();\nCache[key] = new object();\nResponse.AddCacheItemDependency(key);\n</code></pre>\n\n<p>and when someone adds a comment...</p>\n\n<pre><code>Cache.Remove(key);\n</code></pre>\n\n<p>I guess this would work even with VaryByParam *, since all requests would be tied to the same cache dependency.</p>\n'}, {'answer_id': 11641, 'author': 'GateKiller', 'author_id': 383, 'author_profile': 'https://Stackoverflow.com/users/383', 'pm_score': 7, 'selected': True, 'text': '<p>I\'ve found the answer I was looking for:</p>\n\n<pre><code>HttpResponse.RemoveOutputCacheItem("/caching/CacheForever.aspx");\n</code></pre>\n'}, {'answer_id': 416601, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 3, 'selected': False, 'text': '<p>Using Response.AddCacheItemDependency to clear all outputcaches.</p>\n\n<pre><code>  public class Page : System.Web.UI.Page\n  {\n    protected override void OnLoad(EventArgs e)\n    {\n        try\n        {\n            string cacheKey = "cacheKey";\n            object cache = HttpContext.Current.Cache[cacheKey];\n            if (cache == null)\n            {\n              HttpContext.Current.Cache[cacheKey] = DateTime.UtcNow.ToString();\n            }\n\n            Response.AddCacheItemDependency(cacheKey);\n        }\n        catch (Exception ex)\n        {\n            throw new SystemException(ex.Message);\n        }\n\n        base.OnLoad(e);\n    }     \n }\n\n\n\n  // Clear All OutPutCache Method    \n\n    public void ClearAllOutPutCache()\n    {\n        string cacheKey = "cacheKey";\n        HttpContext.Cache.Remove(cacheKey);\n    }\n</code></pre>\n\n<p>This is also can be used in ASP.NET MVC\'s OutputCachedPage.</p>\n'}, {'answer_id': 1990123, 'author': 'Brian Scott', 'author_id': 135731, 'author_profile': 'https://Stackoverflow.com/users/135731', 'pm_score': 1, 'selected': False, 'text': '<p>why not use the sqlcachedependency on the posts table?</p>\n\n<p><a href="http://msdn.microsoft.com/en-us/library/ms178604.aspx" rel="nofollow noreferrer">sqlcachedependency msdn</a></p>\n\n<p>This way your not implementing custom cache clearing code and simply refreshing the cache as the content changes in the db?</p>\n'}, {'answer_id': 2876701, 'author': 'Kevin', 'author_id': 153942, 'author_profile': 'https://Stackoverflow.com/users/153942', 'pm_score': 5, 'selected': False, 'text': '<p>The above are fine if you know what pages you want to clear the cache for. In my instance (ASP.NET MVC) I referenced the same data from all over. Therefore, when I did a [save] I wanted to clear cache site wide. This is what worked for me: <a href="http://aspalliance.com/668" rel="noreferrer">http://aspalliance.com/668</a></p>\n\n<p>This is done in the context of an OnActionExecuting filter. It could just as easily be done by overriding OnActionExecuting in a BaseController or something.</p>\n\n<pre><code>HttpContextBase httpContext = filterContext.HttpContext;\nhttpContext.Response.AddCacheItemDependency("Pages");\n</code></pre>\n\n<p>Setup:</p>\n\n<pre><code>protected void Application_Start()\n{\n    HttpRuntime.Cache.Insert("Pages", DateTime.Now);\n}\n</code></pre>\n\n<p>Minor Tweak:\nI have a helper which adds "flash messages" (Error messages, success messages - "This item has been successfully saved", etc). In order to avoid the flash message from showing up on every subsequent GET, I had to invalidate after writing the flash message.</p>\n\n<p>Clearing Cache:</p>\n\n<pre><code>HttpRuntime.Cache.Insert("Pages", DateTime.Now);\n</code></pre>\n\n<p>Hope this helps.</p>\n'}, {'answer_id': 4727875, 'author': 'Julien', 'author_id': 280617, 'author_profile': 'https://Stackoverflow.com/users/280617', 'pm_score': -1, 'selected': False, 'text': '<p><code>HttpRuntime.Close()</code> .. I try all method and this is the only that work for me</p>\n'}, {'answer_id': 6439754, 'author': 'Mohd Adil', 'author_id': 810324, 'author_profile': 'https://Stackoverflow.com/users/810324', 'pm_score': 2, 'selected': False, 'text': '<p>On the master page load event, please write the following:</p>\n\n<pre><code>Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));\nResponse.Cache.SetCacheability(HttpCacheability.NoCache);\nResponse.Cache.SetNoStore();\n</code></pre>\n\n<p>and in the logout button click:</p>\n\n<pre><code>Session.Abandon();\nSession.Clear();\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/11585', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/383/']