Asp.net中使用緩存(cache)
阿新 • • 發佈:2019-01-16
https 設置 add dem ros change app map time
做了一個時間優化的項目,目的就是縮短程序過程中的時間花費,最後發現了asp.net和asp.net core 中都有緩存工具來進行緩存,以加快訪問速度。
找了官方demo來進行分析:
ObjectCache cache = MemoryCache.Default; string fileContents = cache["filecontents"] as string; //獲取緩存值 if (fileContents == null) { CacheItemPolicy policy= new CacheItemPolicy(); policy.AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(10.0);//設置緩存失效時間 List<string> filePaths = new List<string>(); string cachedFilePath = Server.MapPath("~") + "\\cacheText.txt"; filePaths.Add(cachedFilePath); policy.ChangeMonitors.Add(new HostFileChangeMonitor(filePaths)); fileContents = System.IO.File.ReadAllText(cachedFilePath, Encoding.Default); cache.Set("filecontents", fileContents, policy);//設置緩存中 } return fileContents;
官方參考路徑:https://docs.microsoft.com/en-us/dotnet/framework/performance/caching-in-net-framework-applications
Asp.net中使用緩存(cache)