Memcached是开源.高性能内存对象缓存系统。Memcached是以LiveJournal旗下Danga Interactive公司的Brad Fitzpatric为首开发的一款软件。现在已成为mixi、hatena、Facebook、Vox、LiveJournal等众多服务中提高Web应用扩展性的重要因素。  MemCached是一种基于内存的key-value存储,用来存储小块的任意数据。实现原理就是,第一次从数据库获取到结果之后,同时将结果保存在内存中,从而简单对数据库的访问次数,以提高Web应用的速度。

一、准备:

  Memcaced服务端下载地址: http://files.cnblogs.com/sjns/memcached_en32or64.rar

  Memcaced 客户端类库:http://files.cnblogs.com/sjns/Memcached.ClientLibrary.rar

二、安装Memcaced服务端

  将memcached.exe复制到任意目录下。用cmd命令安装。(注:再win7下运行“命令提示符”要用管理身份运行

  安装:memcached.exe -d install

  启动:memcached.exe -d start

  卸载:memcached.exe -d uninstall

三、创建程序,我在这里用的是控制台应用程序。

  把Memcaced 客户端类库添加到引用中来。

    

      SockIOPool是Memcached客户端提供的一个套接字连接池,通俗讲,就是与Memcached服务器端交换数据的对象。

      注: SockIOPool在应用程序启动时初始化一次就可以了,我们可以把这个工作放在 GLOBAL.ASAX.CS的Application_Start方法里。

  好了,接下来就是我的代码了很的一个Demo

 

  

复制代码

static void Main(string[] args)
        {         
           char[] separator = { ',' };       
            string[] serverlist = new string[] { "127.0.0.1:11211" };  
          try
            {
                SockIOPool pool = SockIOPool.GetInstance();
                pool.SetServers(serverlist);

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

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

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

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

                MemcachedClient mc = new MemcachedClient();
                mc.Set("123", "123123123");

                Console.WriteLine("输入要查询的KEY");                string key = Console.ReadLine();
                Console.WriteLine(mc.Get(key));
                Console.WriteLine("输入要修改的KEY");
                String ukey = Console.ReadLine();
                Console.WriteLine("输入要修改的Value");
                String uval = Console.ReadLine();
                Console.WriteLine(mc.Set(ukey, uval));
                Console.WriteLine("key:" + ukey + "的值已修改。如下:");
                Console.WriteLine("value:" + uval);
            }            catch (Exception err)
            {                //这里就可以用Log4Net记录Error啦!
            }
        }

复制代码

下面是运行效果:

 

                                   





文章来源于:https://www.cnblogs.com/sjns/p/3973892.html。