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="13"></row>
    <row Name="V1" Value="11"></row>
    <row Name="V2" Value="12"></row>
  </Settings>
</root>

設計思路是:將XML文件讀取,儲存到Dictionary<string,string>中,並同時將這個Dictionary儲存到緩存並依賴XML文件,當XML文件發生變化是,緩存會自動更新改變的值

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM