ASP.NET cache缓存的用法

本文导读:在.NET运用中经常用到缓存(Cache)对象。有HttpContext.Current.Cache以及HttpRuntime.Cache,HttpRuntime.Cache是应用程序级别的,而HttpContext.Current.Cache是针对当前WEB上下文定义的。HttpRuntime下的除了WEB中可以使用外,非WEB程序也可以使用。

1、HttpRuntime.Cache 相当于就是一个缓存具体实现类,这个类虽然被放在了 System.Web 命名空间下了。但是非 Web 应用也是可以拿来用的。

2、HttpContext.Cache 是对上述缓存类的封装,由于封装到了 HttpContext ,局限于只能在知道 HttpContext 下使用,即只能用于 Web 应用。

综上所属,在可以的条件,尽量用 HttpRuntime.Cache ,而不是用 HttpContext.Cache 。

有以下几条缓存数据的规则。

第一,数据可能会被频繁的被使用,这种数据可以缓存。

第二,数据的访问频率非常高,或者一个数据的访问频率不高,但是它的生存周期很长,这样的数据最好也缓存起来。

第三是一个常常被忽略的问题,有时候我们缓存了太多数据,通常在一台X86的机子上,如果你要缓存的数据超过800M的话,就会出现内存溢出的错误。所以说缓存是有限的。换名话说,你应该估计缓存集的大小,把缓存集的大小限制在10以内,否则它可能会出问题。在Asp.net中,如果缓存过大的话也会报内存溢出错误,特别是如果缓存大的DataSet对象的时候。

你应该认真分析你的程序。根据实际情况来看哪里该用,哪里不该用。如:cache用得过多也会增大服务器的压力。整页输出缓存,又会影响数据的更新。 如果真的需要缓存很大量的数据,可以考虑静态技术。

下面介绍HttpRuntime.Cache常用方法:

/// <summary>
/// 得到一个对象实体
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public DataTable GetModel(int id)
{
    DataTable table = (DataTable)HttpRuntime.Cache["getModel"];//从缓存中获取getModel的数据
    if (table == null)  //如果缓存中没有找到
    {
        table = SQLHelper.ExecuteReader("select top 1 Id,料件编号,品名,规格,数量 from Material where ,
        new SqlParameter("@Id", id));
        //用HttpRuntime.Cache.Insert插入缓存
        //HttpRuntime.Cache.Insert("getModel", table, null, DateTime.Now.AddMinutes(1), TimeSpan.Zero);
        CacheInsert("getModel", table, DateTime.Now.AddHours(1));
    }
    return table;
}

/// <summary>
/// 插入缓存
/// </summary>
/// <param name="key">设置缓存的key值</param>
/// <param name="value">设置缓存的value值</param>
/// <param name="absoluteExpiration">缓存过期时间</param>
public void CacheInsert(string key, object value, DateTime absoluteExpiration)
{
    HttpRuntime.Cache.Insert(key,value,null,absoluteExpiration,TimeSpan.Zero);
}
using System;
using System.Web;
using System.Collections;

public class CookiesHelper
{
    /// <summary>
    /// 获取数据缓存
    /// </summary>
    /// <param name="CacheKey">键</param>
    public static object GetCache(string CacheKey)
    {
        System.Web.Caching.Cache objCache = HttpRuntime.Cache;
        return objCache[CacheKey];
    }

    /// <summary>
    /// 设置数据缓存
    /// </summary>
    public static void SetCache(string CacheKey, object objObject)
    {
        System.Web.Caching.Cache objCache = HttpRuntime.Cache;
        objCache.Insert(CacheKey, objObject);
    }

    /// <summary>
    /// 设置数据缓存
    /// </summary>
    public static void SetCache(string CacheKey, object objObject, TimeSpan Timeout)
    {
        System.Web.Caching.Cache objCache = HttpRuntime.Cache;
        objCache.Insert(CacheKey, objObject, null, DateTime.MaxValue, Timeout, System.Web.Caching.CacheItemPriority.NotRemovable, null);
    }

    /// <summary>
    /// 设置数据缓存
    /// </summary>
    public static void SetCache(string CacheKey, object objObject, DateTime absoluteExpiration, TimeSpan slidingExpiration)
    {
        System.Web.Caching.Cache objCache = HttpRuntime.Cache;
        objCache.Insert(CacheKey, objObject, null, absoluteExpiration, slidingExpiration);
    }

    /// <summary>
    /// 移除指定数据缓存
    /// </summary>
    public static void RemoveAllCache(string CacheKey)
    {
        System.Web.Caching.Cache _cache = HttpRuntime.Cache;
        _cache.Remove(CacheKey);
    }

    /// <summary>
    /// 移除全部缓存
    /// </summary>
    public static void RemoveAllCache()
    {
        System.Web.Caching.Cache _cache = HttpRuntime.Cache;
        IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
        while (CacheEnum.MoveNext())
        {
            _cache.Remove(CacheEnum.Key.ToString());
        }
    }
}