ASP.NET Cache缓存管理基于文件的依赖
public class CacheCD
{
public static string GetMessageByCache//使用方法和属性都可以被HtmlEncode()传回去
{
//HttpContext context = HttpContext.Current;
get
{
//string message = context.Cache[ "Message" ] as string;
string message = HttpRuntime.Cache[ "Message" ] as string;
if ( message == null )
{
string path = HttpContext.Current.Server.MapPath( "~/App_Data/TextFile.txt" );
message = System.IO.File.ReadAllText( path );//
//为了以后访问,将信息保存在缓存中,设置相对过期的时间为 1 小时
//context.Cache.Add(
HttpRuntime.Cache.Add(
"Message",
message,
new System.Web.Caching.CacheDependency( path ),
System.Web.Caching.Cache.NoAbsoluteExpiration,
new TimeSpan( 1, 0, 0 ),
System.Web.Caching.CacheItemPriority.Normal,
//System.Web.Caching.CacheItemPriority.Default,//ok
//new System.Web.Caching.CacheItemRemovedCallback( Callback ) //ok
Callback );
}
return message;
}
}
public static void Callback( string key, object value, System.Web.Caching.CacheItemRemovedReason reason )
{
}
}