CacheDataManagerBase.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. namespace Easychart.Finance.DataProvider
  2. {
  3. using System;
  4. using System.IO;
  5. using System.Reflection;
  6. using System.Web;
  7. using System.Web.Caching;
  8. public class CacheDataManagerBase : DataManagerBase
  9. {
  10. public string CacheRoot;
  11. public TimeSpan CacheTimeSpan = TimeSpan.Zero;
  12. public bool EnableFileCache = true;
  13. public bool EnableMemoryCache = false;
  14. private string GetKey(string Code)
  15. {
  16. return ("Cache_" + Code + "_" + base.StartTime.ToString("yyyyMMddHHmmss") + "_" + base.EndTime.ToString("yyyyMMddHHmmss"));
  17. }
  18. public CommonDataProvider MergeRealtime(CommonDataProvider cdp, string Code)
  19. {
  20. if (cdp != null)
  21. {
  22. if (base.DownloadRealTimeQuote && (this.CacheTimeSpan.Days >= 1))
  23. {
  24. DataPacket dp = DataPacket.DownloadFromYahoo(Code);
  25. cdp.Merge(dp);
  26. cdp.SetStringData("Name", dp.StockName);
  27. }
  28. this.SetStrings(cdp, Code);
  29. }
  30. return cdp;
  31. }
  32. public override IDataProvider this[string Code, int Count]
  33. {
  34. get
  35. {
  36. if (this.CacheTimeSpan == TimeSpan.Zero)
  37. {
  38. return base[Code, Count];
  39. }
  40. if (this.EnableMemoryCache && (HttpContext.Current != null))
  41. {
  42. object obj2 = HttpContext.Current.Cache[this.GetKey(Code)];
  43. if (obj2 != null)
  44. {
  45. return this.MergeRealtime((CommonDataProvider) obj2, Code);
  46. }
  47. }
  48. string path = "";
  49. if ((this.EnableFileCache && (this.CacheRoot != null)) && (this.CacheRoot != ""))
  50. {
  51. try
  52. {
  53. if (this.CacheRoot.EndsWith(@"\"))
  54. {
  55. this.CacheRoot = this.CacheRoot.Substring(0, this.CacheRoot.Length - 1);
  56. }
  57. if (!Directory.Exists(this.CacheRoot))
  58. {
  59. Directory.CreateDirectory(this.CacheRoot);
  60. }
  61. this.CacheRoot = this.CacheRoot + @"\";
  62. path = this.CacheRoot + this.GetKey(Code);
  63. if (File.Exists(path) && (File.GetLastWriteTime(path).Add(this.CacheTimeSpan) > DateTime.Now))
  64. {
  65. CommonDataProvider cdp = new CommonDataProvider(this);
  66. cdp.LoadBinary(path);
  67. return this.MergeRealtime(cdp, Code);
  68. }
  69. }
  70. catch
  71. {
  72. path = "";
  73. }
  74. }
  75. IDataProvider provider2 = base[Code, Count];
  76. bool flag = false;
  77. if ((provider2 is CommonDataProvider) && (provider2.Count > 0))
  78. {
  79. try
  80. {
  81. if (this.EnableFileCache && (path != ""))
  82. {
  83. (provider2 as CommonDataProvider).SaveBinary(path);
  84. flag = true;
  85. }
  86. }
  87. catch
  88. {
  89. }
  90. }
  91. if ((this.EnableMemoryCache && (HttpContext.Current != null)) && (!flag && (provider2 != null)))
  92. {
  93. HttpContext.Current.Cache.Add(this.GetKey(Code), provider2, null, DateTime.Now.Add(this.CacheTimeSpan), TimeSpan.Zero, CacheItemPriority.Normal, null);
  94. }
  95. return this.MergeRealtime((CommonDataProvider) provider2, Code);
  96. }
  97. }
  98. }
  99. }