码迷,mamicode.com
首页 > 系统相关 > 详细

Memcached的使用

时间:2017-05-07 16:49:29      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:我的电脑   time   library   安装包   相关   new   nagle   lov   server   

MemCache的简单介绍
      高性能的分布式内存对象缓存系统,读写速度非常快,解决了IO读写慢的痛苦,它主要是考虑的性能,没有容灾功能,当缓存服务器挂掉后,数据将会全部的丢失。Memcache中的key的值最大是255字符,会将可以用的内存进行分区,然后在对每一个区域进行分块,每个块有1M,存放的数据最好不要超过1M,当一个缓存数据存放在一个块中,下一个数据将会存放在新的块中,所以存在一定的资源的浪费
     Memcache中没有缓存依赖和时间检测,而是采用的惰性删除,当查询数据的时候,查看数据是否过期,如果过期,将数据删除,数据删除只是删除数据不会删除内存中的快,这样可以避免内存碎片的产生。当内存用完了之后,新的数据存进来的时候,首先检查是否存在空闲的内存块,但是没有的时候,在检查是否有数据鼓起如果过期,则将该数据删除,然后存入新的数据,如果没有过期的数据,则会将用的次数比较少的内存块中的数据删除,存入新的数据。
    对于缓存服务器的选择,当存在多台缓存服务器的时候,采用的算法:先将键值进行哈希运算,将运算后的结果跟缓存服务器的数据量取余数,就是要存储的地方。

MemCache的安装

1.Memcached是一套分布式缓存系统,需要下载安装包,运行相应的服务,为了避免每次启动的时候,都得手动开启Memcache,所以需要将memcache安装到windows服务,设置自动启动。

(1)打开cmd窗口,使用管理员的身份运行,定位到memcached.exe所在的文件夹(cd)

(2)输入安装指令 memcached.exe -d install

            (卸载指令:。。。。uninstall  开启指令:start 重启指令:restart 关闭指令stop)

(3)可以通过右击我的电脑-管理-服务和应用程序,通过微软的界面查看服务

(4)通过telnet(远程通信工具),测试与memcached的服务器的通信情况,通过控制面板中的程序和功能,开启telnet工具(只是开启客户端就够了)

(5)通过dos指令 telnet 127.0.0.1(memcache的电脑的ip) 11211(memcache默认的端口号),进入telnet客户端和memcache服务器的通信界面

(6)输入指令stats可以获取memcache的一些信息,判断是否安装成功

Memecache的使用

1.安装完成后,接下就是 下载.net使用memcache所需要的相关的类库Commons.dll,ICSharpCode.SharpZipLib.dll,log4net.dll,Memcached.ClientLibrary.dll

2.新建一个控制台应用程序

3.测试代码

 1 namespace MemcacheDemo
 2 {
 3     class Program
 4     {
 5         static void Main(string[] args)
 6         {//分布式集群设置,采用的是socket会使用三次握手,如果没有改server,则会将其放弃
 7                string[] serverlist = { "127.0.0.1:11211", "10.0.0.132:11211" };
 8  
 9              //初始化池
10              SockIOPool pool = SockIOPool.GetInstance();
11              pool.SetServers(serverlist);
12  
13              pool.InitConnections = 3;
14             pool.MinConnections = 3;
15              pool.MaxConnections = 5;
16  
17              pool.SocketConnectTimeout = 1000;
18              pool.SocketTimeout = 3000;
19  
20             pool.MaintenanceSleep = 30;
21              pool.Failover = true;
22  
23              pool.Nagle = false;
24              pool.Initialize();
25  
26              // 获得客户端实例
27              MemcachedClient mc = new MemcachedClient();
28              mc.EnableCompression = false;
29  
30              Console.WriteLine("------------测  试-----------");
31              mc.Set("test", "my value");  //存储数据到缓存服务器,这里将字符串"my value"缓存,key 是"test"
32  
33              if (mc.KeyExists("test"))   //测试缓存存在key为test的项目
34             {
35                  Console.WriteLine("test is Exists");
36                 Console.WriteLine(mc.Get("test").ToString());  //在缓存中获取key为test的项目
37             }
38              else
39             {
40                 Console.WriteLine("test not Exists");
41             }
42  
43             Console.ReadLine();
44 
45              mc.Delete("test");  //移除缓存中key为test的项目
46  
47              if (mc.KeyExists("test"))
48             {
49                 Console.WriteLine("test is Exists");
50                  Console.WriteLine(mc.Get("test").ToString());
51             }
52              else
53              {
54                 Console.WriteLine("test not Exists");
55              }
56             Console.ReadLine();
57              
58              SockIOPool.GetInstance().Shutdown();  //关闭池, 关闭sockets
59 
60         }
61     }
62 }

Memcache的封装

public  class MemcacheHelper
    {
     private static MemcachedClient mc;
     public static MemcacheHelper()
     {
         //分布式集群设置,采用的是socket会使用三次握手,如果没有改server,则会将其放弃
        //这个应该放置在配置文件.

         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();
         // 获得客户端实例
         mc = new MemcachedClient();
         mc.EnableCompression = false;
 
     }
     public static bool Set(string key, object value)
     {
        return mc.Set(key, value);
         
     }
     public static bool Set(string key, object value, DateTime dateTime)
     {
         return mc.Set(key, value, dateTime);

     }
     public static object Get(string key)
     {
         return mc.Get(key);
     }
     public static bool Delete(string key)
     {
         return mc.Delete(key);
     }
     public static bool Add(string key, object value)
     {
         return mc.Add(key, value);
     }
     public static bool Add(string key, object value, DateTime dateTime)
     {
         return mc.Add(key, value, dateTime);
     }
    }

 

Memcached的使用

标签:我的电脑   time   library   安装包   相关   new   nagle   lov   server   

原文地址:http://www.cnblogs.com/XZhao/p/6821121.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!