缓存(关联文件)处理

来源:互联网 发布:决策树分类实现Python 编辑:程序博客网 时间:2024/05/06 20:51

参考:http://msdn.microsoft.com/library/system.runtime.caching.memorycache(v=vs.110).aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-2

引用: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);        }


0 0