【wpf】缓存

1、引用 System.Runtime.Caching

2、源 msdn

 1 //实例化MemoryCache类
 2             ObjectCache cache = MemoryCache.Default;
 3             //读取缓存
 4             string fileContents = cache["filecontents"] as string;
 5             if (fileContents == null)
 6             {
 7                 StringBuilder strb = new StringBuilder();
 8                 path = AppDomain.CurrentDomain.BaseDirectory + "Resources/caching.txt";
 9                 //读取文件内容
10                 StreamReader sr = new StreamReader(path, Encoding.Default);
11                 String line;
12                 while ((line = sr.ReadLine()) != null)
13                 {
14                     strb.Append(line.ToString());
15                 }
16                 fileContents = strb.ToString() + "---" + DateTime.Now;
17                 //设置缓存过期时间
18                 CacheItemPolicy policy = new CacheItemPolicy();
19                 policy.AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(10.0);
20 
21                 List<string> filePaths = new List<string>();
22                 filePaths.Add(path);
23                 //添加缓存
24                 cache.Set("filecontents", fileContents, policy);
25             }

 

posted @ 2016-05-18 14:05  oiliu  阅读(2210)  评论(0编辑  收藏  举报