文件和缓存项目依赖

文件和缓存项目依赖

       要创建缓存依赖,你需要创建一个 CacheDependency 对象并在添加依赖的缓存项目时使用它。例如,下面的代码创建一个缓存项目,它在一个 XML 文件被修改、删除、覆盖时自动从缓存中移除:

CacheDependency prodDependency = new CacheDependency(Server.MapPath("ProductList.xml"));
Cache.Insert("ProductInfo", prodInfo, prodDependency);

 CacheDependency 还有一个构造函数,接收一个文件名的数组和一个缓存键值的数组。下面这个示例使用该构造函数创建了一个依赖于缓存中其他项目的项目:

Cache["Key1"] = "Cache Item 1";
 
string[] dependencyKey = new string[1];
dependencyKey[0] = "Key2";
CacheDependency dependency = new CacheDependency(null, dependencyKey);
 
Cache.Insert("Key2", "Cache Item 2", dependency);

       此后,当 Cache["Key1"] 发生变化或从缓存中移除时,Cache["Key2"] 就会被自动移除。

聚合依赖

       有时你可能会希望组合多个依赖创建一个项目,它依赖多个其他资源。例如,它在 3 个文件中的任意一个发生变化时就无效等等。

       使用 AggregateCacheDependency 可以包含任意多个 CacheDependency 对象。你所需要做的只是使用 AggregateCacheDependency .Add()方法提供一个 CacheDependency 对象的数组。

       下面这个示例使一个缓存项目依赖于两个文件:

CacheDependency dep1 = new CacheDependency(Server.MapPath("ProductList1.xml"));
CacheDependency dep2 = new CacheDependency(Server.MapPath("ProductList2.xml"));
 
CacheDependency[] deps = new CacheDependency[] { dep1, dep2 };
AggregateCacheDependency aggregateDep = new AggregateCacheDependency();
aggregateDep.Add(deps);
 
Cache.Insert("ProductInfo", prodInfo, aggregateDep);

 

如何设置HttpRuntime.Cache的有效期

//添加到文件依赖缓存
System.Web.Caching.CacheDependency dep = new System.Web.Caching.CacheDependency(xmlname);
HttpRuntime.Cache.Insert(cachename, data, dep);

//添加绝对过期时间
DateTime absExpireTime = DateTime.Now.AddSeconds(3600);
HttpRuntime.Cache.Insert(cacheName, data, null, absExpireTime, TimeSpan.Zero);

 

个人应用:

    public class AppReturnMsg
    {
        public GloablMsg msg;
        public GloablMsg Msg
        {
            get
            {
                if (msg == null || msg.MsgType <= 0)
                {
                    GlobalMsgList msglist;
                    string fileFullName = AppDomain.CurrentDomain.BaseDirectory + "/App_Data/" + "GlobalMsgConfig.xml";
                    object obj = HttpRuntime.Cache["GlobalMsg"];
                    if (obj == null)
                    {
                        msglist = XmlHelper.XmlToObj<GlobalMsgList>(fileFullName); //从XML获取数据
                        //缓存文件内容
                        HttpRuntime.Cache.Insert("GlobalMsg", msglist, new System.Web.Caching.CacheDependency(fileFullName));
                    }
                    else
                    {
                        msglist = obj as GlobalMsgList;
                    }

                    if (msglist != null)
                    {
                        string path = "";
                        if (HttpContext.Current != null)
                        {
                            path = HttpContext.Current.Request.Path;   //gateway/QueryReCategoryBrands
                        }
                        msg = msglist.MsgItem.FirstOrDefault(t => String.Equals(t.Path, path, StringComparison.CurrentCultureIgnoreCase));
                    }
                }
                return msg;
            }
            set { msg = value; }
        }
    }

    [XmlRoot("GlobalMsg")]
    public class GlobalMsgList
    {
        [XmlElement("MsgItem")]
        public List<GloablMsg> MsgItem { get; set; }

    }

    [DataContract]  
    [Serializable]
    public class GloablMsg
    {
        [IgnoreDataMember]
        [XmlElement("Path")]
        public string Path { get; set; }
         [DataMember]  
        [XmlElement("MsgType")]
        public int MsgType { get; set; }
         [DataMember]  
        [XmlElement("Redirect")]
        public string Redirect { get; set; }
         [DataMember]  
        [XmlElement("Content")]
        public string Content { get; set; }
         [DataMember]  
        [XmlElement("Title")]
        public string Title { get; set; }
    }
<?xml version="1.0" encoding="utf-8" ?>
<GlobalMsg>
    <MsgItem>
        <Path>/json/reply/Test</Path>  
        <MsgType>1</MsgType>
        <!--1头部广告条,2确认弹窗,3提示弹窗,4自动消失,5Picture,6宝贝币,10强制跳转-->
        <Redirect>www.baidu.com</Redirect>
        <Content>测试内容关于广告内容1</Content>
        <Title>测试配置文件第一个</Title>
    </MsgItem>
    <MsgItem>
        <Path>/gateway/QueryReCategoryBrands</Path>
        <MsgType>1</MsgType>
        <Redirect>www.baidu.com</Redirect>
        <Content>测试内容关于广告内容2</Content>
        <Title>测试配置文件第一个</Title>
    </MsgItem>
</GlobalMsg>

 

 

        [System.Web.Mvc.HttpGet]
        public JsonResult GetMenu()
        {
            List<BigMenu> menuList = new List<BigMenu>();
            try
            {
                string filePath = Path.Combine(HttpContext.Server.MapPath("~/"), Menufilename);
                object obj = HttpRuntime.Cache["MenuList"];
                if (obj == null)
                {
                    XmlDocument xmlDoc = new XmlDocument();
                    xmlDoc.Load(filePath);
                    XmlNode NarBarXmlNode = xmlDoc.SelectSingleNode("//NavBar");
                    XmlNodeList xmlNodeList = NarBarXmlNode.ChildNodes;
                    if (xmlNodeList != null && xmlNodeList.Count > 0)
                    {
                        foreach (XmlNode xmlnode in xmlNodeList)
                        {
                            string itemtext = xmlnode.Attributes["Name"].Value; //一级菜单
                            int count = 0;
                            //获取当前结点的子节点
                            //Dictionary<string, string> smallmenu = new Dictionary<string, string>();
                            List<SmallMenu> smMunuList = new List<SmallMenu>();
                            foreach (XmlNode childxmlnode in xmlnode.ChildNodes)
                            {
                                string childitemtext = childxmlnode.Attributes["Name"].Value; //二级菜单页面名称
                                string childitemsrc = childxmlnode.Attributes["Src"].Value; //二级菜单页面跳转路径

                                if (FormUser.UserInfo.ArrRightList.Contains(childitemtext)) //判断是否包含权限
                                {
                                    SmallMenu smMenu = new SmallMenu();
                                    smMenu.SmallMenuName = childitemtext.Replace("PSS", "");
                                    smMenu.MenuSrc = childitemsrc;
                                    smMunuList.Add(smMenu);
                                    count++;
                                }
                            }
                            if (count > 0)
                            {
                                BigMenu bgMenu = new BigMenu();
                                bgMenu.BigMenuName = itemtext;
                                bgMenu.smobj = smMunuList;
                                menuList.Add(bgMenu);
                            }
                        }
                    }
                    //缓存文件内容
                    HttpRuntime.Cache.Insert("MenuList", menuList, new System.Web.Caching.CacheDependency(filePath));
                }
                else
                {
                    menuList = obj as List<BigMenu>;
                }
                return Json(new
                {
                    Status = "OK",
                    Menu = menuList//(new System.Web.Script.Serialization.JavaScriptSerializer()).Serialize(menuList)
                }, JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return Json(new
                {
                    Status = "Fail",
                    Menu = menuList//(new System.Web.Script.Serialization.JavaScriptSerializer()).Serialize(menuList)
                }, JsonRequestBehavior.AllowGet);
            }

        }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值