利用缓存

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Caching;

namespace SpaceTime
{
    /// <summary>
    /// VsCache 的摘要说明
    /// </summary>
    public class STCache
    {
        public STCache()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }


        private static void onRemove(string strIdentify, object userInfo, CacheItemRemovedReason reason)
        {

        }
        //建立回调委托的一个实例
        CacheItemRemovedCallback callBack = new CacheItemRemovedCallback(onRemove);


        /// <summary>
        /// 创建缓存对象(默认24小时缓存)
        /// </summary>
        /// <param name="name">键名</param>
        /// <param name="value">键值</param>
        public static void CreateCache(string name, object value)
        {
            CreateCache(name, 24, value);
        }

        /// <summary>
        /// 创建缓存对象(指定缓存小时)
        /// </summary>
        /// <param name="name">键名</param>
        /// <param name="hours">缓存时间(小时)</param>
        /// <param name="value">键值</param>
        public static void CreateCache(string name,double hours, object value)
        {
            //建立回调委托的一个实例
            CacheItemRemovedCallback callBack = new CacheItemRemovedCallback(onRemove);

            //以Identify为标志,将userInfo存入Cache
            HttpContext.Current.Cache.Insert(name, value, null,
                 System.DateTime.Now.AddHours(hours),//当前指定为24小时,7*24=168
                 System.Web.Caching.Cache.NoSlidingExpiration,
                 System.Web.Caching.CacheItemPriority.Default,
                 callBack);
        }

        /// <summary>
        /// 读取缓存对象结果
        /// </summary>
        /// <param name="name">键名</param>
        /// <returns></returns>
        public static object GetCache(string name)
        {
            if (ExistCache(name))
            {
                return HttpContext.Current.Cache[name];
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 判断存储的"一键一值"值是否还存在(有没有过期失效或从来都未存储过)
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static bool ExistCache(string name)
        {
            if (HttpContext.Current.Cache[name] != null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除缓存数据
        /// </summary>
        /// <param name="name"></param>
        public static void Remove(string name)
        {
            try
            {
                HttpContext.Current.Cache.Remove(name);                
            }
            catch (Exception exp)
            { 
                //
            }

        }
    }
}

private void BindItemCats()
    {
        try
        {
            int pageIndex = DNTRequest.GetInt("PageNo", 1);
            List<TaobaokeItem> items = new List<TaobaokeItem>();//定义返回商品泛型对象
            string totalPageNum = "0";
            if (!SpaceTime.STCache.ExistCache("ItemList-" + cid + "_" + pageIndex))
            {
                TopXmlRestClient client = new TopXmlRestClient("http://gw.api.taobao.com/router/rest", "12150539", "f1e413524f3bec524c7677aba2ab43f3");
                DynamicTopRequest req = new DynamicTopRequest("taobao.taobaoke.items.get");
                req.AddTextParameter("fields", "iid,num_iid,title,nick,pic_url,price,click_url,commission,commission_rate,commission_num,commission_volume,seller_credit_score,item_location,volume,shop_click_url,total_results");
                req.AddTextParameter("nick", "andyli783");
                if (cid != 0)
                {
                    req.AddTextParameter("cid", cid);
                }
                req.AddTextParameter("keyword", DNTRequest.GetString("key"));
                req.AddTextParameter("page_no", pageIndex);
                req.AddTextParameter("page_size", 18);

                req.AddTextParameter("start_price", DNTRequest.GetString("sc"));
                req.AddTextParameter("end_price", DNTRequest.GetString("ec"));
                req.AddTextParameter("area", DNTRequest.GetString("ac"));
                req.AddTextParameter("sort", DNTRequest.GetString("sort"));

                string rsp = client.GetResponse(req);
                Parser parser = new Parser();//定义解析XML对象
                parser.XmlToObject2<TaobaokeItem>(rsp, "taobaoke_items_get", "taobaoke_items/taobaoke_item", items);
                SpaceTime.STCache.CreateCache("ItemList-" + cid + "_" + pageIndex, 24, items);
                XmlDocument xml = new XmlDocument();
                xml.LoadXml(rsp);
                XmlElement root = xml.DocumentElement;
                XmlNode xn = root.SelectSingleNode("//total_results");
                if (xn != null)
                {
                    SpaceTime.STCache.CreateCache("ItemListPage-" + cid + "_" + pageIndex, 24, xn.InnerText);
                    totalPageNum = xn.InnerText;
                }
            }
            else
            {
                items = SpaceTime.STCache.GetCache("ItemList-" + cid + "_" + pageIndex) as List<TaobaokeItem>;
                totalPageNum = SpaceTime.STCache.GetCache("ItemListPage-" + cid + "_" + pageIndex).ToString();
            }
            hrrPage.RecordCount = int.Parse(totalPageNum);
            hrrPage.LinkPageUrl = Request.Url.ToString().Trim();
            dlItemList.DataSource = items;//&sc={2}&ec={3}
            dlItemList.DataBind();            
            if (items.Count < 1)
            {
                wushuju = "<div class=\"tijiao\">很抱歉,没有找到与“<strong><font color=\"#FF0000\">" + DNTRequest.GetString("key") + "</font></strong>”相关的宝贝</div>";
                hrrPage.Visible = false;
            }
            else
                wushuju = "";
        }
        catch (Exception) 
        {
            Utils.ShowAlert("类型和关键字必须有一个", Page);
        }        
    }

 

posted @ 2012-11-13 17:42  一千零一夜  阅读(242)  评论(0编辑  收藏  举报