博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

分布式缓存系统Memcached学习心得

Posted on 2009-01-14 11:34  linFen  阅读(4389)  评论(1编辑  收藏  举报
缘起: 在数据驱动的web开发中,经常要重复从数据库中取出相同的数据,这种重复极大的增加了数据库负载。缓存是解决这个问题的好办法。
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 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 可以查看其帮助

二、客户端使用
      下载memcached java client:http://www.whalin.com/memcached/#download
   1 解压后将java_memcached-release_2.0.1.jar jar包添加到工程的classpath中
       2 利用memcached java client 一个简单的应用
Java代码 复制代码
  1. package com.danga.MemCached.test;       
  2.       
  3. import java.util.Date;       
  4.       
  5. import com.danga.MemCached.MemCachedClient;       
  6. import com.danga.MemCached.SockIOPool;       
  7.       
  8.       
  9. public class Test {           
  10.     protected static MemCachedClient mcc = new MemCachedClient();          
  11.           
  12.     static {          
  13.         String[] servers ={"192.168.40.4:12000"};          
  14.           
  15.         Integer[] weights = { 3 };          
  16.           
  17.         //创建一个实例对象SockIOPool        
  18.         SockIOPool pool = SockIOPool.getInstance();          
  19.           
  20.         // set the servers and the weights       
  21.         //设置Memcached Server       
  22.         pool.setServers( servers );          
  23.         pool.setWeights( weights );          
  24.           
  25.         // set some basic pool settings          
  26.         // 5 initial, 5 min, and 250 max conns          
  27.         // and set the max idle time for a conn          
  28.         // to 6 hours          
  29.         pool.setInitConn( 5 );          
  30.         pool.setMinConn( 5 );          
  31.         pool.setMaxConn( 250 );          
  32.         pool.setMaxIdle( 1000 * 60 * 60 * 6 );          
  33.           
  34.         // set the sleep for the maint thread          
  35.         // it will wake up every x seconds and          
  36.         // maintain the pool size          
  37.         pool.setMaintSleep( 30 );          
  38.           
  39.         // Tcp的规则就是在发送一个包之前,本地机器会等待远程主机       
  40.                   // 对上一次发送的包的确认信息到来;这个方法就可以关闭套接字的缓存,       
  41.                   // 以至这个包准备好了就发;       
  42.                   pool.setNagle( false );          
  43.         //连接建立后对超时的控制       
  44.                   pool.setSocketTO( 3000 );       
  45.         //连接建立时对超时的控制       
  46.                   pool.setSocketConnectTO( 0 );          
  47.           
  48.         // initialize the connection pool          
  49.         //初始化一些值并与MemcachedServer段建立连接       
  50.                   pool.initialize();       
  51.                   
  52.           
  53.         // lets set some compression on for the client          
  54.         // compress anything larger than 64k          
  55.         mcc.setCompressEnable( true );          
  56.         mcc.setCompressThreshold( 64 * 1024 );          
  57.     }          
  58.               
  59.     public static void bulidCache(){          
  60.         //set(key,value,Date) ,Date是一个过期时间,如果想让这个过期时间生效的话,这里传递的new Date(long date) 中参数date,需要是个大于或等于1000的值。       
  61.         //因为java client的实现源码里是这样实现的 expiry.getTime() / 1000 ,也就是说,如果 小于1000的值,除以1000以后都是0,即永不过期       
  62.         mcc.set( "test""This is a test String" ,new Date(11211));      
  63.     //十秒后过期       
  64.                  
  65.     }          
  66.          
  67.     public static void output() {          
  68.         //从cache里取值       
  69.         String value = (String) mcc.get( "test" );          
  70.         System.out.println(value);           
  71.     }          
  72.               
  73.     public static void main(String[] args){          
  74.         bulidCache();         
  75.         output();              
  76.     }        
  77.           
  78. }