namespace Easychart.Finance.DataProvider { using System; using System.IO; using System.Reflection; using System.Web; using System.Web.Caching; public class CacheDataManagerBase : DataManagerBase { public string CacheRoot; public TimeSpan CacheTimeSpan = TimeSpan.Zero; public bool EnableFileCache = true; public bool EnableMemoryCache = false; private string GetKey(string Code) { return ("Cache_" + Code + "_" + base.StartTime.ToString("yyyyMMddHHmmss") + "_" + base.EndTime.ToString("yyyyMMddHHmmss")); } public CommonDataProvider MergeRealtime(CommonDataProvider cdp, string Code) { if (cdp != null) { if (base.DownloadRealTimeQuote && (this.CacheTimeSpan.Days >= 1)) { DataPacket dp = DataPacket.DownloadFromYahoo(Code); cdp.Merge(dp); cdp.SetStringData("Name", dp.StockName); } this.SetStrings(cdp, Code); } return cdp; } public override IDataProvider this[string Code, int Count] { get { if (this.CacheTimeSpan == TimeSpan.Zero) { return base[Code, Count]; } if (this.EnableMemoryCache && (HttpContext.Current != null)) { object obj2 = HttpContext.Current.Cache[this.GetKey(Code)]; if (obj2 != null) { return this.MergeRealtime((CommonDataProvider) obj2, Code); } } string path = ""; if ((this.EnableFileCache && (this.CacheRoot != null)) && (this.CacheRoot != "")) { try { if (this.CacheRoot.EndsWith(@"\")) { this.CacheRoot = this.CacheRoot.Substring(0, this.CacheRoot.Length - 1); } if (!Directory.Exists(this.CacheRoot)) { Directory.CreateDirectory(this.CacheRoot); } this.CacheRoot = this.CacheRoot + @"\"; path = this.CacheRoot + this.GetKey(Code); if (File.Exists(path) && (File.GetLastWriteTime(path).Add(this.CacheTimeSpan) > DateTime.Now)) { CommonDataProvider cdp = new CommonDataProvider(this); cdp.LoadBinary(path); return this.MergeRealtime(cdp, Code); } } catch { path = ""; } } IDataProvider provider2 = base[Code, Count]; bool flag = false; if ((provider2 is CommonDataProvider) && (provider2.Count > 0)) { try { if (this.EnableFileCache && (path != "")) { (provider2 as CommonDataProvider).SaveBinary(path); flag = true; } } catch { } } if ((this.EnableMemoryCache && (HttpContext.Current != null)) && (!flag && (provider2 != null))) { HttpContext.Current.Cache.Add(this.GetKey(Code), provider2, null, DateTime.Now.Add(this.CacheTimeSpan), TimeSpan.Zero, CacheItemPriority.Normal, null); } return this.MergeRealtime((CommonDataProvider) provider2, Code); } } } }