Windows下的Memcache安裝
1、下載memcache for windows。下載地址:http://jehiah.cz/projects/memcached-win32,推薦下載binaries版本,解壓(本例中解壓到c:memcached)。
2、在命令列狀態下輸入: c:memcachedmemcached.exe -d install 。至此memcached已經安裝成windows服務
3、在命令列下輸入: c:memcachedmemcached.exe -d start 以啟動memcached服務。當然也可以選擇在windows服務中啟動
你沒看錯,就是這麼簡單,簡簡單單的三步memcache的伺服器端就準備完畢
php安裝Memcached模組支援
1、下載php_memcache.dll模組,你可以從http://downloads.php.net/pierre/找到對應的版本,
php5.3的直接下載http://shikii.net/blog/downloads/php_memcache-cvs-20090703-5.3-VC6-x86.zip
2、修改php.ini,添加如下內容:
extension=php_memcache.dll3、重啟apache伺服器,然後查看一下phpinfo,如果有memcache,那麼就說明安裝成功!
測試windows下的Memcached
測試代碼如下:
代碼如下 |
複製代碼 |
<?php $mem = new Memcache; $mem->connect("127.0.0.1", 11211); $mem->set('key', 'Hello Memcached!', 0, 60); $val = $mem->get('key'); echo $val; ?> |
Example #1 memcache extension overview example
In this example, an object is being saved in the cache and then retrieved back. Object and other non-scalar types are serialized before saving, so it's impossible to store resources (i.e. connection identifiers and others) in the cache.
代碼如下 |
複製代碼 |
<?php $memcache = new Memcache; $memcache->connect('localhost', 11211) or die ("Could not connect"); $version = $memcache->getVersion(); echo "Server's version: ".$version."<br/>n"; $tmp_object = new stdClass; $tmp_object->str_attr = 'test'; $tmp_object->int_attr = 123; $memcache->set('key', $tmp_object, false, 10) or die ("Failed to save data at the server"); echo "Store data in the cache (data will expire in 10 seconds)<br/>n"; $get_result = $memcache->get('key'); echo "Data from the cache:<br/>n"; var_dump($get_result); ?> |
Example #2 Using memcache session handler
代碼如下 |
複製代碼 |
<?php $session_save_path = "tcp://$host:$port?persistent=1&weight=2&timeout=2&retry_interval=10, ,tcp://$host:$port "; ini_set('session.save_handler', 'memcache'); ini_set('session.save_path', $session_save_path); ?> |
-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 顯示協助