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.

162 lines
5.2 KiB

4 years ago
  1. #if MYSQL_6_10
  2. // Copyright ?2016, 2018, 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.Generic;
  25. using System.Linq;
  26. namespace Externals.MySql.Data.MySqlClient
  27. {
  28. internal class TableCache
  29. {
  30. private static readonly BaseTableCache cache;
  31. static TableCache()
  32. {
  33. cache = new BaseTableCache(480 /* 8 hour max by default */);
  34. }
  35. public static void AddToCache(string commandText, ResultSet resultSet)
  36. {
  37. cache.AddToCache(commandText, resultSet);
  38. }
  39. public static ResultSet RetrieveFromCache(string commandText, int cacheAge)
  40. {
  41. return (ResultSet)cache.RetrieveFromCache(commandText, cacheAge);
  42. }
  43. public static void RemoveFromCache(string commandText)
  44. {
  45. cache.RemoveFromCache(commandText);
  46. }
  47. public static void DumpCache()
  48. {
  49. cache.Dump();
  50. }
  51. }
  52. /// <summary>
  53. /// Defines the basic operations to be performed on the table cache.
  54. /// </summary>
  55. internal class BaseTableCache
  56. {
  57. /// <summary>
  58. /// The maximum age allowed for cache entries.
  59. /// </summary>
  60. protected int MaxCacheAge;
  61. private Dictionary<string, CacheEntry> cache = new Dictionary<string, CacheEntry>();
  62. public BaseTableCache(int maxCacheAge)
  63. {
  64. MaxCacheAge = maxCacheAge;
  65. }
  66. /// <summary>
  67. /// Adds the given command and result set to the cache.
  68. /// </summary>
  69. /// <param name="commandText">The command to store in the cache.</param>
  70. /// <param name="resultSet">The resultset associated to the stored command.</param>
  71. public virtual void AddToCache(string commandText, object resultSet)
  72. {
  73. CleanCache();
  74. CacheEntry entry = new CacheEntry();
  75. entry.CacheTime = DateTime.Now;
  76. entry.CacheElement = resultSet;
  77. lock (cache)
  78. {
  79. if (cache.ContainsKey(commandText)) return;
  80. cache.Add(commandText, entry);
  81. }
  82. }
  83. /// <summary>
  84. /// Retrieves the specified command from the cache.
  85. /// </summary>
  86. /// <param name="commandText">The command to retrieve.</param>
  87. /// <param name="cacheAge">The allowed age for the cache entry.</param>
  88. /// <returns></returns>
  89. public virtual object RetrieveFromCache(string commandText, int cacheAge)
  90. {
  91. CleanCache();
  92. lock (cache)
  93. {
  94. if (!cache.ContainsKey(commandText)) return null;
  95. CacheEntry entry = cache[commandText];
  96. if (DateTime.Now.Subtract(entry.CacheTime).TotalSeconds > cacheAge) return null;
  97. return entry.CacheElement;
  98. }
  99. }
  100. /// <summary>
  101. /// Removes the specified command from the cache.
  102. /// </summary>
  103. /// <param name="commandText">The command to remove from the cache.</param>
  104. public void RemoveFromCache(string commandText)
  105. {
  106. lock (cache)
  107. {
  108. if (!cache.ContainsKey(commandText)) return;
  109. cache.Remove(commandText);
  110. }
  111. }
  112. /// <summary>
  113. /// Clears the cache.
  114. /// </summary>
  115. public virtual void Dump()
  116. {
  117. lock (cache)
  118. cache.Clear();
  119. }
  120. /// <summary>
  121. /// Removes cache entries older than the value defined by <see cref="MaxCacheAge"/>.
  122. /// </summary>
  123. protected virtual void CleanCache()
  124. {
  125. DateTime now = DateTime.Now;
  126. List<string> keysToRemove = new List<string>();
  127. lock (cache)
  128. {
  129. keysToRemove.AddRange(from key in cache.Keys let diff = now.Subtract(cache[key].CacheTime) where diff.TotalSeconds > MaxCacheAge select key);
  130. foreach (string key in keysToRemove)
  131. cache.Remove(key);
  132. }
  133. }
  134. private struct CacheEntry
  135. {
  136. public DateTime CacheTime;
  137. public object CacheElement;
  138. }
  139. }
  140. }
  141. #endif