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文件


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