|
| 1 | +namespace DotNetToolkit.Repository.Caching.Memcached |
| 2 | +{ |
| 3 | + using Configuration.Caching; |
| 4 | + using Enyim.Caching; |
| 5 | + using Enyim.Caching.Configuration; |
| 6 | + using Enyim.Caching.Memcached; |
| 7 | + using JetBrains.Annotations; |
| 8 | + using Newtonsoft.Json; |
| 9 | + using System; |
| 10 | + using Utility; |
| 11 | + |
| 12 | + /// <summary> |
| 13 | + /// An implementation of <see cref="ICache" />. |
| 14 | + /// </summary> |
| 15 | + public class MemcachedCache : ICache |
| 16 | + { |
| 17 | + #region Properties |
| 18 | + |
| 19 | + /// <summary> |
| 20 | + /// Gets the memcached client. |
| 21 | + /// </summary> |
| 22 | + public IMemcachedClient Client { get; } |
| 23 | + |
| 24 | + #endregion |
| 25 | + |
| 26 | + #region Constructors |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// Initializes a new instance of the <see cref="MemcachedCache" /> class using the default configuration section (enyim/memcached). |
| 30 | + /// </summary> |
| 31 | + public MemcachedCache() |
| 32 | + { |
| 33 | + Client = new MemcachedClient(); |
| 34 | + } |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// Initializes a new instance of the <see cref="MemcachedCache" /> class using the specified configuration section. |
| 38 | + /// </summary> |
| 39 | + /// <param name="sectionName">The name of the configuration section to be used for configuring the behavior of the client.</param> |
| 40 | + public MemcachedCache([NotNull] string sectionName) |
| 41 | + { |
| 42 | + Client = new MemcachedClient(Guard.NotEmpty(sectionName, nameof(sectionName))); |
| 43 | + } |
| 44 | + |
| 45 | + /// <summary> |
| 46 | + /// Initializes a new instance of the <see cref="MemcachedCache" /> class using the specified client. |
| 47 | + /// </summary> |
| 48 | + /// <param name="client">The memcached client.</param> |
| 49 | + public MemcachedCache([NotNull] IMemcachedClient client) |
| 50 | + { |
| 51 | + Client = Guard.NotNull(client, nameof(client)); |
| 52 | + } |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// Initializes a new instance of the <see cref="MemcachedCache" /> class. |
| 56 | + /// </summary> |
| 57 | + /// <param name="configuration">The configuration to use for the memcached client.</param> |
| 58 | + public MemcachedCache([NotNull] IMemcachedClientConfiguration configuration) |
| 59 | + { |
| 60 | + Client = new MemcachedClient(Guard.NotNull(configuration, nameof(configuration))); |
| 61 | + } |
| 62 | + |
| 63 | + /// <summary> |
| 64 | + /// Initializes a new instance of the <see cref="MemcachedCache" /> class. |
| 65 | + /// </summary> |
| 66 | + /// <param name="host">The host name.</param> |
| 67 | + /// <param name="port">The port.</param> |
| 68 | + /// <param name="protocol">The type of the communication between client and server.</param> |
| 69 | + public MemcachedCache([NotNull] string host, int port, MemcachedProtocol protocol) |
| 70 | + { |
| 71 | + Guard.NotEmpty(host, nameof(host)); |
| 72 | + |
| 73 | + var config = new MemcachedClientConfiguration(); |
| 74 | + |
| 75 | + config.AddServer(host, port); |
| 76 | + config.Protocol = protocol; |
| 77 | + |
| 78 | + Client = new MemcachedClient(config); |
| 79 | + } |
| 80 | + |
| 81 | + /// <summary> |
| 82 | + /// Initializes a new instance of the <see cref="MemcachedCache" /> class. |
| 83 | + /// </summary> |
| 84 | + /// <param name="host">The host name.</param> |
| 85 | + /// <param name="port">The port.</param> |
| 86 | + /// <param name="username">The username to use to authenticate with the server.</param> |
| 87 | + /// <param name="password">The password to use to authenticate with the server.</param> |
| 88 | + /// <param name="protocol">The type of the communication between client and server.</param> |
| 89 | + public MemcachedCache([NotNull] string host, int port, [NotNull] string username, [NotNull] string password, MemcachedProtocol protocol) |
| 90 | + { |
| 91 | + Guard.NotEmpty(host, nameof(host)); |
| 92 | + Guard.NotEmpty(username, nameof(username)); |
| 93 | + Guard.NotEmpty(password, nameof(password)); |
| 94 | + |
| 95 | + var config = new MemcachedClientConfiguration(); |
| 96 | + |
| 97 | + config.AddServer(host, port); |
| 98 | + config.Protocol = protocol; |
| 99 | + config.Authentication.Type = typeof(PlainTextAuthenticator); |
| 100 | + config.Authentication.Parameters["userName"] = username; |
| 101 | + config.Authentication.Parameters["password"] = password; |
| 102 | + |
| 103 | + Client = new MemcachedClient(config); |
| 104 | + } |
| 105 | + |
| 106 | + #endregion |
| 107 | + |
| 108 | + #region Private Methods |
| 109 | + |
| 110 | + private static string Serialize(object o) |
| 111 | + { |
| 112 | + if (o == null) |
| 113 | + return null; |
| 114 | + |
| 115 | + return JsonConvert.SerializeObject(o); |
| 116 | + } |
| 117 | + |
| 118 | + private static T Deserialize<T>(string v) |
| 119 | + { |
| 120 | + if (string.IsNullOrEmpty(v)) |
| 121 | + return default(T); |
| 122 | + |
| 123 | + return JsonConvert.DeserializeObject<T>(v); |
| 124 | + } |
| 125 | + |
| 126 | + #endregion |
| 127 | + |
| 128 | + #region Implementation of ICache |
| 129 | + |
| 130 | + /// <summary> |
| 131 | + /// Create or overwrite an entry in the cache. |
| 132 | + /// </summary> |
| 133 | + /// <typeparam name="T">The type of the cache value.</typeparam> |
| 134 | + /// <param name="key">An object identifying the entry.</param> |
| 135 | + /// <param name="value">The value to cache.</param> |
| 136 | + /// <param name="expiry">The cache expiration time.</param> |
| 137 | + /// <param name="cacheRemovedCallback">A callback function for a value is removed from the cache.</param> |
| 138 | + public void Set<T>([NotNull] string key, [CanBeNull] T value, [CanBeNull] TimeSpan? expiry, [CanBeNull] Action<string> cacheRemovedCallback = null) |
| 139 | + { |
| 140 | + Guard.NotEmpty(key, nameof(key)); |
| 141 | + |
| 142 | + if (expiry.HasValue) |
| 143 | + { |
| 144 | + Client.Store(StoreMode.Set, key, Serialize(value), expiry.Value); |
| 145 | + } |
| 146 | + else |
| 147 | + { |
| 148 | + Client.Store(StoreMode.Set, key, Serialize(value)); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + /// <summary> |
| 153 | + /// Removes the object associated with the given key. |
| 154 | + /// </summary> |
| 155 | + /// <param name="key">An object identifying the entry.</param> |
| 156 | + public void Remove([NotNull] string key) |
| 157 | + { |
| 158 | + Client.Remove(Guard.NotEmpty(key, nameof(key))); |
| 159 | + } |
| 160 | + |
| 161 | + /// <summary> |
| 162 | + /// Gets the item associated with this key if present. |
| 163 | + /// </summary> |
| 164 | + /// <typeparam name="T">The type of the cache value.</typeparam> |
| 165 | + /// <param name="key">An object identifying the requested entry.</param> |
| 166 | + /// <param name="value">The object found in the cache.</param> |
| 167 | + /// <returns>c<c>true</c> if the an object was found with the specified key; otherwise, <c>false</c>.</returns> |
| 168 | + public bool TryGetValue<T>(string key, out T value) |
| 169 | + { |
| 170 | + try |
| 171 | + { |
| 172 | + value = Deserialize<T>(Client.Get<string>(Guard.NotEmpty(key, nameof(key)))); |
| 173 | + |
| 174 | + if (Equals(value, default(T))) |
| 175 | + { |
| 176 | + value = default(T); |
| 177 | + |
| 178 | + return false; |
| 179 | + } |
| 180 | + } |
| 181 | + catch |
| 182 | + { |
| 183 | + value = default(T); |
| 184 | + |
| 185 | + return false; |
| 186 | + } |
| 187 | + |
| 188 | + return true; |
| 189 | + } |
| 190 | + |
| 191 | + /// <summary> |
| 192 | + /// Increments the number stored at key by one |
| 193 | + /// </summary> |
| 194 | + /// <param name="key">The key.</param> |
| 195 | + /// <param name="defaultValue">The default value.</param> |
| 196 | + /// <param name="incrementValue">The increment value.</param> |
| 197 | + /// <returns>The value of key after the increment.</returns> |
| 198 | + public int Increment(string key, int defaultValue, int incrementValue) |
| 199 | + { |
| 200 | + Guard.NotEmpty(key, nameof(key)); |
| 201 | + |
| 202 | + var value = Client.Increment(key, Convert.ToUInt64(defaultValue), Convert.ToUInt64(incrementValue)); |
| 203 | + |
| 204 | + return Convert.ToInt32(value); |
| 205 | + } |
| 206 | + |
| 207 | + #endregion |
| 208 | + |
| 209 | + #region Implementation of IDisposable |
| 210 | + |
| 211 | + /// <summary> |
| 212 | + /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. |
| 213 | + /// </summary> |
| 214 | + public void Dispose() |
| 215 | + { |
| 216 | + Client.Dispose(); |
| 217 | + } |
| 218 | + |
| 219 | + #endregion |
| 220 | + } |
| 221 | +} |
0 commit comments