日期:2014-05-17  浏览次数:20829 次

Windows下Memcached安装及Java客户端调用

Windows下的Memcache安装:
1. 下载memcache的windows稳定版,解压放某个盘下面,比如在c:\memcached
2. 在终端(也即cmd命令界面)下输入 ‘c:\memcached\memcached.exe -d install’ 安装
3. 再输入: ‘c:\memcached\memcached.exe -d start’ 启动。NOTE: 以后memcached将作为windows的一个服务每次开机时自动启动。这样服务器端已经安装完毕了。
4.下载php_memcache.dll,请自己查找对应的php版本的文件
5. 在C:\winnt\php.ini 加入一行 ‘extension=php_memcache.dll’
6.重新启动Apache,然后查看一下phpinfo,如果有memcache,那么就说明安装成功!

memcached的基本设置:

-p 监听的端口
-l 连接的IP地址, 默认是本机
-d start 启动memcached服务
-d restart 重起memcached服务
-d stop|shutdown 关闭正在运行的memcached服务
-d install 安装memcached服务
-d uninstall 卸载memcached服务
-u 以的身份运行 (仅在以root运行的时候有效)
-m 最大内存使用,单位MB。默认64MB
-M 内存耗尽时返回错误,而不是删除项
-c 最大同时连接数,默认是1024
-f 块大小增长因子,默认是1.25
-n 最小分配空间,key+value+flags默认是48
-h 显示帮助

JAVA客户端的编写:

?

import java.util.Date;
import com.danga.MemCached.*;

public class BasicTest {
 private static final String POOL_NAME="test_pool";
 protected static MemCachedClient mcc;
 static {
  //设置缓存服务器列表,当使用分布式缓存的时,可以指定多个缓存服务器
  String[] servers =
  {
     "127.0.0.1:11211"
  };
  //与服务器列表中对应的各服务器的权重
  Integer[] weights = {3};

  //创建Socked连接池
  SockIOPool pool = SockIOPool.getInstance(POOL_NAME);

  //向连接池设定服务器和权重
  pool.setServers( servers );
  pool.setWeights( weights );
  
  //连接池参数
  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 );

  // set some TCP settings
  // disable nagle
  // set the read timeout to 3 secs
  // and don't set a connect timeout
  pool.setNagle( false );
  pool.setSocketTO( 3000 );
  pool.setSocketConnectTO( 0 );

  // initialize the connection pool
  pool.initialize();


  // lets set some compression on for the client
  // compress anything larger than 64k

  mcc=new MemCachedClient(POOL_NAME);
  mcc.setCompressEnable( true );
  mcc.setCompressThreshold( 64 * 1024 );
 }

 public static void main(String[] args) throws Exception{
  
  mcc.set("msg","Hello,world!",new Date(System.currentTimeMillis()+1300));
  Thread.sleep(500);
  System.out.println(mcc.get("msg"));
 }
}
?

相关链接文章:

memcached教程

memcached 理论参数计算方式

Memcached 集群,客户端自动Hash到不同服务器的实现