edit-icon download-icon

C#_.NET_ EnyimMemcached

Last Updated: Dec 14, 2017

Download the client

Download address

About the client

Client versions

C#/.NET example code

  1. using System.Net;
  2. using Enyim.Caching;
  3. using Enyim.Caching.Configuration;
  4. using Enyim.Caching.Memcached;
  5. namespace OCS.Memcached
  6. {
  7. public sealed class MemCached
  8. {
  9. private static MemcachedClient MemClient;
  10. static readonly object padlock = new object();
  11. //Thread-safe single instance mode
  12. public static MemcachedClient getInstance()
  13. {
  14. if (MemClient == null)
  15. {
  16. lock (padlock)
  17. {
  18. if (MemClient == null)
  19. {
  20. MemClientInit();
  21. }
  22. }
  23. }
  24. return MemClient;
  25. }
  26. static void MemClientInit()
  27. {
  28. //Initialize cache
  29. MemcachedClientConfiguration memConfig = new MemcachedClientConfiguration();
  30. IPAddress newaddress =
  31. IPAddress.Parse(Dns.GetHostEntry
  32. ("your_ocs_host").AddressList[0].ToString());//Replace your_ocs_host with the ApsaraDB for Memcache intranet address
  33. IPEndPoint ipEndPoint = new IPEndPoint(newaddress, 11211);
  34. // Configuration file - IP
  35. memConfig.Servers.Add(ipEndPoint);
  36. // Configuration file - protocol
  37. memConfig.Protocol = MemcachedProtocol.Binary;
  38. // Configuration file - permission
  39. memConfig.Authentication.Type = typeof(PlainTextAuthenticator);
  40. memConfig.Authentication.Parameters["zone"] = "";
  41. memConfig.Authentication.Parameters["userName"] = "username";
  42. memConfig.Authentication.Parameters["password"] = "password";
  43. //Follow the maximum connections of the instance for the following settings
  44. memConfig.SocketPool.MinPoolSize = 5;
  45. memConfig.SocketPool.MaxPoolSize = 200;
  46. MemClient=new MemcachedClient(memConfig);
  47. }
  48. }
  49. }

Dependencies

Code:MemcachedClient MemClient = MemCached.getInstance();

Thank you! We've received your feedback.