542179528 发表于 2018-12-25 09:01:21

#Memcached系列#(5)使用.NET memcached client library访问Memcached的一个C#控制台程序

using System;  
using Memcached.ClientLibrary;
  

  
namespace LearnMemcached
  
{
  
    class Program
  
    {
  
      static void Main(string[] args)
  
      {
  
            string[] serverlist = { "192.168.100.63:11211", "192.168.100.85:11211" };//注意:这里IP地址+端口号,即“IP:Port”
  

  
            //初始化池
  
            SockIOPool pool = SockIOPool.GetInstance();
  
            pool.SetServers(serverlist);
  

  
            pool.InitConnections = 3;
  
            pool.MinConnections = 3;
  
            pool.MaxConnections = 5;
  

  
            pool.SocketConnectTimeout = 1000;
  
            pool.SocketTimeout = 3000;
  

  
            pool.MaintenanceSleep = 30;
  
            pool.Failover = true;
  

  
            pool.Nagle = false;
  
            pool.Initialize();
  

  
            //获得客户端实例
  
            MemcachedClient mc = new MemcachedClient();
  
            mc.EnableCompression = false;
  

  

  
            Console.WriteLine("------------测试-----------");
  
            mc.Set("test", "myvalue");////存储数据到缓存服务器,这里将字符串"my value"缓存,key 是"test"
  

  
            PrintMessage(mc);
  
            mc.Delete("test");
  
            PrintMessage(mc);
  

  
            SockIOPool.GetInstance().Shutdown();//关闭池, 关闭sockets
  
      }
  

  
      private static void PrintMessage(MemcachedClient mc)
  
      {
  
            if (mc.KeyExists("test"))//测试缓存存在key为test的item
  
            {
  
                Console.WriteLine("test is Exists");
  
                Console.WriteLine(mc.Get("test").ToString());
  
            }
  
            else
  
            {
  
                Console.WriteLine("test not Exists");
  
            }
  

  
            Console.ReadLine();
  
      }
  
    }
  
}


页: [1]
查看完整版本: #Memcached系列#(5)使用.NET memcached client library访问Memcached的一个C#控制台程序