PHP 使用 memcache

這次我第一次開始用 memcached, 除了在 Google App Engine 上的 memcache 不算的話 XD

首先先裝 memcached, 我使用的是 gentoo linux

emerge memcached

接著裝上 pecl-memcache

emerge dev-php5/pecl-memcache

不過目前 amd64 平台的 pecl-memcache 套件還是 masked, 所以只好加上 ACCEPT_KEYWORDS 來安裝。

ACCEPT_KEYWORDS='~amd64' emerge dev-php5/pecl-memcache

然後把 www server 重開就可以開始用 memcache 了 :D 當然 memcached 也要記得開起來阿 XD

來個簡單的 php 範例:

$memcache = new Memcache;
$memcache->connect('', 11211);
$memcache->set('key', 'value', false, 3600);
$cache_data = $memcache->get('key');```

大概就是這樣啦! 簡單方便好用阿! :D

不過目前我的一些東西都會用到同一台 memcache server, 所以至少每個網站都得要各自的 key prefix, 才不會衝到, 所以寫了一個給 CodeIgniter 用的 mcache library, 基本上就是個 wrapper, 除了處理 key prefix, 連線, 還有把設定給寫到 config 檔, 可以直接修改 config 檔來設定 memcache, 跟 prefix。

key prefix 處理大概像是這樣, 而 prefix 就是在 config 做設定了:
public function get($key)
    if (!$this->status) return;

    return $this->memcache->get($this->prefix . '_' . $key);

public function set($key, $value, $compressed = null, $expire = null) { if (!$this->status) return;

if (!isset($compressed))
    $compressed = $this->compressed;

if (!isset($expire))
    $expire = $this->expire;

$this->memcache->set($this->prefix . '_' . $key, $value, $compressed, $expire);



$config['memcachehost'] = '';
$config['memcacheport'] = 11211;
$config['memcacheprefix'] = 'stickeraction';
$config['memcachecompressed'] = MEMCACHECOMPRESSED;
$config['memcacheexpire'] = 60*5;
$config['memcache_pconnect'] = false;

目前用來處理 stickeraction 貼紙的部份, 大概從 0.01s 降到 0.003s 吧? 其實沒認真測 XD

另外, 可以用 memcache 的 getStats 函式寫一個來看 memcache 的使用情形

$memcache = new Memcache;
$memcache->connect('', 11211);
$stats = $memcache->getStats();
foreach ($stats as $key => $val) {
    echo "$key : $val 
"; }

在 PHP Manual 的這裡可以看到相對應的欄位說明, 如下:

pid                       Process id of this server process
uptime                    Number of seconds this server has been running
time                      Current UNIX time according to the server
version                   Version string of this server
rusageuser               Accumulated user time for this process
rusagesystem             Accumulated system time for this process
curritems                Current number of items stored by the server
totalitems               Total number of items stored by this server ever since it started
bytes                     Current number of bytes used by this server to store items
currconnections          Number of open connections
totalconnections         Total number of connections opened since the server started running
connectionstructures     Number of connection structures allocated by the server
cmdget                   Cumulative number of retrieval requests
cmdset                   Cumulative number of storage requests
gethits                  Number of keys that have been requested and found present
getmisses                Number of items that have been requested and not found
bytesread                Total number of bytes read by this server from network
byteswritten             Total number of bytes sent by this server to network
limitmaxbytes            Number of bytes this server is allowed to use for storage.

memcache 真的是簡單又好用阿! :D



Read more posts by this author.

Subscribe to Oceanic / 海海人生

Get the latest posts delivered right to your inbox.

or subscribe via RSS with Feedly!