Memcached windows 下安装与测试

来源:互联网 发布:java记录访问次数 编辑:程序博客网 时间:2024/05/26 12:57
Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载。它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提供动态、数据库驱动网站的速度。Memcached基于一个存储键/值对的hashmap。其守护进程(daemon )是用C写的,但是客户端可以用任何语言来编写,并通过memcached协议与守护进程通信。但是它并不提供冗余(例如,复制其hashmap条目);当某个服务器S停止运行或崩溃了,所有存放在S上的键/值对都将丢失。

Memcached官方:http://danga.com/memcached/

关于Memcached的介绍请参考:Memcached深度分析

下载Windows的Server端

下载地址:http://code.jellycan.com/memcached/

安装Memcache Server(也可以不安装直接启动)

1. 下载memcached的windows稳定版,解压放某个盘下面,比如在c:\memcached
2. 在CMD下输入 "c:\memcached\memcached.exe -d install" 安装.
3. 再输入:"c:\memcached\memcached.exe -d start" 启动。NOTE: 以后memcached将作为windows的一个服务每次开机时自动启动。这样服务器端已经安装完毕了。

如果下载的是二进制的版本,直接运行就可以了,可以加上参数来加以设置。


常用设置:
-p <num>          监听的端口
-l <ip_addr>      连接的IP地址, 默认是本机
-d start          启动memcached服务
-d restart        重起memcached服务
-d stop|shutdown  关闭正在运行的memcached服务
-d install        安装memcached服务
-d uninstall      卸载memcached服务
-u <username>     以<username>的身份运行 (仅在以root运行的时候有效)
-m <num>          最大内存使用,单位MB。默认64MB
-M                内存耗尽时返回错误,而不是删除项
-c <num>          最大同时连接数,默认是1024
-f <factor>       块大小增长因子,默认是1.25
-n <bytes>        最小分配空间,key+value+flags默认是48
-h                显示帮助

然后就可以用.net 的memcached客户端来试一下了。

C# 下可用的API(每个客户端API中都有详细的说明和注释)

https://sourceforge.net/projects/memcacheddotnet/
http://www.codeplex.com/EnyimMemcached/ - Client developed in .NET 2.0 keeping performance and extensibility in

mind. (Supports consistent hashing.) 
http://code.google.com/p/beitmemcached/ - Client developed by BeIT with many new features

转载出处: http://www.yaosansi.com/

----------------------------------------------------------------------------------------

Client调用:

下载示例代码网址:http://sourceforge.net/projects/memcacheddotnet/

C#/.NET memcached client library. This library can be used by .NET projects to access memcached servers. Ported from the Java memcached library located athttp://www.whalin.com/memcached/.

/**  * MemcachedBench.cs  *  * Copyright (c) 2005  * Tim Gebhardt <tim@gebhardtcomputing.com>  *   * Based off of code written by  * Greg Whalin <greg@meetup.com>  * for his Java Memcached client:  * http://www.whalin.com/memcached/  *   *  * See the memcached website:  * http://www.danga.com/memcached/  *  * This module is Copyright (c) 2005 Tim Gebhardt.  * All rights reserved.  *  * This library is free software; you can redistribute it and/or  * modify it under the terms of the GNU Lesser General Public  * License as published by the Free Software Foundation; either  * version 2.1 of the License, or (at your option) any later  * version.  *  * This library is distributed in the hope that it will be  * useful, but WITHOUT ANY WARRANTY; without even the implied  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR  * PURPOSE.  See the GNU Lesser General Public License for more  * details.  *  * You should have received a copy of the GNU Lesser General Public  * License along with this library; if not, write to the Free Software  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA  *  * @author Tim Gebhardt<tim@gebhardtcomputing.com>   * @version 1.0  */namespace Memcached.MemcachedBench {     using System;     using System.Collections;       using Memcached.ClientLibrary;       public class MemcachedBench      {         /// <summary>         /// Arguments:          ///     arg[0] = the number of runs to do         ///     arg[1] = the run at which to start benchmarking         /// </summary>         /// <param name="args"></param>         [STAThread]         public static void Main(String[] args)          {             int runs = 100;             int start = 200;             if(args.Length > 1)             {                 runs = int.Parse(args[0]);                 start = int.Parse(args[1]);             }               //可以设置多个服务器列表             //string[] serverlist = { "127.0.0.1:11211" , "140.192.34.73:11211" };             string[] serverlist = { "127.0.0.1:11211" };    //, "140.192.34.73:11211" };               // initialize the pool for memcache servers             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();               // initialize the pool for memcache servers //          SockIOPool pool = SockIOPool.Instance; //          pool.Servers = serverlist; // //          pool.InitConn = 5; //          pool.MinConn = 5; //          pool.MaxConn = 50; //          pool.MaintSleep = 30; //          pool.SocketTO = 1000; // //          pool.Nagle = false; //          pool.Initialize();   // //          // get client instance             MemcachedClient mc = new MemcachedClient();             mc.EnableCompression = false;   //          MemcachedClient mc = new MemcachedClient(); //          mc.CompressEnable = false; //          mc.CompressThreshold = 0; //          mc.Serialize = true;               string keyBase = "testKey";             string obj = "这是我的字符串This is a test of an object blah blah es, serialization does not seem to slow things down so much.  The gzip compression is horrible horrible performance, so we only use it for very large objects.  I have not done any heavy benchmarking recently";               long begin = DateTime.Now.Ticks;             for(int i = start; i < start+runs; i++)              {                 mc.Set(keyBase + i, obj);             }             long end = DateTime.Now.Ticks;             long time = end - begin;               Console.WriteLine(runs + " 设置花费的时间-sets: " + new TimeSpan(time).ToString() + "ms");               begin = DateTime.Now.Ticks;             int hits = 0;             int misses = 0;             for(int i = start; i < start+runs; i++)              {                 string str = (string) mc.Get(keyBase + i);                 Console.WriteLine("key={0},value={1}",keyBase+i,str);                 if(str != null)                     ++hits;                 else                    ++misses;             }             end = DateTime.Now.Ticks;             time = end - begin;               Console.WriteLine(runs + "读取花费的时间- gets: " + new TimeSpan(time).ToString() + "ms");             Console.WriteLine("Cache hits,成功: " + hits.ToString());             Console.WriteLine("Cache misses,失败: " + misses.ToString());               IDictionary stats = mc.Stats();             foreach(string key1 in stats.Keys)             {                 Console.WriteLine(key1);                 Hashtable values = (Hashtable)stats[key1];                 foreach(string key2 in values.Keys)                 {                     Console.WriteLine(key2 + ":" + values[key2]);                 }                 Console.WriteLine();             }               SockIOPool.GetInstance().Shutdown();             Console.ReadKey();         }     } }

服务器端:http://files.cnblogs.com/wucg/memcached-1.2.6-win32-bin.zip

下载Client库文件及示例,vs2008,.netframework 1.0,2.0 http://files.cnblogs.com/wucg/clientlib.zip


 

 

 另外一篇文章有详细介绍:http://hi.baidu.com/qkyh1225/item/ef4d3fdfd63a5fed3dc2cbb0

 1. 下载windows版本的Memcached:http://code.jellycan.com/memcached/



2.解压之后放在硬盘的目录下,如:D:\memcached . 然后在运行中输入cmd进入命令行,进入到Memcached.exe 所在的目录,例如:D:\memcached,然后输入:


Cmd代码  收藏代码
到memcached根目录
1、安装
memcached.exe –d install 

2、启动
memcached.exe -d start

此时memcached已经注册为开机启动服务

完成安装。

 

 经实验使用命令修改端口无效,相应的解决方案是修改注册表:

通过修改注册表来达到这个修改端口的目的。

在 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\memcached Server 下面找到一个 ImagePath 的字符串项,正好是服务的执行路径的字符串,双击该串,在后面加入 -l 192.168.1.135 -m 45 -p 12345 (访问ip为:192.168.1.135 使用45M内存,12345为端口),再启动服务。




现在来试试是否可以连接:

使用方法为“telnet ip 端口号”,登录后使用“stats”命令
cmd下
telnet 192.168.1.135 12345(注意:这里只能用IP 不能用 localhost 或127.0.0.1)

然后使用:stats命令可以看到memcached的使用情况如下:

STAT uptime 760
STAT time 1311913149
STAT version 1.2.1
STAT pointer_size 32
STAT curr_items 0
STAT total_items 0
STAT bytes 0
STAT curr_connections 4
STAT total_connections 6
STAT connection_structures 5
STAT cmd_get 0
STAT cmd_set 0
STAT get_hits 0
STAT get_misses 0
STAT bytes_read 63
STAT bytes_written 793
STAT limit_maxbytes 10485760
END




3、memcached的基本设置 :



-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 显示帮助



4、最主要的一点,在windows下安装memcache通过命令行修改参数好像不起什么作用,需要进入注册表修改启动项参数才有用,具体操作如下:进入注册表,找到HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\memcached Server , 在其中有一个ImagePath 项,值为"d:\memcached\memcached.exe" -d runservice ,在后面加上-l 127.0.0.1 -m 3000 -c 2048 最后ImagePath 项值应该像这样:


注册表值代码  收藏代码

   1. d:\memcached\memcached.exe" -d runservice -l 127.0.0.1 -m 3000 -c 2048 

d:\memcached\memcached.exe" -d runservice -l 127.0.0.1 -m 3000 -c 2048



5、memcached –d start 启动memcached服务,这样就将memcached的最大内存修改为了3000MB。



对Memcached缓存服务的状态查询,可以先telnet连接上服务:telnet 127.0.0.1 11211 ,然后使用 stats命令查看缓存服务的状态,会返回如下的数据:



time: 1255537291 服务器当前的unix时间戳


total_items: 54 从服务器启动以后存储的items总数量


connection_structures: 19 服务器分配的连接构造数


version: 1.2.6 memcache版本


limit_maxbytes: 67108864 分配给memcache的内存大小(字节)


cmd_get: 1645 get命令(获取)总请求次数


evictions: 0 为获取空闲内存而删除的items数(分配给memcache的空间用满后需
要删除旧的items来得到空间分配给新的items)


total_connections: 19 从服务器启动以后曾经打开过的连接数


bytes: 248723 当前服务器存储items占用的字节数


threads: 1 当前线程数


get_misses: 82 总未命中次数


pointer_size: 32 当前操作系统的指针大小(32位系统一般是32bit)


bytes_read: 490982 总读取字节数(请求字节数)


uptime: 161 服务器已经运行的秒数


curr_connections: 18 当前打开着的连接数


pid: 2816 memcache服务器的进程ID


bytes_written: 16517259 总发送字节数(结果字节数)


get_hits: 1563 总命中次数


cmd_set: 54 set命令(保存)总请求次数


curr_items: 28 服务器当前存储的items数量



关于memcache 的一下疑问:

memcached能接受的key的最大长度是多少?
key 的最大长度是250个字符。需要注意的是,250是memcached服务器端内部的限制,如果您使用的客户端支持”key的前缀”或类似特性,那么 key(前缀+原始key)的最大长度是可以超过250个字符的。我们推荐使用使用较短的key,因为可以节省内存和带宽。

memcached对item的过期时间有什么限制?
过期时间最大可以达到30天。memcached把传入的过期时间(时间段)解释成时间点后,一旦到了这个时间点,memcached就把item置为失效状态。这是一个简单但obscure的机制。

memcached最大能存储多大的单个item?
1MB。如果你的数据大于1MB,可以考虑在客户端压缩或拆分到多个key中。

为什么单个item的大小被限制在1M byte之内?
啊…这是一个大家经常问的问题!

简单的回答:因为内存分配器的算法就是这样的

 

 

 

原创粉丝点击