我想在 ASP.NET CacheObject 中有一个项目,如果它被更改,许多相关项目将被删除
所以..在一个请求中
- 如果提示并且它存在于缓存中删除根对象,所有依赖也将被删除
- 检查缓存中的根对象,如果它不存在,则添加它
- 将依赖于根对象的其他对象添加到缓存中
当我这样做时,我得到一个错误“ An attempt was made to reference a CacheDependency object from more than one Cache entry
”
我看到您可以执行 AggregateCacheDependency 将许多依赖项应用于一个缓存项,但似乎您不能以相反的方式执行此操作。
有没有人找到一种方法来做到这一点?
这是一些代码,它不是我实际存储的,但它代表相同的任务
public class HomeController : Controller
{
private const string ROOT_KEY = "ROOT";
protected override void Initialize(System.Web.Routing.RequestContext requestContext)
{
base.Initialize(requestContext);
if(Request.QueryString["clearcache"]!=null){
// removed the root, hopefully removing all dependents
HttpContext.Cache.Remove(ROOT_KEY);
}
if (HttpContext.Cache[ROOT_KEY] == null)
{
// create the root entry
HttpContext.Cache[ROOT_KEY] = string.Empty;
}
if(HttpContext.Cache[Request.Url.AbsolutePath]==null){
// add the url if not already added
HttpContext.Cache.Insert(
Request.Url.AbsolutePath, string.Empty,
new CacheDependency(null, new []{ROOT_KEY}));
}
}
}