首页 > 代码库 > C#简单的文件依赖缓存的使用

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

一,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=http://www.mamicode.com/"13"></row>
    <row Name="V1" Value=http://www.mamicode.com/"11"></row>
    <row Name="V2" Value=http://www.mamicode.com/"12"></row>
  </Settings>
</root>

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

 

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