【问题标题】:How is HttpContext.Current.Cache saving objects in memory?HttpContext.Current.Cache 如何将对象保存在内存中?
【发布时间】:2013-02-22 17:36:47
【问题描述】:

我正在使用HttpContext.Current.Cache 将对象保存到内存中。

我的代码看起来像这样:

public void Add(string key, object data, TimeSpan slidingExpirationTime)
{
    HttpContext.Current.Cache.Insert(key, data, null, System.Web.Caching.Cache.NoAbsoluteExpiration, slidingExpirationTime);
}

public T Get<T>(string key)
{
    T itemStored = (T)HttpContext.Current.Cache.Get(key);
    if (itemStored == null)
        itemStored = default(T);

    return itemStored;
}

这工作非常快!

我很好奇它是如何将对象保存到内存中的。

是保存指针值,还是对对象进行哈希处理,然后将其保存到内存中,当我请求它时,它会反序列化它?

【问题讨论】:

    标签: asp.net asp.net-caching httpcontext.cache


    【解决方案1】:

    数据,是一种object,从插入缓存键的内部函数中,我们看到很简单,保持对object的引用

    internal CacheEntry(string key, object value, CacheDependency dependency, CacheItemRemovedCallback onRemovedHandler, DateTime utcAbsoluteExpiration, TimeSpan slidingExpiration, CacheItemPriority priority, bool isPublic) : base(key, isPublic)
    {
        if (value == null)
        {
            throw new ArgumentNullException("value");
        }
        .... code ....
        this._value = value;
        .... code ....
    }
    

    【讨论】:

    • 这就是为什么它这么快?它不对对象做任何处理?
    猜你喜欢
    • 2011-02-22
    • 1970-01-01
    • 2016-11-09
    • 2015-03-24
    • 2016-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多