码迷,mamicode.com
首页 > Windows程序 > 详细

C#简单的文件依赖缓存的使用

时间:2017-06-17 11:15:26      阅读:358      评论:0      收藏:0      [点我收藏+]

标签:send   void   type   body   eric   key   model   attribute   toe   

一,FileCache.aspx页面

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="FileCache.aspx.cs" Inherits="WebApplication1.FileCache" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Label ID="Label1" runat="server" />
        </div>
    </form>
</body>
</html>

二,FileCache.aspx.cs

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

namespace WebApplication1
{
    public partial class FileCache : System.Web.UI.Page
    {
        /// <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];
        }

        /// <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);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();   
            string CacheKey = "CacheKey";
            Dictionary<string, string> objModel = GetCache(CacheKey) as Dictionary<string, string>;//从缓存中获取
            if (objModel == null) //缓存里没有
            {
                string path = Server.MapPath("SX.xml");    //读取服务器路径下的XML文件,生成Dictionary储存到缓存,并依赖这个文件
                XElement root = XElement.Load(path);

                foreach (var item in root.Elements("Settings").Elements("row"))
                {
                    var xAttribute = item.Attribute("Name");
                    if (xAttribute != null && !dic.ContainsKey(xAttribute.Value))
                    {
                        dic.Add(xAttribute.Value, item.Attribute("Value").Value);
                    }
                }
                System.Web.Caching.CacheDependency dep = new System.Web.Caching.CacheDependency(path);
                SetCache(CacheKey, dic, dep);//写入缓存,并依赖SX.xml文件

            }
            objModel = GetCache(CacheKey) as Dictionary<string, string>;
            if (objModel != null) Label1.Text = objModel["V"];      //当更改服务器的SX.xml的Name=V的value时,缓存会自动更新值
        }
    }
}

三,服务器下的SX.xml

<?xml version="1.0" encoding="utf-8"?>
<root>
  <Settings>
    <row Name="V" Value="13"></row>
    <row Name="V1" Value="11"></row>
    <row Name="V2" Value="12"></row>
  </Settings>
</root>

设计思路是:将XML文件读取,储存到Dictionary<string,string>中,并同时将这个Dictionary储存到缓存并依赖XML文件,当XML文件发生变化是,缓存会自动更新改变的值

 

C#简单的文件依赖缓存的使用

标签:send   void   type   body   eric   key   model   attribute   toe   

原文地址:http://www.cnblogs.com/May-day/p/7039554.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!