首页 > Web开发 > 详细

asp.net 缓存依赖demo

时间:2020-06-04 20:55:14      阅读:37      评论:0      收藏:0      [点我收藏+]

 

City.xml

<?xml version="1.0" encoding="utf-8" ?>
<Citys>
  <city>
    <code>A01</code>
    <name>北京</name>
  </city>
  <city>
    <code>A02</code>
    <name>上海</name>
  </city>
  <city>
    <code>A03</code>
    <name>广州</name>
  </city>
  <city>
    <code>A04</code>
    <name>深圳3</name>
  </city>
</Citys>

 

缓存帮助类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace CacheDependency
{
    public class MeCache
    {
        /// <summary>
        /// 设置以缓存依赖的方式缓存数据
        /// </summary>
        /// <param name="cacheKey">索引键值</param>
        /// <param name="objObject">缓存对象</param>
        /// <param name="dep"></param>
        public static void SetCache(string cacheKey, object objObject, System.Web.Caching.CacheDependency caDep)
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            objCache.Insert(
                cacheKey,
                objObject,
                caDep,
                System.Web.Caching.Cache.NoAbsoluteExpiration, //从不过期
                System.Web.Caching.Cache.NoSlidingExpiration, //禁用可调过期
                System.Web.Caching.CacheItemPriority.Default,
                null);
        }



        /// <summary>
        /// 获取当前应用程序指定CacheKey的Cache对象值
        /// </summary>
        /// <param name="cacheKey">索引键值</param>
        /// <returns>返回缓存对象</returns>
        public static object GetCache(string cacheKey)
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            return objCache[cacheKey];
        }

    }
}

 

城市数据读取类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;

namespace CacheDependency
{
    public class CityData
    {
        static CityData()
        {
            InitCity();
        }

        private static List<CityModel> InitCity()
        {
            List<CityModel> listCity = ReadCityXml();
            string path = System.AppDomain.CurrentDomain.BaseDirectory + "" + "City.xml";
            System.Web.Caching.CacheDependency dep = new System.Web.Caching.CacheDependency(path);
            MeCache.SetCache("city", listCity, dep);

            return listCity;
        }


        public static List<CityModel> GetCity()
        {
            object obj = MeCache.GetCache("city");
            if (obj == null)
            {
                return InitCity();

            }
            else
            {
                return (List<CityModel>)obj;
            }

        }




        public static List<CityModel> ReadCityXml()
        {
            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            string path = System.AppDomain.CurrentDomain.BaseDirectory + "" + "City.xml";
            doc.Load(path);

            System.Xml.XmlNodeList list = doc.SelectNodes("//city");

            List<CityModel> listCity = new List<CityModel>();
            foreach (XmlNode item in list)
            {
                XmlNode t = item.SelectSingleNode("code");
                XmlNode t2 = item.SelectSingleNode("name");

                CityModel model = new CityModel();
                model.Code = t.InnerText;
                model.Name = t2.InnerText;

                listCity.Add(model);
            }
            return listCity;
        }
    }

    public class CityModel
    {
        public string Code { get; set; }

        public string Name { get; set; }
    }
}

 

测试代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CacheDependency
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           
            List<CityModel> list = CityData.GetCity();
            if (list != null)
            {
                foreach (var item in list)
                {
                    Response.Write(item.Code + "----" + item.Name);
                    Response.Write("<br/>");
                }
            }

            Response.Write("<br/>");
            Response.Write("ok!");
        }
    }
}

 

 

以上代码是一个完整的缓存依赖数据示例。

不过少了一个地方,缓存失效后,重新读取,并加入缓存的地方,没有加lock可能存在并发问题;

 

 

 

 

 

 

asp.net 缓存依赖demo

原文:https://www.cnblogs.com/Tpf386/p/13045560.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!