C#操作Memcache

软件从单机到分布式

走向分布式就要解决:多台机器共享登录信息的问题。

解决1:AspNet 进程外的Session。
解决2:用数据库存储当前登录状态。
解决3:Memcache 【性能最好,类似的:Redis,NoSql】

memcache 是一个分布式的缓存系统,但是本身没有提供集群功能,数据也是键值对存储Memcache服务器端并没有提供集群功能,但是通过客户端的驱动程序实现了集群配置

why memcache ?

1,高并发访问数据库的痛楚:死锁!

2磁盘IO之痛:

3多客户端共享缓存
4Net + Memory  >> IO    
5读写性能完美  1s:读取可以1w次。 写:10w
6超简单集群搭建
7开源 Open Source
8没有提供主从赋值功能,也没提供容灾等功能,所以所有的代码基本都只是考虑性能最佳。(Redis)
9学习成本非常低,入门非常容易

memcache搭建小例子

下载Memcache

安装服务:cmd→Memcached.exe -d install 

启动服务:cmd→Memcached.exe -d start

检查服务是否启动:连接到Memcache控制台:telnet ServerIP 11211  输入命令:stats检查当前服务状态。

C#操作Memcache代码

1.添加程序集引用
http://sourceforge.net/projects/memcacheddotnet/

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

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" };//ip地址
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("------------测 试Memcache----------");
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();   // 关闭池
65         }
66     }
67 }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值