`
jolestar
  • 浏览: 195474 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

配置了一下memcached

阅读更多
1.memcached是什么?
       memcached is a high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.
2.下载地址
      http://www.danga.com/memcached/
3.安装
     跟一般的linux软件安装一样 ./configure 然后 make,make install
     需要一个库 libevent
    下载地址:
     http://www.monkey.org/~provos/libevent/
     安装库后创建个链接
     ln -s /usr/local/lib/libevent-1.3b.so.1 /lib/libevent-1.3b.so.1
4.启动
     memcached -u nobody -d -m 50 -l 192.168.0.8 -p 10001
    -u 指定用户
    -m 指定缓存使用内存大小,只是测试,就用了50m
    -l 指定ip地址
    -p 指定端口
5.测试
   memcached是一个数据源,所以与应用是独立的,理论上支持任何语言访问。现在已有的client库可在下面的地址得到:
   http://www.danga.com/memcached/apis.bml
   我们用java测试,jdk为1.6
  
java 代码
 
  1. import com.danga.MemCached.MemCachedClient;  
  2. import com.danga.MemCached.SockIOPool;  
  3.   
  4. public class MemcacheTest {  
  5.   
  6. //   create a static client as most installs only need  
  7.     // a single instance  
  8.     protected static MemCachedClient mcc = new MemCachedClient();  
  9.   
  10.     // set up connection pool once at class load  
  11.     static {  
  12.   
  13.         // server list and weights  
  14.         String[] servers ={"192.168.0.8:10001"};  
  15.   
  16.         Integer[] weights = { 3 };  
  17.   
  18.         // grab an instance of our connection pool  
  19.         SockIOPool pool = SockIOPool.getInstance();  
  20.   
  21.         // set the servers and the weights  
  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.         // set some TCP settings  
  40.         // disable nagle  
  41.         // set the read timeout to 3 secs  
  42.         // and don't set a connect timeout  
  43.         pool.setNagle( false );  
  44.         pool.setSocketTO( 3000 );  
  45.         pool.setSocketConnectTO( 0 );  
  46.   
  47.         // initialize the connection pool  
  48.         pool.initialize();  
  49.   
  50.   
  51.         // lets set some compression on for the client  
  52.         // compress anything larger than 64k  
  53.         mcc.setCompressEnable( true );  
  54.         mcc.setCompressThreshold( 64 * 1024 );  
  55.     }  
  56.       
  57.     public static void bulidCache(){  
  58.         mcc.set( "foo""This is a test String" );  
  59.         TestObj obj = new TestObj();  
  60.         obj.setId(new Long(1));  
  61.         obj.setName("test");  
  62.         mcc.set("testObj", obj);  
  63.     }  
  64.   
  65.     // from here on down, you can call any of the client calls  
  66.     public static void output() {  
  67.         //  
  68.         String bar = (String) mcc.get( "foo" );  
  69.         System.out.println(bar);  
  70.         TestObj obj = (TestObj)mcc.get("testObj");  
  71.         System.out.println(obj);  
  72.     }  
  73.       
  74.     public static void main(String[] args){  
  75.         bulidCache();  
  76.         output();  
  77.     }  
  78.   
  79.   
  80. }  
java 代码
 
  1. import java.io.Serializable;  
  2.   
  3. public class TestObj implements Serializable {  
  4.   
  5.     /** 
  6.      *  
  7.      */  
  8.     private static final long serialVersionUID = 1L;  
  9.     private String name;  
  10.     private Long id;  
  11.     /** 
  12.      *  
  13.      */  
  14.     public TestObj() {  
  15.     }  
  16.     public Long getId() {  
  17.         return id;  
  18.     }  
  19.     public void setId(Long id) {  
  20.         this.id = id;  
  21.     }  
  22.     public String getName() {  
  23.         return name;  
  24.     }  
  25.     public void setName(String name) {  
  26.         this.name = name;  
  27.     }  
  28.   
  29.     public String toString(){  
  30.         return "id:"+this.getId()+";name:"+this.getName();  
  31.     }  
  32. }  


从faq上找了段代码,稍做改动。

运行,成功输出:

This is a test String
com.danga.MemCached.MemCachedClient Sat Jul 14 18:59:46 CST 2007 - ++++ deserializing class net.teamhot.memecache.TestObj
id:1;name:test

中间两行是日志。
把buildCache方法注释了,再运行,成功。

如果是两种语言共享cache,要注意对象的持久化方式。

6.可能遇到的问题
   如果连接不成功请用telent测试服务器端口是否能连接通。连不通请更改服务器的防火墙设置,或者干脆把防火墙关闭了。

   /etc/init.d/iptables stop

    今天就到此为止。改天找几台服务器测试一下memcache的性能。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics