引用:System.Runtime.Caching.dll,如下测试,fm4.5
static void CacheTest()
{
string cname = "filescontents";
ObjectCache cc = MemoryCache.Default;
string fileContents = cc[cname] as string;
if (fileContents == null)
{
CacheItemPolicy policy = new CacheItemPolicy();
TimeSpan sp = new TimeSpan(0, 1, 0);
policy.SlidingExpiration = sp;
List<string> filePaths = new List<string>();
string path = System.IO.Directory.GetCurrentDirectory() + "\\example.txt";
filePaths.Add(path);
policy.ChangeMonitors.Add(new HostFileChangeMonitor(filePaths));
fileContents = System.IO.File.ReadAllText(path, Encoding.Default);
cc.Set(cname, fileContents, policy);
}
Console.WriteLine(fileContents);
}
static void Main(string[] args)
{
//ExecuteCode(WriteData);
//ExecuteCode(ReadData);
//ExecuteCode(TransData);
bool quit = false;
while (!quit)
{
Console.Write("get cache: ");
string demo = Console.ReadLine();
switch (demo)
{
case "Y": ExecuteCode(CacheTest); break;
case "Q":
quit = true;
break;
default:
Console.WriteLine("Choose a Word of Y and Q(to quit)");
break;
}
}
Console.ReadKey();
}
public static void ExecuteCode(Action a)
{
System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
stopwatch.Start();
a();
stopwatch.Stop();
TimeSpan timespan = stopwatch.Elapsed;
Console.WriteLine("运行{0}秒", timespan.TotalSeconds);
}