memcached 真的很有意思 …

這個版本 support multiple instance

 * With this version of Memcached Gentoo now supports multiple instances.
 * To enable this you should create a symlink in /etc/init.d/ for each instance
 * to /etc/init.d/memcached and create the matching conf files in /etc/conf.d/
 * Please see Gentoo bug #122246 for more info



PORT="11212"  <---- 改!


 9312 ?        Ssl    0:00 /usr/bin/memcached -d -p 11211 -U 11211 -l -m 64 -c 1024 -u memcached -P /var/run/memcached/
 9366 ?        Ssl    0:00 /usr/bin/memcached -d -p 11212 -U 11212 -l -m 64 -c 1024 -u memcached -P /var/run/memcached/
 9420 ?        Ssl    0:00 /usr/bin/memcached -d -p 11213 -U 11213 -l -m 64 -c 1024 -u memcached -P /var/run/memcached/
 9474 ?        Ssl    0:00 /usr/bin/memcached -d -p 11214 -U 11214 -l -m 64 -c 1024 -u memcached -P /var/run/memcached/

下 , netstat -t -u -an 可以看到 ….


它的 memcache client API for php 有新舊兩個版, 之前只參考到 PHP 官網的 manual 寫的 function 用了它的舊 API call , 造成 connection 不會自動斷線, 需要改用新的才行.

另外, memcache 改用 UDP protocol 比較省 CPU cost , 不過 memcached 的 CPU cost 本來就很低, 所以省的部份是在 memcache client 端, 也就是前台 web server 的 CPU cost 可以省一些.

這個就是 "隱藏版" 的 manual , 竟然要到 cvs 去才看得到 ORZ

initial revision

memcached module for PHP


This module requires zlib library, used for on-the-fly data (de)compression.

Also, you’ll need memcached to use it =)

The memcached website is here:

You will probably need libevent to install memcached:

You can download it here:

New API in 3.0


Version 3 introduces a new class "MemcachePool" which implements the new API, the

old class "Memcache" is still retained (but is deprecated) with the same interface

for backwards compatibility. Please note that you need a new memcached version to

use the CAS, default value to increment/decrement, append and prepend, and binary

protocol features.

New INI directives are available to allow control over protocol, redundancy and hash

strategy selection. These are

# The binary protocol results in less traffic and is more efficient

# for the client and server to generate/parse

memcache.protocol = {ascii, binary} # default ascii

# When enabled the client sends requests to N servers in parallel, resulting in

# a somewhat crude reduncancy or mirroring, suitable when used as a session

# storage.


# If data integrity is of greater importance a real replicating memcached

# backend such as "repcached" ( is

# recommended

memcache.redundancy = <int> # default 1

memcache.session_redundancy = <int> # default 2

# Hash strategy and function selection. The consistent hashing strategy

# is now the default as it allows servers to be added and removed from

# the pool without resulting in all or most keys being re-mapped to

# other server (ie. voiding the cache)

memcache.hash_strategy = {standard, consistent} # default consistent

memcache.hash_function = {crc32, fnv} # default crc32

The directives are used by the MemcachePool constructor so you can instantiate

several pools with different settings by using ini_set() creativly. For example

ini_set(‘memcache.protocol’, ‘binary’);

$binarypool = new MemcachePool();


ini_set(‘memcache.protocol’, ‘ascii’);

ini_set(‘memcache.redundancy’, ‘2’);

$redundantpool = new MemcachePool();


ini_set(‘memcache.redundancy’, ‘1’);

The new interface looks like

class MemcachePool() {

bool connect(string host, int tcp_port = 11211, int udp_port = 0, bool persistent = true, int weight = 1, int timeout = 1, int retry_interval = 15)

bool addServer(string host, int tcp_port = 11211, int udp_port = 0, bool persistent = true, int weight = 1, int timeout = 1, int retry_interval = 15, bool status = true)

bool setServerParams(string host, int tcp_port = 11211, int timeout = 1, int retry_interval = 15, bool status = true)


* Supports fetching flags and CAS values


mixed get(mixed key, mixed &flags = null, mixed &cas = null)


* Supports multi-set, for example

* $memcache->set(array(‘key1’ => ‘val1’, ‘key2’ => ‘val1’), null, 0, 60)


bool add(mixed key, mixed var = null, int flag = 0, int exptime = 0)

bool set(mixed key, mixed var = null, int flag = 0, int exptime = 0)

bool replace(mixed key, mixed var = null, int flag = 0, int exptime = 0)


* Compare-and-Swap, uses the CAS param from MemcachePool::get()


bool cas(mixed key, mixed var = null, int flag = 0, int exptime = 0, int cas = 0)


* Prepends/appends a value to an existing one


bool append(mixed key, mixed var = null, int flag = 0, int exptime = 0)

bool prepend(mixed key, mixed var = null, int flag = 0, int exptime = 0)


* Supports multi-key operations, for example

    * $memcache->delete(array(‘key1’, ‘key2’))


    bool delete(mixed key, int exptime = 0)


    * Supports multi-key operations, for example

    * $memcache->increment(array(‘key1’, ‘key2’), 1, 0, 0)


    * The new defval (default value) and exptime (expiration time) are used

    * if the key doesn’t already exist. They must be supplied (even if 0) for

    * this to be enabled.


    mixed increment(mixed key, int value = 1, int defval = 0, int exptime = 0)

    mixed decrement(mixed key, int value = 1, int defval = 0, int exptime = 0)


    * Assigns a pool-specific failure callback which will be called when

    * a request fails. May be null in order to disable callbacks. The callback

    * receive arguments like


    * function mycallback($host, $tcp_port, $udp_port, $error, $errnum)


    * Where $host and $error are strings or null, the other params are integers.


    bool setFailureCallback(function callback)


在這邊看到 兩個 memcache php client 的比較表 :

PHP Client Comparison

There are primarily two clients used with PHP. One is the older, more widespread pecl/memcache and the other is the newer, less used, more feature rich pecl/memcached.

Both support the basics such as multiple servers, setting vaules, getting values, increment, decrement and getting stats.

Here are some more advanced features and information.

pecl/memcache pecl/memcached
First Release Date 2004-06-08 2009-01-29 (beta)
Actively Developed? Yes Yes
External Dependency None libmemcached
Automatic Key Fixup1 Yes No
Append/Prepend No Yes
Automatic Serialzation2 Yes Yes
Binary Protocol No Optional
CAS No Yes
Compression Yes Yes
Communication Timeout Connect Only Various Options
Consistent Hashing Yes Yes
Delayed Get No Yes
Multi-Get Yes Yes
Session Support Yes Yes
Set/Get to a specific server No Yes
Stores Numerics Converted to Strings Yes
  1. pecl/memcache will convert an invalid key into a valid key for you. pecl/memcached will return false when trying to set/get a key that is not valid.
  2. You do not have to serialize your objects or arrays before sending them to the set commands. Both clients will do this for you.

CodeIgniter MVC CRUD + memcache = Secret Message

這個 idea 是來自這個網站 : , 簡單的說它是一個經由 https 保護傳送內容的網路服務 , user 用這個服務把臨時要給朋友的機密/私人資料譬如 password / URL 之類的 data 存放在這網站上, 然後此系統會給 user 一個唯一網址, 再把這網址給朋友, 開啟這個唯一網址後, 就可以看到這個訊息, 系統同時會把這訊息從系統中刪除… 哈哈! 看懂了嗎?

我的改進是用 memcache 的 expire 機制, 設定 10分鐘後, 若 user 沒讀過此訊息, 訊息會 ‘自動’ 銷毀… 我的站若再去申請 ssl 加密的話就跟那個站功能是類似的了.

codeigniter 的 MVC 架構, 讓我很快的把這個 idea implement 出來了 , 基本上就是一個簡單版的 CRUD

我的 Secret Message 服務長像非常的陽春 😛 , 僅用簡單的 HTML , 若有空再加上 style 美化一下版面, 不過基本功能是有的了, 大家用看看, 有 idea 或意見請再告訴我.

目前 message 不提供 HTML / VBB , 但是有簡單的 skype 版表情符號 ( icon 正在慢慢搜集中 ) ….

Secret Message 網址是:




CI 表情符號改寫例:



debian / ubuntu 裝 memcached 跟 pecl memcache

裝 memcached :

apt-get install memcached

/etc/default/memcached 裡面設 yes –> 啟用 設定檔 /etc/memcached.conf

裝 php5 的 memcache extension

apt-get install php5-memcache

會產生 /etc/php5/conf.d/memcache.ini 內容是


—– 以下是舊的方法, 2010.0630 前

裝 pecl memcache

aptitude install libevent-dev

因為 pecl 要用到 phpize , 所以要裝 php5-dev 套件

apt-get install php5-dev

apt-get install php-pear

pecl install memcache

裝好了, 要在 /etc/php5/conf.d 設一個檔 memcache.ini

cat > /etc/php5/conf.d/memcache.ini

安裝 apache / mod_memcache

準備好這幾個 tarball, 並且解開:

drwxr-xr-x  6 rimmon rimmon  576 Mar 14 17:36 mod_memcached_cache-0.1.0
drwxr-xr-x  7 rimmon rimmon  584 Mar 14 17:09 apr_memcache-0.7.0
drwxr-xr-x 12 rimmon rimmon 1416 Mar 14 16:55 httpd-2.2.11