• OCS 使用


    C#/.net 代码样例:

    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();
            //线程安全的单例模式
            public static MemcachedClient getInstance()
            {
                if (MemClient == null)
                {
                    lock (padlock)
                    {
                        if (MemClient == null)
                        {
                            MemClientInit();                        
                        }
                    }
                }
                return MemClient;
            }
    
            static void MemClientInit()
            {
                //初始化缓存
                MemcachedClientConfiguration memConfig = new MemcachedClientConfiguration();
                IPAddress newaddress = 
       IPAddress.Parse(Dns.GetHostByName
     ("your_instanceid.m.cnhzalicm10pub001.ocs.aliyuncs.com").AddressList[0].ToString());//your_instanceid替换为你的OCS实例的ID
                IPEndPoint ipEndPoint = new IPEndPoint(newaddress, 11211);
    
                  // 配置文件 - ip
                memConfig.Servers.Add(ipEndPoint);
                // 配置文件 - 协议
               memConfig.Protocol = MemcachedProtocol.Binary;
                // 配置文件-权限
                memConfig.Authentication.Type = typeof(PlainTextAuthenticator);
                memConfig.Authentication.Parameters["zone"] = "";
                memConfig.Authentication.Parameters["userName"] = "username";
                memConfig.Authentication.Parameters["password"] = "password";
          //下面请根据实例的最大连接数进行设置
                memConfig.SocketPool.MinPoolSize = 5;
                memConfig.SocketPool.MaxPoolSize = 200;
                MemClient=new MemcachedClient(memConfig);
                      }
              }
    }
    

      调用代码:

    1.添加KEY

      MemcachedClient MemClient = MemCached.getInstance();
      MemClient.Store(StoreMode.Add, "MYKEY", "This is store in memcached server value!");
    

    2.替换KEY

      MemcachedClient MemClient = MemCached.getInstance();
      MemClient.Store(StoreMode.Replace, "MYKEY", "This is store in memcached server value111!");
    

    3.删除KEY

      MemcachedClient MemClient = MemCached.getInstance();
      MemClient.Remove("MYKEY");
    

    客户端下载地址:https://github.com/enyim/EnyimMemcached/downloads

  • 相关阅读:
    LightOJ 1024 Eid(高精度乘法+求n个数最小公约数)
    LightOJ 1414 February 29(闰年统计+容斥原理)
    LightOJ 1410 Consistent Verdicts(找规律)
    LightOJ 1369 Answering Queries(找规律)
    LightOJ 1323 Billiard Balls(找规律(蚂蚁爬木棍))
    LightOJ 1349 Aladdin and the Optimal Invitation(中位数)
    LightOJ
    LightOJ
    bzoj 4180: 字符串计数
    bzoj 4260: Codechef REBXOR
  • 原文地址:https://www.cnblogs.com/lampon/p/4040646.html
Copyright © 2020-2023  润新知