zoukankan      html  css  js  c++  java
  • asp.net 自动检测缓存内容是否变化

    1 使用cache.Insert方法时,新建一个System.Web.Caching.CacheDependency对象,告诉缓存,当缓存的内容发生变化时,将删除缓存,并重新缓存

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.IO;
    
    namespace Cachting
    {
        public class CityListInfo
        {
            public string Timestamp { get; set; }
            public string Html { get; set; }
        }
    
        public partial class CitiesControl : System.Web.UI.UserControl
        {
            private static readonly string fileName = "/CitiesList.html";
            private static readonly string CACHE_KEY = "cities_html";
            private CityListInfo cityInfo;
            private bool cached = false;
            protected void Page_Load(object sender, EventArgs e)
            {
                cityInfo = Cache[CACHE_KEY] as CityListInfo;
                if (cityInfo==null)
                {
                    cityInfo = new CityListInfo { Timestamp=DateTime.Now.ToLongTimeString(),
                        Html =File.ReadAllText(MapPath(fileName))};
                    //只要未修改文件就应该缓存数据
                    Cache.Insert(CACHE_KEY, cityInfo,new System.Web.Caching.CacheDependency(MapPath(fileName)));
                }
                else
                {
                    cached = true;
                }
            }
    
            public string GetCities()
            {
                //return File.ReadAllText(MapPath(fileName));
                return cityInfo.Html;
    
    
            }
    
            public string GetTimeStamp()
            {
                // return DateTime.Now.ToLongTimeString();
                return cityInfo.Timestamp + (cached ? "<b>Cached</b>" : "");
            }
        }
    }
  • 相关阅读:
    hdu 2112 (最短路+map)
    poj 1502 最短路+坑爹题意
    poj 1696 Space Ant (极角排序)
    poj 1410 线段相交判断
    使用本地光盘安装Microsoft .NET Framework 3.5 for Win8.1/WinServer2012R2
    Excel REPT函数使用
    tomcat7配置虚拟目录
    Tomcat 7.0的配置
    js去除空格
    JAVABEAN连接各数据库
  • 原文地址:https://www.cnblogs.com/mibing/p/7791777.html
Copyright © 2011-2022 走看看