寫一個php memcache 簡單的函數

來源:互聯網
上載者:User

標籤: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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.