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.

200 lines
8.3 KiB

4 years ago
  1. #if MYSQL_6_10
  2. // Copyright © 2004, 2016, 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.IO;
  26. using Externals.MySql.Data.Common;
  27. namespace Externals.MySql.Data.MySqlClient.Memcached
  28. {
  29. /// <summary>
  30. /// An interface of the client memcached protocol. This class is abstract for
  31. /// implementation of the Memcached client interface see <see cref="TextClient"/> for the
  32. /// text protocol version and <see cref="BinaryClient"/> for the binary protocol version.
  33. /// </summary>
  34. internal abstract class Client
  35. {
  36. /// <summary>
  37. /// The port used by the connection.
  38. /// </summary>
  39. protected uint port;
  40. /// <summary>
  41. /// The server DNS or IP address used by the connection.
  42. /// </summary>
  43. protected string server;
  44. /// <summary>
  45. /// The network stream used by the connecition.
  46. /// </summary>
  47. protected Stream stream;
  48. /// <summary>
  49. /// Factory method for creating instances of <see cref="Client"/> that implement a connection with the requested features.
  50. /// The connection object returned must be explicitely opened see method <see cref="Client.Open"/>.
  51. /// </summary>
  52. /// <param name="server">The Memcached server DNS or IP address.</param>
  53. /// <param name="port">The port for the Memcached server</param>
  54. /// <param name="flags">A set of flags indicating characterestics requested.</param>
  55. /// <returns>An instance of a client connection ready to be used.</returns>
  56. public static Client GetInstance(string server, uint port, MemcachedFlags flags)
  57. {
  58. if ((flags | MemcachedFlags.TextProtocol) != 0)
  59. {
  60. return new TextClient(server, port);
  61. }
  62. else if ((flags | MemcachedFlags.BinaryProtocol) != 0)
  63. {
  64. return new BinaryClient(server, port);
  65. }
  66. return null;
  67. }
  68. /// <summary>
  69. /// Opens the client connection.
  70. /// </summary>
  71. public virtual void Open()
  72. {
  73. this.stream = StreamCreator.GetStream(server, port, null, 10, new DBVersion(), 60);
  74. }
  75. /// <summary>
  76. /// Closes the client connection.
  77. /// </summary>
  78. public virtual void Close()
  79. {
  80. stream.Dispose();
  81. }
  82. protected Client(string server, uint port)
  83. {
  84. this.server = server;
  85. this.port = port;
  86. }
  87. /// <summary>
  88. /// Adds a new key/value pair with the given TimeSpan expiration.
  89. /// </summary>
  90. /// <param name="key">The key for identifying the entry.</param>
  91. /// <param name="data">The data to associate with the key.</param>
  92. /// <param name="expiration">The interval of timespan, use TimeSpan.Zero for no expiration.</param>
  93. public abstract void Add(string key, object data, TimeSpan expiration);
  94. /// <summary>
  95. /// Appens the data to the existing data for the associated key.
  96. /// </summary>
  97. /// <param name="key">The key for identifying the entry.</param>
  98. /// <param name="data">The data to append with the data associated with the key.</param>
  99. public abstract void Append(string key, object data);
  100. /// <summary>
  101. /// Executes the Check-and-set Memcached operation.
  102. /// </summary>
  103. /// <param name="key">The key for identifying the entry.</param>
  104. /// <param name="data">The data to use in the CAS.</param>
  105. /// <param name="expiration">The interval of timespan, use TimeSpan.Zero for no expiration.</param>
  106. /// <param name="casUnique">The CAS unique value to use.</param>
  107. /// <exception cref="MemcachedException"></exception>
  108. public abstract void Cas(string key, object data, TimeSpan expiration, ulong casUnique);
  109. /// <summary>
  110. /// Decrements the value associated with a key by the given amount.
  111. /// </summary>
  112. /// <param name="key">The key associated with the value to decrement.</param>
  113. /// <param name="amount">The amount to decrement the value.</param>
  114. public abstract void Decrement(string key, int amount);
  115. /// <summary>
  116. /// Removes they pair key/value given the specified key.
  117. /// </summary>
  118. /// <param name="key"></param>
  119. public abstract void Delete(string key);
  120. /// <summary>
  121. /// Removes all entries from the storage, effectively invalidating the whole cache.
  122. /// </summary>
  123. /// <param name="delay">The interval after which the cache will be cleaned. Can be TimeSpan.Zero for immediately.</param>
  124. public abstract void FlushAll(TimeSpan delay);
  125. /// <summary>
  126. /// Get the key/value pair associated with a given key.
  127. /// </summary>
  128. /// <param name="key">The key for which to returm the key/value.</param>
  129. /// <returns>The key/value associated with the key or a MemcachedException if it does not exists.</returns>
  130. public abstract KeyValuePair<string, object> Get(string key);
  131. /// <summary>
  132. /// Increments the value associated with a key by the given amount.
  133. /// </summary>
  134. /// <param name="key">The key associated with the value to increment.</param>
  135. /// <param name="amount">The amount to increment the value.</param>
  136. public abstract void Increment(string key, int amount);
  137. /// <summary>
  138. /// Prepends the data to the existing data for the associated key.
  139. /// </summary>
  140. /// <param name="key">The key for identifying the entry.</param>
  141. /// <param name="data">The data to append with the data associated with the key.</param>
  142. public abstract void Prepend(string key, object data);
  143. /// <summary>
  144. /// Replaces the value associated with the given key with another value.
  145. /// </summary>
  146. /// <param name="key">The key for identifying the entry.</param>
  147. /// <param name="data">The data to replace the value associated with the key.</param>
  148. /// <param name="expiration">The interval of timespan, use TimeSpan.Zero for no expiration.</param>
  149. public abstract void Replace(string key, object data, TimeSpan expiration);
  150. /// <summary>
  151. /// Set the value of a given key.
  152. /// </summary>
  153. /// <param name="key">The key for identifying the entry.</param>
  154. /// <param name="data">The data to associate with the given key.</param>
  155. /// <param name="expiration">The interval of timespan, use TimeSpan.Zero for no expiration.</param>
  156. public abstract void Set(string key, object data, TimeSpan expiration);
  157. }
  158. /// <summary>
  159. /// A set of flags for requesting new instances of connections
  160. /// </summary>
  161. [Flags]
  162. internal enum MemcachedFlags : ushort
  163. {
  164. /// <summary>
  165. /// Requests a connection implememting the text protocol.
  166. /// </summary>
  167. TextProtocol = 0x1,
  168. /// <summary>
  169. /// Requests a connection implementing the binary protocol.
  170. /// </summary>
  171. BinaryProtocol = 0x2,
  172. /// <summary>
  173. /// Requests a TCP connection. Currently UDP is not supported.
  174. /// </summary>
  175. Tcp = 0x4
  176. }
  177. }
  178. #endif