using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Caching;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace XXX.WebApp
{
public partial class FileCacheDep : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string filePath = Request.MapPath("File.txt");
if (Cache["fileContent"] == null)
{
//文件缓存依赖。CacheDependency类会不断轮循扫描File.txt文件,如果文件发生改变,就会清空对应的Cache。
CacheDependency cDep = new CacheDependency(filePath);
string fileContent = File.ReadAllText(filePath);
Cache.Insert("fileContent", fileContent, cDep); //将文件依赖项绑定到Cache中。
Response.Write("数据来自文件");
}
else
{
Response.Write("数据来自缓存:"+Cache["fileContent"].ToString());
}
}
}
}