Memcached windows 下安装与测试
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/.
e.g.:
001 |
/** |
002 |
* MemcachedBench.cs |
003 |
* |
004 |
* Copyright (c) 2005 |
005 |
* Tim Gebhardt <tim@gebhardtcomputing.com> |
006 |
* |
007 |
* Based off of code written by |
008 |
* Greg Whalin <greg@meetup.com> |
009 |
* for his Java Memcached client: |
011 |
* |
012 |
* |
013 |
* See the memcached website: |
015 |
* |
016 |
* This module is Copyright (c) 2005 Tim Gebhardt. |
017 |
* All rights reserved. |
018 |
* |
019 |
* This library is free software; you can redistribute it and/or |
020 |
* modify it under the terms of the GNU Lesser General Public |
021 |
* License as published by the Free Software Foundation; either |
022 |
* version 2.1 of the License, or (at your option) any later |
023 |
* version. |
024 |
* |
025 |
* This library is distributed in the hope that it will be |
026 |
* useful, but WITHOUT ANY WARRANTY; without even the implied |
027 |
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
028 |
* PURPOSE. See the GNU Lesser General Public License for more |
029 |
* details. |
030 |
* |
031 |
* You should have received a copy of the GNU Lesser General Public |
032 |
* License along with this library; if not, write to the Free Software |
033 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
034 |
* |
035 |
* @author Tim Gebhardt<tim@gebhardtcomputing.com> |
036 |
* @version 1.0 |
037 |
*/ |
038 |
namespace Memcached.MemcachedBench |
039 |
{ |
040 |
using System; |
041 |
using System.Collections; |
042 |
|
043 |
using Memcached.ClientLibrary; |
044 |
|
045 |
public class MemcachedBench |
046 |
{ |
047 |
/// <summary> |
048 |
/// Arguments: |
049 |
/// arg[0] = the number of runs to do |
050 |
/// arg[1] = the run at which to start benchmarking |
051 |
/// </summary> |
052 |
/// <param name="args"></param> |
053 |
[STAThread] |
054 |
public static void Main(String[] args) |
055 |
{ |
056 |
int runs = 100; |
057 |
int start = 200; |
058 |
if (args.Length > 1) |
059 |
{ |
060 |
runs = int .Parse(args[0]); |
061 |
start = int .Parse(args[1]); |
062 |
} |
063 |
|
064 |
//可以设置多个服务器列表 |
065 |
//string[] serverlist = { "127.0.0.1:11211" , "140.192.34.73:11211" }; |
066 |
string [] serverlist = { "127.0.0.1:11211" }; //, "140.192.34.73:11211" }; |
067 |
|
068 |
// initialize the pool for memcache servers |
069 |
SockIOPool pool = SockIOPool.GetInstance(); |
070 |
pool.SetServers(serverlist); |
071 |
|
072 |
pool.InitConnections = 3; |
073 |
pool.MinConnections = 3; |
074 |
pool.MaxConnections = 5; |
075 |
|
076 |
pool.SocketConnectTimeout = 1000; |
077 |
pool.SocketTimeout = 3000; |
078 |
|
079 |
pool.MaintenanceSleep = 30; |
080 |
pool.Failover = true ; |
081 |
|
082 |
pool.Nagle = false ; |
083 |
pool.Initialize(); |
084 |
|
085 |
// initialize the pool for memcache servers |
086 |
// SockIOPool pool = SockIOPool.Instance; |
087 |
// pool.Servers = serverlist; |
088 |
// |
089 |
// pool.InitConn = 5; |
090 |
// pool.MinConn = 5; |
091 |
// pool.MaxConn = 50; |
092 |
// pool.MaintSleep = 30; |
093 |
// pool.SocketTO = 1000; |
094 |
// |
095 |
// pool.Nagle = false; |
096 |
// pool.Initialize(); |
097 |
|
098 |
// |
099 |
// // get client instance |
100 |
MemcachedClient mc = new MemcachedClient(); |
101 |
mc.EnableCompression = false ; |
102 |
|
103 |
// MemcachedClient mc = new MemcachedClient(); |
104 |
// mc.CompressEnable = false; |
105 |
// mc.CompressThreshold = 0; |
106 |
// mc.Serialize = true; |
107 |
|
108 |
string keyBase = "testKey" ; |
109 |
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" ; |
110 |
|
111 |
long begin = DateTime.Now.Ticks; |
112 |
for ( int i = start; i < start+runs; i++) |
113 |
{ |
114 |
mc.Set(keyBase + i, obj); |
115 |
} |
116 |
long end = DateTime.Now.Ticks; |
117 |
long time = end - begin; |
118 |
|
119 |
Console.WriteLine(runs + " 设置花费的时间-sets: " + new TimeSpan(time).ToString() + "ms" ); |
120 |
|
121 |
begin = DateTime.Now.Ticks; |
122 |
int hits = 0; |
123 |
int misses = 0; |
124 |
for ( int i = start; i < start+runs; i++) |
125 |
{ |
126 |
string str = ( string ) mc.Get(keyBase + i); |
127 |
Console.WriteLine( "key={0},value={1}" ,keyBase+i,str); |
128 |
if (str != null ) |
129 |
++hits; |
130 |
else |
131 |
++misses; |
132 |
} |
133 |
end = DateTime.Now.Ticks; |
134 |
time = end - begin; |
135 |
|
136 |
Console.WriteLine(runs + "读取花费的时间- gets: " + new TimeSpan(time).ToString() + "ms" ); |
137 |
Console.WriteLine( "Cache hits,成功: " + hits.ToString()); |
138 |
Console.WriteLine( "Cache misses,失败: " + misses.ToString()); |
139 |
|
140 |
IDictionary stats = mc.Stats(); |
141 |
foreach ( string key1 in stats.Keys) |
142 |
{ |
143 |
Console.WriteLine(key1); |
144 |
Hashtable values = (Hashtable)stats[key1]; |
145 |
foreach ( string key2 in values.Keys) |
146 |
{ |
147 |
Console.WriteLine(key2 + ":" + values[key2]); |
148 |
} |
149 |
Console.WriteLine(); |
150 |
} |
151 |
|
152 |
SockIOPool.GetInstance().Shutdown(); |
153 |
Console.ReadKey(); |
154 |
} |
155 |
} |
156 |
} |
服务器端: https://files.cnblogs.com/wucg/memcached-1.2.6-win32-bin.zip
下载Client库文件及示例,vs2008,.netframework 1.0,2.0 https://files.cnblogs.com/wucg/clientlib.zip