【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             }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM