zoukankan      html  css  js  c++  java
  • asp.net 缓存依赖demo

     示例简述:

    项目中使用的城市数据、分类数据、站点数据、项目数据等少量且不怎么会变动的数据,不愿意存在数据库。

    这个时候可以考虑放在一个文件中,xml或者txt都可以,然后将数据缓存到内存,加上缓存依赖,数据文件修改

    后缓存自动失效,程序访问时,可以重新访问文件加载最新数据,并再次保存到内存中。

    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可能存在并发问题;

     加限制参考单例双重验证模式。

  • 相关阅读:
    leetcode每日一题(2020-06-24):16. 最接近的三数之和
    leetcode每日一题(2020-06-23):67. 二进制求和
    leetcode每日一题(2020-06-19):125. 验证回文串
    leetcode每日一题(2020-06-18):1028. 从先序遍历还原二叉树
    leetcode每日一题(2020-06-17):1014. 最佳观光组合
    leetcode每日一题(2020-06-16):297. 二叉树的序列化与反序列化
    leetcode每日一题(2020-06-14):1300. 转变数组后最接近目标值的数组和
    leetcode每日一题(2020-06-13):70.爬楼梯
    leetcode每日一题(2020-06-12):15. 三数之和
    springboot 项目中_ 利用Filter去解决跨域问题
  • 原文地址:https://www.cnblogs.com/Tpf386/p/13045560.html
Copyright © 2011-2022 走看看