You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
3.8 KiB

4 years ago
  1. #if MYSQL_6_9
  2. // Copyright � 2011, Oracle and/or its affiliates. All rights reserved.
  3. //
  4. // MySQL Connector/NET is licensed under the terms of the GPLv2
  5. // <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
  6. // MySQL Connectors. There are special exceptions to the terms and
  7. // conditions of the GPLv2 as it is applied to this software, see the
  8. // FLOSS License Exception
  9. // <http://www.mysql.com/about/legal/licensing/foss-exception.html>.
  10. //
  11. // This program is free software; you can redistribute it and/or modify
  12. // it under the terms of the GNU General Public License as published
  13. // by the Free Software Foundation; version 2 of the License.
  14. //
  15. // This program is distributed in the hope that it will be useful, but
  16. // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  17. // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  18. // for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License along
  21. // with this program; if not, write to the Free Software Foundation, Inc.,
  22. // 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  23. using System;
  24. using System.Collections;
  25. using System.Data;
  26. using System.Collections.Generic;
  27. using Externals.MySql.Data.MySqlClient.Properties;
  28. using System.Diagnostics;
  29. using System.Text;
  30. using System.Globalization;
  31. namespace Externals.MySql.Data.MySqlClient
  32. {
  33. internal class TableCache
  34. {
  35. private static BaseTableCache cache;
  36. static TableCache()
  37. {
  38. cache = new BaseTableCache(480 /* 8 hour max by default */);
  39. }
  40. public static void AddToCache(string commandText, ResultSet resultSet)
  41. {
  42. cache.AddToCache(commandText, resultSet);
  43. }
  44. public static ResultSet RetrieveFromCache(string commandText, int cacheAge)
  45. {
  46. return (ResultSet)cache.RetrieveFromCache(commandText, cacheAge);
  47. }
  48. public static void RemoveFromCache(string commandText)
  49. {
  50. cache.RemoveFromCache(commandText);
  51. }
  52. public static void DumpCache()
  53. {
  54. cache.Dump();
  55. }
  56. }
  57. internal class BaseTableCache
  58. {
  59. protected int MaxCacheAge;
  60. private Dictionary<string, CacheEntry> cache = new Dictionary<string, CacheEntry>();
  61. public BaseTableCache(int maxCacheAge)
  62. {
  63. MaxCacheAge = maxCacheAge;
  64. }
  65. public virtual void AddToCache(string commandText, object resultSet)
  66. {
  67. CleanCache();
  68. CacheEntry entry = new CacheEntry();
  69. entry.CacheTime = DateTime.Now;
  70. entry.CacheElement = resultSet;
  71. lock (cache)
  72. {
  73. if (cache.ContainsKey(commandText)) return;
  74. cache.Add(commandText, entry);
  75. }
  76. }
  77. public virtual object RetrieveFromCache(string commandText, int cacheAge)
  78. {
  79. CleanCache();
  80. lock (cache)
  81. {
  82. if (!cache.ContainsKey(commandText)) return null;
  83. CacheEntry entry = cache[commandText];
  84. if (DateTime.Now.Subtract(entry.CacheTime).TotalSeconds > cacheAge) return null;
  85. return entry.CacheElement;
  86. }
  87. }
  88. public void RemoveFromCache(string commandText)
  89. {
  90. lock (cache)
  91. {
  92. if (!cache.ContainsKey(commandText)) return;
  93. cache.Remove(commandText);
  94. }
  95. }
  96. public virtual void Dump()
  97. {
  98. lock (cache)
  99. cache.Clear();
  100. }
  101. protected virtual void CleanCache()
  102. {
  103. DateTime now = DateTime.Now;
  104. List<string> keysToRemove = new List<string>();
  105. lock (cache)
  106. {
  107. foreach (string key in cache.Keys)
  108. {
  109. TimeSpan diff = now.Subtract(cache[key].CacheTime);
  110. if (diff.TotalSeconds > MaxCacheAge)
  111. keysToRemove.Add(key);
  112. }
  113. foreach (string key in keysToRemove)
  114. cache.Remove(key);
  115. }
  116. }
  117. private struct CacheEntry
  118. {
  119. public DateTime CacheTime;
  120. public object CacheElement;
  121. }
  122. }
  123. }
  124. #endif