上午发帖问了个 asp.net数据缓存问题后(http://topic.csdn.net/u/20090316/09/fdffefc8-2291-43ea-9b9f-d6b893cac846.html)  简单明白了 数据缓存。  但是遇到一个问题。 怎样在数据访问层运用 缓存 比如说 在Global.asax 中的 Application_Start 中  首先查询数据 然后把数据放入 cache中。 但是只能用 Cache ch=new Cache(); 然后给 cache 赋值。 不能象 页面那样 把 Cache 当属性来用。  那么怎样在 别的类中取得 ch的值?

解决方案 »

  1.   

    if (Cache["ds"] != null)
                    {
                        Cache.Remove("ds");
                    }
                    dv = (DataView)Cache["ds"];
                    if (dv == null)
                    {
                        dv = dt.DefaultView;
                        Cache.Insert("ds", dv, null, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(3));
                    }
      

  2.   

    可以通过使用System.Web.Caching来访问Cache对象。
    通过使用System.Web中HttpContext类的Cache属性或者Page对象的Cache属性来设置与Cache对象关联的属性和方法。
      

  3.   

    用System.Web.HttpRuntime来访问Cache,
    如System.Web.HttpRuntime.Cache.Insert("ds", dv, null, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(3)); 
      

  4.   

    /// <summary>
        /// 缓存操作类
        /// </summary>
        public class CacheHelper
        {
            /// <summary>
            /// 获取当前应用程序指定CacheKey的Cache值
            /// </summary>
            /// <param name="CacheKey"></param>
            /// <returns></returns>
            public static object GetCache(string CacheKey)
            {
                Cache objCache = HttpRuntime.Cache;
                return objCache[CacheKey];
            }        /// <summary>
            /// 设置当前应用程序指定CacheKey的Cache值
            /// </summary>
            /// <param name="CacheKey"></param>
            /// <param name="objObject"></param>
            public static void SetCache(string CacheKey, object objObject)
            {
                Cache objCache = HttpRuntime.Cache;
                objCache.Insert(CacheKey, objObject);
            }        /// <summary>
            /// 设置当前应用程序指定CacheKey的Cache值
            /// </summary>
            /// <param name="CacheKey"></param>
            /// <param name="objObject"></param>
            /// <param name="absoluteExpiration"></param>
            /// <param name="slidingExpiration"></param>
            public static void SetCache(string CacheKey, object objObject, DateTime absoluteExpiration,
                                        TimeSpan slidingExpiration)
            {
                Cache objCache = HttpRuntime.Cache;
                objCache.Insert(CacheKey, objObject, null, absoluteExpiration, slidingExpiration);
            }