勒布朗 发表于 2013-2-4 14:32:26

membcached使用

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代码
package com.danga.MemCached.test;      
      
import java.util.Date;      
      
import com.danga.MemCached.MemCachedClient;      
import com.danga.MemCached.SockIOPool;      
      
      
public class Test {            
    protected static MemCachedClient mcc = new MemCachedClient();         
         
    static {         
      String[] servers ={"192.168.40.4:12000"};         
         
      Integer[] weights = { 3 };         
         
      //创建一个实例对象SockIOPool         
      SockIOPool pool = SockIOPool.getInstance();         
         
      // set the servers and the weights      
      //设置Memcached Server      
      pool.setServers( servers );         
      pool.setWeights( weights );         
         
      // set some basic pool settings         
      // 5 initial, 5 min, and 250 max conns         
      // and set the max idle time for a conn         
      // to 6 hours         
      pool.setInitConn( 5 );         
      pool.setMinConn( 5 );         
      pool.setMaxConn( 250 );         
      pool.setMaxIdle( 1000 * 60 * 60 * 6 );         
         
      // set the sleep for the maint thread         
      // it will wake up every x seconds and         
      // maintain the pool size         
      pool.setMaintSleep( 30 );         
         
      // Tcp的规则就是在发送一个包之前,本地机器会等待远程主机      
                  // 对上一次发送的包的确认信息到来;这个方法就可以关闭套接字的缓存,      
                  // 以至这个包准备好了就发;      
                  pool.setNagle( false );         
      //连接建立后对超时的控制      
                  pool.setSocketTO( 3000 );      
      //连接建立时对超时的控制      
                  pool.setSocketConnectTO( 0 );         
         
      // initialize the connection pool         
      //初始化一些值并与MemcachedServer段建立连接      
                  pool.initialize();      
                  
         
      // lets set some compression on for the client         
      // compress anything larger than 64k         
      mcc.setCompressEnable( true );         
      mcc.setCompressThreshold( 64 * 1024 );         
    }         
               
    public static void bulidCache(){         
      //set(key,value,Date) ,Date是一个过期时间,如果想让这个过期时间生效的话,这里传递的new Date(long date) 中参数date,需要是个大于或等于1000的值。      
      //因为java client的实现源码里是这样实现的 expiry.getTime() / 1000 ,也就是说,如果 小于1000的值,除以1000以后都是0,即永不过期      
      mcc.set( "test", "This is a test String" ,new Date(11211));      
    //十秒后过期      
                  
    }         
         
    public static void output() {         
      //从cache里取值      
      String value = (String) mcc.get( "test" );         
      System.out.println(value);            
    }         
               
    public static void main(String[] args){         
      bulidCache();         
      output();               
    }         
         
}
页: [1]
查看完整版本: membcached使用