一 Memcached服务器端的安装 (此处将其作为系统服务安装)
下载文件:memcached 1.2.1 for Win32 binaries (Dec 23, 2006)
1 解压缩文件到c:\memcached
2 命令行输入 'c:\memcached\memcached.exe -d install'
3 命令行输入 'c:\memcached\memcached.exe -d start' ,该命令启动 Memcached ,默认监听端口为 11211
通过 memcached.exe -h 可以查看其帮助
注册为服务呢,通过SC命令我们可以创建服务
sc create memDemo binpath= "C:\memcache\memcached.exe -d runservice -m 10240 -c 1000 -p 11216" start= auto displayname= "memDemo"
修改服务
sc config memDemo binpath= "C:\memcache\memcached.exe -d runservice -m 6144 -c 1000 -p 11910" start= auto displayname= "memDemo"
-d选项是启动一个守护进程,-m是分配给memcached使用的内存数量,-u是运行memcached的用户数,-i是监听的服务器IP,-p是端口,-c是连接并发数。
整个卸载
1.memcached.exe -d stop
2.memcached.exe -d uninstall
卸载某个实例
sc delete memDemo
修改某个实例
sc \\127.0.0.1 config memDemo binpath= "C:\memcached\memcached.exe -d runservice -m 64 -c 128 -p 11210 -I 10485760" start= auto displayname= "memDemo"
stats slabs
显示各个slab的信息,包括chunk的大小、数目、使用情况等
stats items
显示各个slab中item的数目和最老item的年龄(最后一次访问距离现在的秒数)
stats detail [on|off|dump]
设置或者显示详细操作记录
telnet 127.0.0.1 11410 进行连接
memlist 一般都是600MB,memproduct 一般是1600MB.
步骤:
1.窗口+R:输入cmd 2.进行G盘: 输入 G: 3.进行Memcached for window 32/64的安装目录:输入 cd CK\memcached_en32or64\x64 4.安装memcached:输入 memcached -d install 5.启动服务:输入 memcached -d start
启动服务后在Windows进程中可以看到memcached.exe.
memcached -d start|stop|shutdown|restart|uninstall|install 启动|停止|关闭|重启|卸载|安装。
安装步骤不是很复杂。第一:找到文件(memcached.exe)路径。第二: memcached -d install 就OK..
基本默认参数说明:
-p 监听的端口
-l 连接的IP地址, 默认是本机
-d start 启动memcached服务
-d restart 重起memcached服务
-d stop|shutdown 关闭正在运行的memcached服务
-d install 安装memcached服务
-d uninstall 卸载memcached服务
-u 以的身份运行 (仅在以root运行的时候有效)
-m 最大内存使用,单位MB。默认64MB -M 内存耗尽时返回错误,而不是删除项
-c 最大同时连接数,默认是1024
-f 块大小增长因子,默认是1.25
-n 最小分配空间,key+value+flags默认是48
-h 显示帮助
服务器操作完成后,我们可以在本机telnet 到服务测试一个下。(如果提示telnet命令不存在,需要去控件面板开启windows的tel服务功能, win7的开启tel功能操作步骤是:【控制面板】->【程序和功能】->【打开或关闭window功能】,然后找到并勾选tel相关即可。其他window系统步骤类似。)
测试telnet是否正常运行 telnet 172.21.0.192 11211
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 MemcacheStoreMethod(); 6 } 7 8 /// <summary> 9 /// Memcached基础方法 10 /// </summary> 11 static void MemcacheBasisMethod() 12 { 13 //参数设置 14 string SockIOPoolName = "Test_SockIOPoolName"; 15 string[] MemcacheServiceList = { "172.21.0.192:11211" }; 16 17 //设置连接池 18 SockIOPool SPool = SockIOPool.GetInstance(SockIOPoolName); 19 SPool.SetServers(MemcacheServiceList); 20 SPool.Initialize(); 21 22 //实例化Client 23 MemcachedClient MClient = new MemcachedClient(); 24 MClient.PoolName = SockIOPoolName; 25 26 Console.WriteLine("1.创建memcache缓存Hello World"); 27 MClient.Add("Key1001", "Hello World"); 28 Console.WriteLine("2.查询缓存信息{0}", MClient.Get("Key1001")); 29 30 Console.WriteLine("3.修改memcache缓存Hello World"); 31 MClient.Set("Key1001", "Hello World - 修改版"); 32 Console.WriteLine("4.查询缓存信息{0}", MClient.Get("Key1001")); 33 34 35 if (MClient.KeyExists("Key1001")) 36 { 37 Console.WriteLine("5.删除memcache缓存"); 38 MClient.Delete("Key1001"); 39 } 40 41 if (MClient.KeyExists("Key1001")) 42 Console.WriteLine(MClient.Get("Key1001")); 43 else 44 Console.WriteLine("6.删除已删除"); 45 46 } 47 48 #region Memcached存储数据测试 49 static void MemcacheStoreMethod() 50 { 51 //参数 52 string[] MemcacheServiceList = { "172.21.0.192:11211", "172.21.0.28:11211", "172.21.13.246:11211" }; 53 54 //设置连接池 55 SockIOPool SPool = SockIOPool.GetInstance(); 56 SPool.SetServers(MemcacheServiceList); 57 SPool.Initialize(); 58 59 MemcachedClient MClient = new MemcachedClient(); 60 MClient.FlushAll(); 61 62 int count = 5; 63 var time = Stopwatch.StartNew(); 64 for (int i = 0; i < count; i++) 65 { 66 MClient.Add(i.ToString(), "value" + i); 67 } 68 Console.WriteLine("memcached缓存创建成功。耗时:{0}",time.ElapsedTicks); 69 70 71 time = Stopwatch.StartNew(); 72 for (int i = 0; i < count; i++) 73 { 74 if (MClient.KeyExists(i.ToString())) 75 { 76 Console.WriteLine("key:{0}.value:{1}", i, MClient.Get(i.ToString())); 77 } 78 else 79 { 80 Console.WriteLine("--------未能查询到数据key:{0}--------",i); 81 } 82 } 83 Console.WriteLine("memcached缓存数据查询完成。耗时:{0}", time.ElapsedTicks); 84 85 SPool.Shutdown(); 86 } 87 88 #endregion 89 90 }