C#/.NET: EnyimMemcached

更新时间: 2016-12-19 10:39:47

客户端下载

客户端下载地址

客户端介绍

客户端版本介绍

C#/.NET 代码示例

  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. //线程安全的单例模式
  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. //初始化缓存
  29. MemcachedClientConfiguration memConfig = new MemcachedClientConfiguration();
  30. IPAddress newaddress =
  31. IPAddress.Parse(Dns.GetHostEntry
  32. ("your_ocs_host").AddressList[0].ToString());//your_ocs_host替换为OCS内网地址
  33. IPEndPoint ipEndPoint = new IPEndPoint(newaddress, 11211);
  34. // 配置文件 - ip
  35. memConfig.Servers.Add(ipEndPoint);
  36. // 配置文件 - 协议
  37. memConfig.Protocol = MemcachedProtocol.Binary;
  38. // 配置文件-权限
  39. memConfig.Authentication.Type = typeof(PlainTextAuthenticator);
  40. memConfig.Authentication.Parameters["zone"] = "";
  41. memConfig.Authentication.Parameters["userName"] = "username";
  42. memConfig.Authentication.Parameters["password"] = "password";
  43. //下面请根据实例的最大连接数进行设置
  44. memConfig.SocketPool.MinPoolSize = 5;
  45. memConfig.SocketPool.MaxPoolSize = 200;
  46. MemClient=new MemcachedClient(memConfig);
  47. }
  48. }
  49. }

依赖

调用代码:MemcachedClient MemClient = MemCached.getInstance();

上一篇: Python 下一篇: C++