环境:SQL 2005,MS NET 2008共2个页面
product.aspx  和 ajax_getdata.aspx 页面。
我点击product.aspx?id=XX页面的1个按钮从    ajax_getdata.aspx?id=XX &time='年-月-日' 来获取数据,并返回给product.aspx在ajax_getdata.aspx 页面时通过  dataset 来获取数据的,我想将 dataset在服务器和客户端缓存下。假如现在有1个用户在之前请求了  数据 id=1&time='年-月-日'。
(id和time确定的话,从数据库读的数据是固定的不会变的)
那么怎么在 服务器如何将这个  id=1&time='年-月-日' 获取的dataset 放在服务器端呢。  只要有用户再次请求数据的话,就根据 id=1&time 来判断 是否 服务器缓存中是否有数据。如果有就直接读取。

解决方案 »

  1.   

    大致是:   DataSet ds=(DataSet)Cache[Request.Url.Query];
       if(ds==null)
       {
           ds=读取数据库查询结果返回ds;
           Cache.Insert(Request.Url.Query,ds);    //实际上将来,你还应该写下Insert的后三个参数
        }
      

  2.   

    缓存依赖
    public static object GetCache(string CacheKey)
    {
        System.Web.Caching.Cache objCache = HttpRuntime.Cache;
        return objCache[CacheKey];
    }
    public static void SetCache(string CacheKey, object objObject, System.Web.Caching.CacheDependency dep)
    {
        System.Web.Caching.Cache objCache = HttpRuntime.Cache;
        objCache.Insert(
            CacheKey,
            objObject,
            dep,
            System.Web.Caching.Cache.NoAbsoluteExpiration,//从不过期
            System.Web.Caching.Cache.NoSlidingExpiration,
            System.Web.Caching.CacheItemPriority.Default,
            null);
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        string CacheKey = "cachetest";
        object objModel = GetCache(CacheKey);
        if (objModel == null)
        {
           objModel = GetData();
            if (objModel != null)
            {
                System.Web.Caching.SqlCacheDependency dep = new System.Web.Caching.SqlCacheDependency("", "");
                SetCache(CacheKey, objModel, dep);/        }
        }    DataSet ds= (DataSet)objModel;
    }
      

  3.   

    总结,本看到cache的参数,就头痛。其实仔细看msdn,也能看懂,关键是耐心。