1、首先服务器上要安装memcached服务。下载地址:http://pan.baidu.com/s/1o8rK8cE
2、下载Memcached.ClientLibrary.dll添加引用。下载地址:http://pan.baidu.com/s/1eR5XJLw
static void Main(string[] args)
{
//初始化memcached 服务器端集群列表。
String[] serverlist = { “127.0.0.1:11211”};
// initialize the pool for memcache servers
SockIOPool pool = SockIOPool.GetInstance(“test”);
//设置怎么mem池连接点服务器端。
pool.SetServers(serverlist);
pool.Initialize();
//创建了一个mem客户端的代理类。
var mc = new MemcachedClient();
mc.PoolName = “test”;
mc.EnableCompression = false;
//mc.Add(“rj”, “rj”);
mc.Set(“test”, “123”, DateTime.Now.AddSeconds(15));
pool.Shutdown();//关闭连接池
Console.ReadKey();
}
3、没什么可以说的,就是这么简单的令人发指.一般情况下我会对memcached进行简单的封装。下面是一个封装示例,也很简单。
public static class MemcacheHelper
{
private static MemcachedClient mc;
static MemcacheHelper()
{
String[] serverlist = { “127.0.0.1:11211” };
// initialize the pool for memcache servers
SockIOPool pool = SockIOPool.GetInstance(“test”);
pool.SetServers(serverlist);
pool.Initialize();
mc = new MemcachedClient();
mc.PoolName = “test”;
mc.EnableCompression = false;
}
public static bool Set(string key, object value,DateTime expiry){
return mc.Set(key, value, expiry);
}
public static bool Set(string key, object value)
{
return mc.Set(key, value);
}
public static object Get(string key)
{
return mc.Get(key);
}
}
4.
Windows下使用Memcache的帮助
将Memcache.exe安装为Windows服务:Memcache.exe-d install
启动Memcache服务:Memcache.exe -d start
启动Memcache服务(windows命令):net start”Memcache Server”
停止Memcache服务(windows命令):net stop”Memcache Server”
连接到Memcache控制台:telnet ServerIP11211
打印当前Memcache服务器状态:stats
打印当前Memcache服务器Items(记录)的统计信息:statsitems
打印当前Memcache服务器Slab(分区)及Chunk(块)的统计信息:statsslabs
打印指定Slab中的KEY列表(可用于遍历items,但效率较低,慎用!):statscachedump SlabId Limit_num。显示结果:ITEM KeyName [ValueByteLength b; LastAccessTimes]。值得注意的是,经过测试确认:那个LastAccessTime并不是记录到期时间,而是最后一次的get时间,并且get之后,也不会自动延长expiry(到期时间)。
添加新记录:add KeyName 0 0ValueByteLength [回车] ValueContent
删除记录 : delete KeyName
添加或更新记录 : set KeyName 0 0ValueByteLength [回车] ValueContent
更新记录 : replace KeyName 0 0ValueByteLength [回车] ValueContent
参考:http://www.cnblogs.com/lost-1987/articles/3069460.html
http://wenku.baidu.com/view/e30db586ec3a87c24028c401.html
也可以图形化监控 Memcached 的运行状态
http://livebookmark.net/journal/2008/05/21/memcachephp-stats-like-apcphp/
在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为端口),再启动服务。