问题描述
我检查了在ASP.NET 4.0中的OutputCacheProvider,并用它来存储我的输出缓存到MongoDB的数据库。我无法理解Add方法这是覆盖方法OutputCacheProvider之一的目的。 Add方法被调用时,你必须的VaryByParam设置的东西。所以,如果我的VaryByParam =ID,那么Add方法将被调用。
I am checking out the OutputCacheProvider in ASP.NET 4.0 and using it to store my output cache into the MongoDb database. I am not able to understand the purpose of Add method which is one of the override methods for OutputCacheProvider. The Add method is invoked when you have VaryByParam set to something. So, if I have VaryByParam = "id" then the Add method will be invoked.
但添加后的设定也会被调用,我可以插入到设置方法里面的MongoDB数据库。
But after the Add the Set is also invoked and I can insert into the MongoDb database inside the Set method.
public override void Set(string key, object entry, DateTime utcExpiry)
{
// if there is something in the query use the path and query to generate the key
var url = HttpContext.Current.Request.Url;
if (!String.IsNullOrEmpty(url.Query))
{
key = url.PathAndQuery;
}
Debug.WriteLine("Set(" + key + "," + entry + "," + utcExpiry + ")");
_service.Set(
new CacheItem() { Key = MD5(key), Item = entry, Expires = utcExpiry }
);
}
里面的设置方法,我用的是PathAndQuery来获取查询字符串的PARAMS,然后做一个MD5的密钥,并将其保存到MongoDB的数据库。
Inside the Set method I use the PathAndQuery to get the params of the QueryString and then do a MD5 on the key and save it into the MongoDb database.
这似乎是Add方法将是有用的,如果我在做类似的VaryByParam =自定义什么的。
It seems like the Add method will be useful if I am doing something like VaryByParam = "custom" or something.
任何人都可以提供一些线索上OutputCacheProvider的Add方法?
Can anyone shed some light on the Add method of OutputCacheProvider?
推荐答案
它们是相似的,但有一个细微的差别。纵观MSDN文档的 OutputCacheProvider 类
They are similar, but there is a slight difference. Looking at the MSDN documentation for the OutputCacheProvider class
- 设置 - 插入指定进入输出缓存,覆盖入口,如果它是已经缓存
- 添加 - 插入指定进入输出缓存。
- Set - "Inserts the specifiedentry into the output cache,overwriting the entry if it isalready cached"
- Add - "Insertsthe specified entry into the outputcache."
备注为添加继续说。
如果在已经值 缓存指定的键, 供应商必须返回值。该 提供者必须不存储数据 使用Add方法传递 参数。 Add方法存储 如果它不是已在数据 缓存。如果数据在高速缓存中, Add方法返回其
因此,对于新的价值观是不是已经在缓存中,他们的行为相同,但其中的值已经存在,设置对其进行更新,而添加保留原始值不变。
So for new values that aren't already in the cache they will behave identically, but where the value already exists, Set updates it, whereas Add leaves the original value intact.
这篇关于.NET 4.0实现OutputCacheProvider的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!