memcache 安装教程

来源:互联网 发布:js 静态资源公共库 编辑:程序博客网 时间:2024/05/20 11:25

laiyuan:http://www.csdn123.com/html/itweb/20131021/179483.htm

memcached:官网http://memcached.org/

说明:memcached本身没有Linux版本,网上的windows 版本都是个人开发的。

memcached和memcache区别:

memcache是php的一个扩展,用于php管理memcached,php-memcache.dll。

如果安装了memcached不安装扩展,那么php无法操控memcached,但是命令行使用起来没有问题

如果安装了memcache扩展但是没有安装memcached服务,那么这个就无法使用。

只有同时安装了memcached服务和memcache扩展才可以在PHP中使用memcached提高动态网站性能。

要在網站中使用 memcached,必須要進行兩個分別的步驟。首先在 Windows 下架設 memcached 伺服器 。

很多人都推荐区http://code.jellycan.com/memcached/ 下载,我进入后没看到下载地址,而是在

 http://downloads.northscale.com/memcached-win32-1.4.4-14.zip! 下载的。


下載後解壓縮到你要放置的資料夾,然後以命令提示字元模式在該資料夾中下指令:

  1. memcached.exe -d install

這是將 memcached 安裝成系統服務。接著啟動 memcached 服務:

  1. memcached.exe -d start

到這裏,只是安裝好「memcached 伺服器」。

接著需在 php 中安裝相關檔案並設定,網站才能使用 memcached。首先去下載與你 php 版本相符的 php_memcache.dll,將它放到 php 資料夾下的 \ext 目錄。
你可以在這裏下載到適合你 php 版本 win32 的 php_memcache.dll:

http://windows.php.net/downloads/pecl/releases/memcache/

例如我的 php 版本為 5.5.1,就下載 3.0.8 資料夾下的 php_memcache-3.0.8-5.5-ts-vc11-x86.zip 或 php_memcache-3.0.8-5.5-nts-vc11-x86.zip
(要使用 64 位元版本的話請下載 x64)。

什麼是 ts 與 nts 版本:
TS:Thread Safe 執行緒安全, 執行時會進行執行緒(Thread)安全檢查,以防止有新要求就啟動新執行緒的 CGI 執行方式而耗盡系統資源
NTS:Non Thread Safe 非執行緒安全, 在執行時不進行執行緒(Thread)安全檢查
PHP的兩種執行方式:ISAPI 和 FastCGI。
ISAPI (Internet Server Application Programming Interface) 執行方式是以 DLL 動態函式庫的形式使用,可以在被使用者請求後執行,在處理完一個使用者請求後不會馬上消失,所以需要進行執行緒安全檢查,這樣來提高程式的執行效率,所以如果是以 ISAPI 來執行 PHP,建議選擇 Thread Safe 版本
apache 中的設定方式:
#下面這個是載入 TS 版本的 php 必須的
LoadModule php5_module 「xxx/php5apache2_2.dll」

接著在 php.ini 的最後面加上以下內容:

  1. [Memcache]
  2. extension=php_memcache.dll

重新啟動 apache 即可。

註:memcached 伺服器 與網站伺服器 (如 Apache Server) 並不用一定要架在同一台電腦上。如果不同電腦,只需在相關的網站設定中指定 memcached 伺服器所在的 IP 就可以了。

测试是否安装成功:

<?php  $memcache = new Memcache;  $memcache->connect("localhost",11211); # You might need to set "localhost" to "127.0.0.1"  echo "Server's version: " . $memcache->getVersion() . "<br />\n";  $tmp_object = new stdClass;  $tmp_object->str_attr = "test";  $tmp_object->int_attr = 123;  $memcache->set("key",$tmp_object,false,10);  echo "Store data in the cache (data will expire in 10 seconds)<br />\n";  echo "Data from the cache:<br />\n";  var_dump($memcache->get("key"));?>

没有错误产生,则成功。

我的输出:

Server's version: 1.4.4-14-g9c660c0
Store data in the cache (data will expire in 10 seconds)
Data from the cache:
object(stdClass)#3 (2) { ["str_attr"]=> string(4) "test" ["int_attr"]=> int(123) }

memcache基本设置

-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 显示帮助

0 0
原创粉丝点击