Memcache简单使用

1:Memcache的下载https://pan.baidu.com/s/1dFnB3NV/08中

   简单安装:直接点击文件夹中的memcached.exe文件即可.但是每次使用都需要双击,比较麻烦.所以也可以安装到Windows服务中

2:将Memcache安装到Windows服务中

  2.1 以管理员身份启动命令窗口

      命令: F:

       cd F:\memcache

      memcached -d install

      memcached -d start

 

3:测试

  通过cmd进入命令提示符窗口后

  telnet 127.0.0.1 11211

    stats

 4 C#操作Memcache所需要的dll文件也在百度云盘中的08文件夹下MemcacheDoNetLibraryAll中

  4.1 创建一个控制条应用程序,并引用文件

  将文件添加到项目的lib文件夹下,添加引用 Commons.dll,ICSharpCode.SharpZipLib.dll,log4net.dll,Memcached.ClientLibrary.dll 

using Memcached.ClientLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MemcacheTest
{
    class Program
    {
         [STAThread]
        static void Main(string[] args)
        {
             //指定Memcache的IP地址和端口号
             string[] serverlist = { "127.0.0.1:11211", "10.0.0.132:11211" };
 
             //初始化池
             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();
 
             // 获得客户端实例--根据IP地址寻找服务器(哈希算法取余数)
             //负责连接服务端
             MemcachedClient mc = new MemcachedClient();
             mc.EnableCompression = false;
 
             Console.WriteLine("------------测  试-----------");
            //向Memcache服务端存数据,键值对
             mc.Set("test", "my value");  //存储数据到缓存服务器,这里将字符串"my value"缓存,key 是"test"
            //判断键值对中是否有值
             if (mc.KeyExists("test"))   //测试缓存存在key为test的项目
             {
                 Console.WriteLine("test is Exists");
                 Console.WriteLine(mc.Get("test").ToString());  //在缓存中获取key为test的项目
             }
             else
             {
                 Console.WriteLine("test not Exists");
             }
 
             Console.ReadLine();
 
             mc.Delete("test");  //移除缓存中key为test的项目
 
             if (mc.KeyExists("test"))
             {
                 Console.WriteLine("test is Exists");
                 Console.WriteLine(mc.Get("test").ToString());
             }
             else
             {
                 Console.WriteLine("test not Exists");
             }
             Console.ReadLine();
             
             SockIOPool.GetInstance().Shutdown();  //关闭池, 关闭sockets
         }
        
    }
}
MemcacheTest

运行结果:

5:从Windows服务中卸载Memcache

  4.1 以管理员身份启动命令窗口

  4.2 定位到memcached.exe所在的文件夹

    命令: F:

       cd F:\memcache

      memcached -d stop

      memcached -d uninstall

 

posted @ 2017-03-29 10:12  逍遥小天狼  阅读(246)  评论(0编辑  收藏  举报