在PHP中使用memcached
由于网站流量逐渐增大,而且有个新需求是需要实时更新文章的点击和回复。所以准备上memcached来做一个解决的办法。
在php中使用memcached
1 安装memcached
直接
njava:~ njava$ sudo apt-get install memcached
本地开发使用的是刚升级了的mac10.6.4,使用port安装
njava-MacPro:~ njava$ sudo port install memcached
当然也可以自己编译代码了,为了省事就这样用了
2 安装php5-memcached
同样使用apt和port安装
njava:~ njava$ sudo apt-get install php5-memcached
macos使用port安装
njava-MacPro:~ njava$ sudo port php5-memcached
3 启动memcache
首先看看memcache的启动参数
njava-MacPro:~ njava$ memcached -help memcached 1.4.5 -pTCP port number to listen on (default: 11211) -U UDP port number to listen on (default: 11211, 0 is off) -s UNIX socket path to listen on (disables network support) -a access mask for UNIX socket, in octal (default: 0700) -l interface to listen on (default: INADDR_ANY, all addresses) -d run as a daemon -r maximize core file limit -u assume identity of (only when run as root) -m max memory to use for items in megabytes (default: 64 MB) -M return error on memory exhausted (rather than removing items) -c max simultaneous connections (default: 1024) -k lock down all paged memory. Note that there is a limit on how much memory you may lock. Trying to allocate more than that would fail, so be sure you set the limit correctly for the user you started the daemon with (not for -u user; under sh this is done with 'ulimit -S -l NUM_KB'). -v verbose (print errors/warnings while in event loop) -vv very verbose (also print client commands/reponses) -vvv extremely verbose (also print internal state transitions) -h print this help and exit -i print memcached and libevent license -P save PID in , only used with -d option -f chunk size growth factor (default: 1.25) -n minimum space allocated for key+value+flags (default: 48) -L Try to use large memory pages (if available). Increasing the memory page size could reduce the number of TLB misses and improve the performance. In order to get large pages from the OS, memcached will allocate the total item-cache in one large chunk. -D Use as the delimiter between key prefixes and IDs. This is used for per-prefix stats reporting. The default is ":" (colon). If this option is specified, stats collection is turned on automatically; if not, then it may be turned on by sending the "stats detail on" command to the server. -t number of threads to use (default: 4) -R Maximum number of requests per event, limits the number of requests process for a given connection to prevent starvation (default: 20) -C Disable use of CAS -b Set the backlog queue limit (default: 1024) -B Binding protocol - one of ascii, binary, or auto (default) -I Override the size of each slab page. Adjusts max item size (default: 1mb, min: 1k, max: 128m) njava-MacPro:~ njava$
测试使用启动参数
njava-MacPro:~ njava$ ~memcached –d –m 200
-d 守护进程 -m 200兆的缓存 默认使用11211端口
4 php连接测试
在php中尝试链接并输出测试信息
$memcache = new Memcache(); @$memcache->connect('127.0.0.1',11211) or die("Can't connect Memcache"); $version = $memcache->getversion(); echo "Memcache Version:$version"; $stats = $memcache->getstats(); echo "
Memcache Status:
"; foreach($stats as $key=>$stat){ echo "$key:$stat "; }
输出:
Memcache Version:1.4.5 pid:11712 uptime:2374 time:1285780715 version:1.4.5 pointer_size:64 rusage_user:0.014314 rusage_system:0.019851 curr_connections:10 total_connections:62 connection_structures:11 cmd_get:29 cmd_set:29 cmd_flush:0 get_hits:29 get_misses:0 delete_misses:0 delete_hits:0 incr_misses:0 incr_hits:0 decr_misses:0 decr_hits:0 cas_misses:0 cas_hits:0 cas_badval:0 auth_cmds:0 auth_errors:0 bytes_read:4332 bytes_written:43345 limit_maxbytes:209715200 accepting_conns:1 listen_disabled_num:0 threads:4 conn_yields:0 bytes:179 curr_items:1 total_items:29 evictions:0 reclaimed:7
成功
5 使用php5-memcached的基本功能接口调用
查看php5-memcached的代码,可以看到Memcache实现了MemcachePool的一堆方法
class Memcache extends MemcachePool{ public function pconnect ($host, $port = null, $timeout = null) {} public function addserver ($host, $port = null, $persistent = null, $weight = null, $timeout = null, $retry_interval = null, $status = null, $failure_callback = null, $timeoutms = null) {} public function setserverparams () {} public function setfailurecallback () {} public function getserverstatus () {} public function getversion () {} public function add () {} public function set () {} public function replace () {} public function cas () {} public function append () {} public function prepend () {} public function get () {} public function delete () {} public function getstats () {} public function getextendedstats () {} public function setcompressthreshold () {} public function increment () {} public function decrement () {} public function close () {} public function flush () {} }
使用memcache来存储php对象
$memcache = new Memcache(); @$memcache->connect('127.0.0.1',11211) or die("Can't connect Memcache"); $row = new stdClass(); $row->click= 50; $row->download = 20; $row->feedback = 13; $row->timestamp = time(); echo "
"; var_dump($row); $memcache->set('row_100', $row, false, 10) or die ("Save Cache Failed"); echo "
"; var_dump($memcache->get('row_100'));
调试结束