CacheDependancy多个缓存的项目 [英] CacheDependancy for multiple Cached items
问题描述
我想有在ASP.NET CacheObject,这好像是改变了一些相关的物品会被删除的项目
所以..在请求
- 如果出现提示,它存在于缓存中删除根对象,所有的依赖也将被删除
- 检查根对象在缓存中,如果它不存在,添加它
- 要缓存添加其他对象的根对象 在依赖
当我这样做,我得到一个错误试图从多个缓存条目中引用的CacheDependency对象
我看到,你可以做一个AggregateCacheDependency许多依赖应用到一个缓存项,但似乎你不能做到这一点周围的其他方法。
有没有人找到了一种方法来做到这一点?
这里是一些code,它不是我所实际存储,但它重新presents相同的任务
公共类HomeController的:控制器
{
私人常量字符串ROOT_KEY =ROOT; 保护覆盖无效初始化(System.Web.Routing.RequestContext的RequestContext)
{
base.Initialize(RequestContext的); 如果(的Request.QueryString [clearcache]!= NULL){
//去掉根部,希望移除所有家属
HttpContext.Cache.Remove(ROOT_KEY);
} 如果(HttpContext.Cache [ROOT_KEY] == NULL)
{
//创建根条目
HttpContext.Cache [ROOT_KEY] =的String.Empty;
} 如果(HttpContext.Cache [Request.Url.AbsolutePath] == NULL){ //添加URL如果尚未添加
HttpContext.Cache.Insert(
Request.Url.AbsolutePath,的String.Empty,
新的CacheDependency(空,新的[] {} ROOT_KEY));
}
}
}
以上code没有工作,的关键是每次创建一个新的CacheDependency 。
在非伪code我试图重复使用相同的对象,这是造成描述的错误。
&@Adeel放大器;雅罗斯拉夫感谢您的答复反正
I would like to have an item in the ASP.NET CacheObject, which if it were changed a number of dependant items would be removed
So.. In a request
- If prompted and it exists in the cache remove the root object, all dependence will be removed too
- Check for root object in the cache, if it doesn't exist, add it
- Add other objects to the cache with a dependency on the root object
When I do this I get an error "An attempt was made to reference a CacheDependency object from more than one Cache entry
"
I see that you can do an AggregateCacheDependency to apply many dependencies to one cached item, but it seems you cannot do it the other way around.
Has any one found a way to do this?
here is some code, its not what I am actually storing but it represents the same task
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}));
}
}
}
The above code does work, the key was creating a new CacheDependency each time. In the non-pseudo code I was attempting to reuse the same object, this was causing the error described.
@Adeel & Jaroslav thanks for the replies anyway
这篇关于CacheDependancy多个缓存的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!