加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – .NET 4.0实现OutputCacheProvider

发布时间:2020-12-16 07:15:01 所属栏目:asp.Net 来源:网络整理
导读:我正在检查ASP.NET 4.0中的OutputCacheProvider并使用它将我的输出缓存存储到MongoDb数据库中.我无法理解Add方法的目的,它是OutputCacheProvider的覆盖方法之一.将VaryByParam设置为某个值时,将调用Add方法.所以,如果我有VaryByParam =“id”,那么将调用Add
我正在检查ASP.NET 4.0中的OutputCacheProvider并使用它将我的输出缓存存储到MongoDb数据库中.我无法理解Add方法的目的,它是OutputCacheProvider的覆盖方法之一.将VaryByParam设置为某个值时,将调用Add方法.所以,如果我有VaryByParam =“id”,那么将调用Add方法.

但是在调用Add the Set之后我也可以在Set方法中插入MongoDb数据库.

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 }
    ); 
}

在Set方法中,我使用PathAndQuery获取QueryString的参数,然后在密钥上执行MD5并将其保存到MongoDb数据库中.

如果我正在做类似VaryByParam =“custom”之类的东西,似乎Add方法会很有用.

任何人都可以对OutputCacheProvider的Add方法有所了解吗?

解决方法

他们是相似的,但有一点点差异.查看 OutputCacheProvider类的MSDN文档

> Set – “插入指定的
进入输出缓存,
如果是,则覆盖该条目
已缓存“
> Add – “插入
指定的输入输入
缓存“.

“添加”的评论继续说

“If there is already a value in the
cache for the specified key,the
provider must return that value. The
provider must not store the data
passed by using the Add method
parameters. The Add method stores the
data if it is not already in the
cache. If the data is in the cache,
the Add method returns it”

因此,对于尚未存在于缓存中的新值,它们的行为相同,但是当值已经存在时,Set会更新它,而Add会保留原始值.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读