|
代码片段:
-----------------------------------------------------------------------
[code=csharp]if (JCache.DataApplica.Get("Config") == null)
{
// 建立缓存依赖项,如果文件发生变化,删除缓存
string _Ctxt = @" est.txt";
CacheDependency dep = new CacheDependency(HttpContext.Current.Server.MapPath(_Ctxt));
//缓存
Cache.Insert("Config", DateTime.Now.ToString(), dep);
}
[/code]---------------------------------------------------------------------------
//通过改变缓存依赖项 更新缓存
[code=csharp]System.IO.File.SetLastWriteTime(Server.MapPath("test.txt"), DateTime.Now);[/code]
------------------------------------------------------------------------------
///建立缓存
===============================
[code=csharp]private void CacheData()
{
// 当缓存不存在或被删除时,重新建立缓存
if (Cache.Get("test") == null)
{
// 建立缓存依赖项,如果文件发生变化,删除缓存
CacheDependency dep = new CacheDependency(Server.MapPath(@"CacheTxtColumn.txt"));
Cache.Insert("test","value", dep);
}
}
[/code]============================
[code=csharp]/// <summary>
/// 更新缓存
/// </summary>
private void ChangeCache()
{
string _Path = @"CacheTxt\";
string _TxtName = @"Column.txt";
string _FullPath = Server.MapPath(_Path + _TxtName);
if (!System.IO.File.Exists(_FullPath))//如果文件不存在
{
if (!System.IO.Directory.Exists(_Path)) //如果目录不存在
{
System.IO.Directory.CreateDirectory(Server.MapPath(_Path));//创建所有目录
}
System.IO.File.CreateText(_FullPath);
// System.IO.StreamWriter str = new System.IO.StreamWriter(_FullPath, true);//创建文件
}
else
{
System.IO.File.SetLastWriteTime(_FullPath, DateTime.Now);
}
}
[/code]
|
|