標籤:memcache 系統最佳化 緩衝 php
在一個項目中添加了memcache層,但由於資料庫本來壓力就不大,資料量很小,所以效能改善不是特別明顯,因此學習並應用下來記錄一下方便以後自己使用。這裡我只應用了直接調用對應api函數的方法,另外一種方法是建立對象來串連memcache,具體是$mem=new Memcache,然後再調用對象裡的方法來操作要儲存的item。
本次使用環境為php 5.4.17,
yum安裝的驅動:
php-pecl-memcache-3.0.8-1.el5.remi
代碼如下:
$MEMCACHE["host"]="10.54.178.202";$MEMCACHE["port"]="11211";$MEMCACHE["timeout"]="5";function cache_set($key, $value, $expire = 86400, $flag = MEMCACHE_COMPRESSED, $cache_host = NULL) { if(empty($cache_host)) { global $MEMCACHE; $cache_host = $MEMCACHE["host"]; $cache_port = $MEMCACHE["port"]; $cache_timeout = $MEMCACHE["timeout"]; } $memcache = memcache_connect($cache_host,$cache_port,$cache_timeout); $memcache->set($key, $value, $flag, $expire); memcache_close($memcache);}function cache_get($key, $cache_host = null) { if(empty($cache_host)) { global $MEMCACHE; $cache_host = $MEMCACHE["host"]; $cache_port = $MEMCACHE["port"]; $cache_timeout = $MEMCACHE["timeout"]; } $memcache = memcache_connect($cache_host,$cache_port,$cache_timeout); $result=$memcache->get($key); memcache_close($memcache); return $result;}function cache_clear($key, $cache_host = null) { if(empty($cache_host)) { global $MEMCACHE; $cache_host = $MEMCACHE["host"]; $cache_port = $MEMCACHE["port"]; $cache_timeout = $MEMCACHE["timeout"]; } $memcache = memcache_connect($cache_host,$cache_port,$cache_timeout); $memcache->delete($key, 0); memcache_close($memcache);}
本文出自 “安家圈子交流學習” 部落格,請務必保留此出處http://brucetam.blog.51cto.com/1863614/1537268