The cache for child actions is stored in the OutputCacheAttribute.ChildActionCache property. The problem is that the API that generates identifiers for child actions and stores them in this object is not publicly available (WHY Microsoft?). Therefore, if you try to skip objects in this collection, you will find that it will also contain a cached value for your action with the child, but you cannot identify it unless you reverse engineer the algorithm used to generate keys that look like something something like this (as seen with Reflector):
internal string GetChildActionUniqueId(ActionExecutingContext filterContext) { StringBuilder builder = new StringBuilder(); builder.Append("_MvcChildActionCache_"); builder.Append(filterContext.ActionDescriptor.UniqueId); builder.Append(DescriptorUtil.CreateUniqueId(new object[] { this.VaryByCustom })); if (!string.IsNullOrEmpty(this.VaryByCustom)) { string varyByCustomString = filterContext.HttpContext.ApplicationInstance.GetVaryByCustomString(HttpContext.Current, this.VaryByCustom); builder.Append(varyByCustomString); } builder.Append(GetUniqueIdFromActionParameters(filterContext, SplitVaryByParam(this.VaryByParam))); using (SHA256 sha = SHA256.Create()) { return Convert.ToBase64String(sha.ComputeHash(Encoding.UTF8.GetBytes(builder.ToString()))); } }
So you can do the following madness:
public ActionResult Invalidate() { OutputCacheAttribute.ChildActionCache = new MemoryCache("NewDefault"); return View(); }
which, obviously, will invalidate all cached actions, which might not be what you are looking for, but I'm afraid that this is the only way, except, of course, reverse engineering key generation :-).
@Microsoft, please, I ask you for ASP.NET MVC 4.0:
- introduce the ability to cache donuts in addition to caching holes in the don.
- introduce the ability to easily end the result of the action of the cached controller (something more MVCish than
Response.RemoveOutputCacheItem ) - introduce the ability to easily complete the result of a cached child action
- if you follow 1. then, obviously, imagine expiring the donut cache.
Darin Dimitrov
source share