CHSHJ 发表于 2015-8-31 12:39:10

net下Memcached简介使用

http://www.iofeng.com/Ublog/ShowBlog.aspx?id=19  
  在数据驱动的web开发中,经常要重复从数据库中取出相同的数据,这种重复极大的增加了数据库负载。缓存是解决这个问题的好办法。但是ASP.NET中的虽然已经可以实现对页面局部进行缓存,但还是不够灵活。此时Memcached或许是你想要的。
  
  Memcached是什么?
  Memcached是由Danga Interactive开发的,高性能的,分布式的内存对象缓存系统,用于在动态应用中减少数据库负载,提升访问速度。
  
  Memcached能缓存什么?
  通过在内存里维护一个统一的巨大的hash表,Memcached能够用来存储各种格式的数据,包括图像、视频、文件以及数据库检索的结果等。
  
  Memcached快么?
  
  非常快。Memcached使用了libevent(如果可以的话,在linux下使用epoll)来均衡任何数量的打开链接,使用非阻塞的网络I/O,对内部对象实现引用计数(因此,针对多样的客户端,对象可以处在多样的状态), 使用自己的页块分配器和哈希表, 因此虚拟内存不会产生碎片并且虚拟内存分配的时间复杂度可以保证为O(1).。
  
  Danga Interactive为提升Danga Interactive的速度研发了Memcached。目前,LiveJournal.com每天已经在向一百万用户提供多达两千万次的页面访问。而这些,是由一个由web服务器和数据库服务器组成的集群完成的。Memcached几乎完全放弃了任何数据都从数据库读取的方式,同时,它还缩短了用户查看页面的速度、更好的资源分配方式,以及Memcache失效时对数据库的访问速度。
  
  Memcached的特点
  Memcached的缓存是一种分布式的,可以让不同主机上的多个用户同时访问, 因此解决了共享内存只能单机应用的局限,更不会出现使用数据库做类似事情的时候,磁盘开销和阻塞的发生。
  
  Memcached的使用
  一 Memcached服务器端的安装 (此处将其作为系统服务安装)
  下载文件:memcached-1.2.1-win32(可QQ咨询我发送)
  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的两种版本 还有一个不错的例子。(memcacheddotnet_clientlib-1.1.5 memcached的.NET客户端类库)
  
  三 应用
  
  1.新建ASP.NET站点,将Commons.dll,ICSharpCode.SharpZipLib.dll,log4net.dll,Memcached.ClientLibrary.dll添加到web引用。
  2.为了进行后续的测试,我们创建两个aspx页面,memcache.aspx和nomemcache.aspx,memcache.aspx是使用MemcacheClient类加入了缓存机制的页面。nomemcache.aspx是没有加入缓存机制的页面,直接连接的数据库。一会我们通过观察数据库事件和进行压力测试来测试在压力测试的情况下应用程序的性能。
  3.memcache.aspx.cs中添加对Memcached.ClientLibrary.dll的引用,即:using Memcached.ClientLibrary;
  加入的代码为:
  public partial class memcache : System.Web.UI.Page
  {
  protected void Page_Load(object sender, EventArgs e)
  {
  string[] serverlist = new string[] { "192.168.140.9:11211" };
  string poolName = "MemcacheIOPool";
  SockIOPool pool = SockIOPool.GetInstance(poolName);
  //设置连接池的初始容量,最小容量,最大容量,Socket 读取超时时间,Socket连接超时时间
  pool.SetServers(serverlist);
  pool.InitConnections = 1;
  pool.MinConnections = 1;
  pool.MaxConnections = 500;
  pool.SocketConnectTimeout = 1000;
  pool.SocketTimeout = 3000;
  pool.MaintenanceSleep = 30;
  pool.Failover = true;
  pool.Nagle = false;
  pool.Initialize();//容器初始化
  //实例化一个客户端
  MemcachedClient mc = new MemcachedClient();
  mc.PoolName = poolName;
  mc.EnableCompression = false;
  string key = "user_info";//key值
  object obj = new object();
  if (mc.KeyExists(key)) //测试缓存中是否存在key的值
  {
  obj = mc.Get(key);
  User user2 = (User)obj;
  Response.Write("" + user2.Name + "," + user2.Pwd + "");
  }
  else
  {
  string conStr =System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
  SqlConnection conn = new SqlConnection(conStr);
  conn.Open();
  string sql = "Select * From UserInfo Where id='US200906052323'";
  SqlCommand cmd = new SqlCommand(sql, conn);
  SqlDataReader dr = cmd.ExecuteReader();
  User user = new User();
  while (dr.Read())
  {
  user.Name = dr["name"].ToString();
  user.Pwd = dr["pwd"].ToString();
  }
  dr.Close();
  conn.Close();
  mc.Set(key, user, System.DateTime.Now.AddMinutes(2)); //存储数据到缓存服务器,这里将user这个对象缓存,key 是"user_info1"
  Response.Write("姓名:" + user.Name + ",密码:" + user.Pwd + "");
  }
  }
  
  }
  
  public class User
  {
  string id;
  public string Id
  {
  get { return id; }
  set { id = value; }
  }
  string name;
  public string Name
  {
  get { return name; }
  set { name = value; }
  }
  string pwd;
  public string Pwd
  {
  get { return pwd; }
  set { pwd = value; }
  }
  }
  4.nomemcache.aspx是没有加缓存机制的直接连接数据库的页面。nomemcache.aspx.cs中的代码:
  public partial class nomemcache : System.Web.UI.Page
  {
  protected void Page_Load(object sender, EventArgs e)
  {
  string conStr = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
  SqlConnection conn = new SqlConnection(conStr);
  conn.Open();
  string sql = "Select * From UserInfo Where id='US20100623032641'";
  SqlCommand cmd = new SqlCommand(sql, conn);
  SqlDataReader dr = cmd.ExecuteReader();
  User user = new User();
  while (dr.Read())
  {
  user.Name = dr["name"].ToString();
  user.Pwd = dr["pwd"].ToString();
  }
  dr.Close();
  conn.Close();
  Response.Write("姓名:" + user.Name + ",密码:" + user.Pwd + "");
  }
  }
  5.总结:memcache.aspx页面第一次请求数据库,后面找memcache.aspx页面,如果有就不取DB了。而nomemcache.aspx页面需要每次取DB.
  以上内容根据:http://tech.it168.com/a2009/0907/675/000000675239.shtml而总结。
  
  源码下载链接:http://u.115.com/file/f0edeb5460#TestMemcached.zip
  提取码:f0edeb5460
页: [1]
查看完整版本: net下Memcached简介使用