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.

204 lines
7.8 KiB

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