win7下memcache的安装

一、下载memcached的稳定版本,然后解压到某个目录下面,我放到了d:\app\memcached

下载链接


二、找到c:\WINDOWS\system32\cmd.exe,右键以管理员身份运行,否则会报错,切换到memcached目录下面,

安装

Cmd代码 收藏代码
  1. memcached.exe –d install

启动

Cmd代码 收藏代码
  1. memcached.exe -d start


三、下载php_memcache.dll,请自己查找对应的php版本的文件,我的是php5.3.5,注意如果是apache就下VC6的

下载链接


四、把php_memcache.dll放到php的ext目录下,在php.ini中加入如下代码:

Php代码 收藏代码
  1. extension=php_memcache.dll

  2. [Memcache]

  3. memcache.allow_failover = 1

  4. memcache.max_failover_attempts=20

  5. memcache.chunk_size =8192

  6. memcache.default_port = 11211


五、测试php的运行

Php代码 收藏代码
  1. <?php

  2. $mem = new Memcache;

  3. $mem->connect('127.0.0.1', 11211);

  4. $mem->set('key', ' a value', 0, 60);

  5. $val = $mem->get('key');

  6. echo$val;

如果成功会输出:a value


六、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客户端代码的编写:

Java代码 收藏代码
  1. import java.util.Date;

  2. import com.danga.MemCached.*;

  3. publicclass BasicTest {

  4. privatestaticfinal String POOL_NAME="test_pool";

  5. protectedstatic MemCachedClient mcc;

  6. static {

  7. //设置缓存服务器列表,当使用分布式缓存的时,可以指定多个缓存服务器

  8. String[] servers =

  9. {

  10. "127.0.0.1:11211"

  11. };

  12. //与服务器列表中对应的各服务器的权重

  13. Integer[] weights = {3};

  14. //创建Socked连接池

  15. SockIOPool pool = SockIOPool.getInstance(POOL_NAME);

  16. //向连接池设定服务器和权重

  17. pool.setServers( servers );

  18. pool.setWeights( weights );

  19. //连接池参数

  20. pool.setInitConn( 5 );

  21. pool.setMinConn( 5 );

  22. pool.setMaxConn( 250 );

  23. pool.setMaxIdle( 1000 * 60 * 60 * 6 );

  24. // set the sleep for the maint thread

  25. // it will wake up every x seconds and

  26. // maintain the pool size

  27. pool.setMaintSleep( 30 );

  28. // set some TCP settings

  29. // disable nagle

  30. // set the read timeout to 3 secs

  31. // and don't set a connect timeout

  32. pool.setNagle( false );

  33. pool.setSocketTO( 3000 );

  34. pool.setSocketConnectTO( 0 );

  35. // initialize the connection pool

  36. pool.initialize();

  37. // lets set some compression on for the client

  38. // compress anything larger than 64k

  39. mcc=new MemCachedClient(POOL_NAME);

  40. mcc.setCompressEnable( true );

  41. mcc.setCompressThreshold( 64 * 1024 );

  42. }

  43. publicstaticvoid main(String[] args) throws Exception{

  44. mcc.set("msg","Hello,world!",new Date(System.currentTimeMillis()+1300));

  45. Thread.sleep(500);

  46. System.out.println(mcc.get("msg"));

  47. }

  48. }

ps:附近中为win32的memcached1.2.4和php5.3 vc6的memcached的dll文件


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章