All Products
Search
Document Center

ApsaraDB for Memcache:C#_.NET_ EnyimMemcached

Last Updated:Aug 08, 2023

Download the client

Download address

About the client

Client versions

C#/.NET example code

using System.Net;
using Enyim.Caching;
using Enyim.Caching.Configuration;
using Enyim.Caching.Memcached;
namespace OCS.Memcached
{
    public sealed class MemCached
    {
        private static MemcachedClient MemClient;
        static readonly object padlock = new object();
        //Thread-safe single instance mode
        public static MemcachedClient getInstance()
        {
            if (MemClient == null)
            {
                lock (padlock)
                {
                    if (MemClient == null)
                    {
                        MemClientInit();                        
                    }
                }
            }
            return MemClient;
        }

        static void MemClientInit()
        {
            //Initialize cache
            MemcachedClientConfiguration memConfig = new MemcachedClientConfiguration();
            IPAddress newaddress = 
   IPAddress.Parse(Dns.GetHostEntry
 ("your_ocs_host").AddressList[0].ToString());//Replace your_ocs_host with the ApsaraDB for Memcache intranet address
            IPEndPoint ipEndPoint = new IPEndPoint(newaddress, 11211);

              // Configuration file - IP
            memConfig.Servers.Add(ipEndPoint);
            // Configuration file - protocol
           memConfig.Protocol = MemcachedProtocol.Binary;
            // Configuration file - permission
            memConfig.Authentication.Type = typeof(PlainTextAuthenticator);
            memConfig.Authentication.Parameters["zone"] = "";
            memConfig.Authentication.Parameters["userName"] = "username";
            memConfig.Authentication.Parameters["password"] = "password";
      //Follow the maximum connections of the instance for the following settings
            memConfig.SocketPool.MinPoolSize = 5;
            memConfig.SocketPool.MaxPoolSize = 200;
            MemClient=new MemcachedClient(memConfig);
                  }
          }
}

Dependencies

Code:MemcachedClient MemClient = MemCached.getInstance();