分布式缓存系统Memcached简介与实践

Memcached的使用
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 可以查看其帮助
二   .NET memcached client library
   下载文件:https://sourceforge.net/projects/memcacheddotnet/

   里面有.net1.1  和 .net2.0的两种版本  还有一个不错的例子。

三  应用

  1 将Commons.dll,ICSharpCode.SharpZipLib.dll,log4net.dll,Memcached.ClientLibrary.dll 等放到bin目录
  2 引用
Memcached.ClientLibrary.dll
  3 代码

 1 namespace Memcached.MemcachedBench
 2 {
 3     using System;
 4     using System.Collections;
 5 
 6     using Memcached.ClientLibrary;
 7 
 8     public class MemcachedBench 
 9     {
10         [STAThread]
11         public static void Main(String[] args) 
12         {
13             string[] serverlist = { "10.0.0.131:11211""10.0.0.132:11211" };
14 
15             //初始化池
16             SockIOPool pool = SockIOPool.GetInstance();
17             pool.SetServers(serverlist);
18 
19             pool.InitConnections = 3;
20             pool.MinConnections = 3;
21             pool.MaxConnections = 5;
22 
23             pool.SocketConnectTimeout = 1000;
24             pool.SocketTimeout = 3000;
25 
26             pool.MaintenanceSleep = 30;
27             pool.Failover = true;
28 
29             pool.Nagle = false;
30             pool.Initialize();
31 
32             // 获得客户端实例
33             MemcachedClient mc = new MemcachedClient();
34             mc.EnableCompression = false;
35 
36             Console.WriteLine("------------测  试-----------");
37             mc.Set("test""my value");  //存储数据到缓存服务器,这里将字符串"my value"缓存,key 是"test"
38 
39             if (mc.KeyExists("test"))   //测试缓存存在key为test的项目
40             {
41                 Console.WriteLine("test is Exists");
42                 Console.WriteLine(mc.Get("test").ToString());  //在缓存中获取key为test的项目
43             }
44             else
45             {
46                 Console.WriteLine("test not Exists");
47             }
48 
49             Console.ReadLine();
50 
51             mc.Delete("test");  //移除缓存中key为test的项目
52 
53             if (mc.KeyExists("test"))
54             {
55                 Console.WriteLine("test is Exists");
56                 Console.WriteLine(mc.Get("test").ToString());
57             }
58             else
59             {
60                 Console.WriteLine("test not Exists");
61             }
62             Console.ReadLine();
63             
64             SockIOPool.GetInstance().Shutdown();  //关闭池, 关闭sockets
65         }
66     }
67 }

 

 

 

 

static void Main(string[] args)
{
string[] serverlist = { "192.168.0.129:11211" };
//初始化池
SockIOPool sock = SockIOPool.GetInstance();
sock.SetServers(serverlist);

sock.InitConnections = 3;
sock.MinConnections = 3;
sock.MaxConnections = 5;

sock.SocketConnectTimeout = 1000;
sock.SocketTimeout = 3000;

sock.MaintenanceSleep = 30;
sock.Failover = true;

sock.Nagle = false;
sock.Initialize();

//获取客户端实例

MemcachedClient memcached = new MemcachedClient();
memcached.EnableCompression = false;

Console.WriteLine("-------Memcached测试-------");
memcached.Set("key1", "Value1");
if (memcached.KeyExists("key1"))
{
Console.WriteLine("存在key1..");
Console.WriteLine(memcached.Get("key1").ToString());
}
else
{
Console.WriteLine("不存在key1键");
}

Console.Read();
SockIOPool.GetInstance().Shutdown(); //关闭池, 关闭sockets
}

 

posted @ 2011-09-11 12:41  qinyi  阅读(545)  评论(0编辑  收藏  举报