添加缓存

1.添加命名空间 using System.Web;

2.     public string GetLeaderInfo(string symbol)
        {
            //获取高管信息的路径         
            XElement ele = XElement.Load("path.xml");
            string path = ele.Element("leader").Value;

            //首次读取缓存
            string jsoncache = HttpRuntime.Cache.Get(symbol) as string;
            if (jsoncache == null || jsoncache.Equals(""))
            {
                //读取数据
                XmlDocument doc = new XmlDocument();
                doc.Load(path);

                string strSearch = "root/高管信息[@代码='" + symbol + "']";

                XmlNode searchNode = doc.SelectSingleNode(strSearch);
                XmlElement element = (XmlElement)searchNode;
                xmltoJson json = new xmltoJson();
                jsoncache = json.XmlToJSON(element);
                //加缓存
                HttpRuntime.Cache.Insert(symbol, jsoncache, null, DateTime.UtcNow.AddMinutes(2), System.Web.Caching.Cache.NoSlidingExpiration);
            }
            return jsoncache;
        }

posted @ 2011-07-20 11:16  xuebingz  阅读(295)  评论(1编辑  收藏  举报