分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 教程案例

asp.net 自动检测缓存内容是否变化

发布时间:2023-09-06 01:23责任编辑:顾先生关键词:缓存

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>" : ""); ???????} ???}}

asp.net 自动检测缓存内容是否变化

原文地址:http://www.cnblogs.com/mibing/p/7791777.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved