Cache.Insert(“Key”,“Value”)和Cache [“Key”] =“Value”之间有什么区别吗?

时间:2013-09-18 01:47:39

标签: c# asp.net caching

我知道Cache.InsertCache.Add之间的区别,但是Cache["Key"] = "Value"呢?

2 个答案:

答案 0 :(得分:3)

根据Cache.Item属性的文档there is no difference

  

说明:

     

您可以使用此属性检索指定缓存项的值,或者将项和密钥添加到缓存中。 使用Item属性添加缓存项等同于调用Cache.Insert方法。

(重点是我的)。

答案 1 :(得分:0)

这是一个编写一个非常简单的包装器(响应注释Is there any difference between Cache.Insert("Key", "Value") and Cache["Key"] = "Value"?)的示例,用于在使用索引方法向缓存添加项目时设置默认值。这是非常基本的。

public class CacheHandler
{
    /// <summary>
    /// static cache dependencies
    /// </summary>
    readonly static CacheDependency dependecies = null;
    /// <summary>
    /// sliding expiration
    /// </summary>
    readonly static TimeSpan slidingExpiration = TimeSpan.FromMinutes(5);

    /// <summary>
    /// absolute expiration
    /// </summary>
    readonly static DateTime absoluteExpiration = System.Web.Caching.Cache.NoAbsoluteExpiration;

    /// <summary>
    /// private singleton 
    /// </summary>
    static CacheHandler handler;

    /// <summary>
    /// gets the current cache handler
    /// </summary>
    public static CacheHandler Current { get { return handler ?? (handler = new CacheHandler()); } }

    /// <summary>
    /// private constructor
    /// </summary>
    private CacheHandler() { }

    /// <summary>
    /// Gets \ Sets objects from the cache. Setting the object will use the default settings above
    /// </summary>
    /// <param name="key">the cache key</param>
    /// <returns>the object stored in the cache</returns>
    public object this[string key]
    {
        get
        {
            if (HttpContext.Current == null)
                throw new Exception("The current HTTP context is unavailable. Unable to read cached objects.");
            return HttpContext.Current.Cache[key];
        }
        set
        {
            if (HttpContext.Current == null)
                throw new Exception("The current HTTP context is unavailable. Unable to set the cache object.");
            HttpContext.Current.Cache.Insert(key, value, dependecies, absoluteExpiration , slidingExpiration);
        }
    }

    /// <summary>
    /// the current HTTP context
    /// </summary>
    public Cache Context
    {
        get
        {
            if (HttpContext.Current == null)
                throw new Exception("The current HTTP context is unavailable. Unable to retrive the cache context.");
            return HttpContext.Current.Cache;
        }
    }
}

同样,这是超级简单和基本的,但需要调用另一个服务来插入类似的缓存。

protected void Page_Load(object sender, EventArgs e)
{
    CacheHandler.Current["abc"] = "123";
}

如果您刚刚启动应用程序,则可以使用新的缓存处理程序替换ASP.Net页面的Cache属性。

public partial class BasePage : Page
{
    protected new CacheHandler Cache
    {
        get { return CacheHandler.Current; }
    }
}

然后您的所有页面都可以更改为以下内容。

public partial class _Default : **BasePage**
{
}

调用缓存处理程序非常简单

protected void Page_Load(object sender, EventArgs e)
{
    Cache["abc"] = "123";
}

这将是您的缓存处理程序,而不是默认的Cache对象。

这些只是选项,非常适合您希望如何在应用程序中处理缓存,以及这是否值得付出努力。

干杯。

相关问题