此模块使用了函数zlib来支持数据压缩,因此安装此模块需要安装 Zlib 模块。PHP 4.3.3 及以后版本需要使用 memcache 扩展。
此PECL 扩展未与 PHP 捆绑。 Note:可以关闭memcache session处理器的支持。使用pecl install进行安装时,在静态编译到php中时使用选项 --disable-memcache-session 可以关闭memcache的session 支持(默认时开启的)。
It is very important to note when reading the information supplied by others on this page that there are two *distinct* memcache PHP implementations for the service "memcached". 1) pecl-memcache 2) pecl-memcached This page is for the first, pecl-memcache. If you are looking for pecl-memcached information, visit here: http://www.php.net/manual/en/book.memcached.php
#if apt-get, rpm, or yum doesn't work cd /usr/src/ wget http://pecl.php.net/get/memcache-2.2.4.tgz tar -zxvf memcached-2.2.4.tgz cd memcached-2.2.4 phpize && ./configure --enable-memcache && make cp modules/memcache.so /usr/lib/php/modules/ # Note: packaged extension modules are now loaded via the .ini files # found in the directory /etc/php.d touch /etc/php.d/memcached.ini echo 'extension=memcache.so' > /etc/php.d/memcached.ini service httpd restart
运行时配置
这些函数的行为受php.ini中的设置影响。
名字 | 默认 | 可修改范围 | 更新日志 |
---|---|---|---|
memcache.allow_failover | "1" | PHP_INI_ALL | Available since memcache 2.0.2. |
memcache.max_failover_attempts | "20" | PHP_INI_ALL | Available since memcache 2.1.0. |
memcache.chunk_size | "8192" | PHP_INI_ALL | Available since memcache 2.0.2. |
memcache.default_port | "11211" | PHP_INI_ALL | Available since memcache 2.0.2. |
memcache.hash_strategy | "standard" | PHP_INI_ALL | Available since memcache 2.2.0. |
memcache.hash_function | "crc32" | PHP_INI_ALL | Available since memcache 2.2.0. |
session.save_handler | "files" | PHP_INI_ALL | Supported since memcache 2.1.2 |
session.save_path | "" | PHP_INI_ALL | Supported since memcache 2.1.2 |
memcache.protocol | ascii | >PHP_INI_ALL | Supported since memcache 3.0.0 |
memcache.redundancy | 1 | >PHP_INI_ALL | Supported since memcache 3.0.0 |
memcache.session_redundancy | 2 | >PHP_INI_ALL | Supported since memcache 3.0.0 |
memcache.compress_threshold | 20000 | >PHP_INI_ALL | Supported since memcache 3.0.3 |
memcache.lock_timeout | 15 | >PHP_INI_ALL | Supported since memcache 3.0.4 |
Memcache类
表示连接到一个服务器组的连接。
Memcache { add ( string $key , mixed $var [, int $flag [, int $expire ]] ) : bool addServer ( string $host [, int $port = 11211 [, bool $persistent [, int $weight [, int $timeout [, int $retry_interval [, bool $status [, callback $failure_callback [, int $timeoutms ]]]]]]]] ) : bool close ( void ) : bool connect ( string $host [, int $port [, int $timeout ]] ) : bool decrement ( string $key [, int $value = 1 ] ) : int delete ( string $key [, int $timeout = 0 ] ) : bool flush ( void ) : bool get ( string $key [, int &$flags ] ) : string getExtendedStats ([ string $type [, int $slabid [, int $limit = 100 ]]] ) : array getServerStatus ( string $host [, int $port = 11211 ] ) : int getStats ([ string $type [, int $slabid [, int $limit = 100 ]]] ) : array getVersion ( void ) : string increment ( string $key [, int $value = 1 ] ) : int pconnect ( string $host [, int $port [, int $timeout ]] ) : mixed replace ( string $key , mixed $var [, int $flag [, int $expire ]] ) : bool set ( string $key , mixed $var [, int $flag [, int $expire ]] ) : bool setCompressThreshold ( int $threshold [, float $min_savings ] ) : bool setServerParams ( string $host [, int $port = 11211 [, int $timeout [, int $retry_interval = false [, bool $status [, callback $failure_callback ]]]]] ) : bool }
Here is a simple memcached aggregator class which distributes the cache among multiple cache servers. If a server fails, the load is redistributed automatically. It uses persistent connections. The constructor takes an array of arrays, with each inner array representing a server, with a 'server' (string) attribute that is the IP addres or host name of the memcached server, and a 'port' (int) attribute that is the port number on which memcached is running on the server. All of the existing memcached API functions are implemented except getStats() and getVersion(), which are server-specific. <?php class MemcachedAggregator { var $connections; public function __construct($servers) { // Attempt to establish/retrieve persistent connections to all servers. // If any of them fail, they just don't get put into our list of active // connections. $this->connections = array(); for ($i = 0, $n = count($servers); $i < $n; $i++) { $server = $servers[$i]; $con = memcache_pconnect($server['host'], $server['port']); if (!($con == false)) { $this->connections[] = $con; } } } private function _getConForKey($key) { $hashCode = 0; for ($i = 0, $len = strlen($key); $i < $len; $i++) { $hashCode = (int)(($hashCode*33)+ord($key[$i])) & 0x7fffffff; } if (($ns = count($this->connections)) > 0) { return $this->connections[$hashCode%$ns]; } return false; } public function debug($on_off) { $result = false; for ($i = 0; $i < count($connections); $i++) { if ($this->connections[$i]->debug($on_off)) $result = true; } return $result; } public function flush() { $result = false; for ($i = 0; $i < count($connections); $i++) { if ($this->connections[$i]->flush()) $result = true; } return $result; } /// The following are not implemented: ///getStats() ///getVersion() public function get($key) { if (is_array($key)) { $dest = array(); foreach ($key as $subkey) { $val = get($subkey); if (!($val === false)) $dest[$subkey] = $val; } return $dest; } else { return $this->_getConForKey($key)->get($key); } } public function set($key, $var, $compress=0, $expire=0) { return $this->_getConForKey($key)->set($key, $var, $compress, $expire); } public function add($key, $var, $compress=0, $expire=0) { return $this->_getConForKey($key)->add($key, $var, $compress, $expire); } public function replace($key, $var, $compress=0, $expire=0) { return $this->_getConForKey($key)->replace ($key, $var, $compress, $expire); } public function delete($key, $timeout=0) { return $this->_getConForKey($key)->delete($key, $timeout); } public function increment($key, $value=1) { return $this->_getConForKey($key)->increment($key, $value); } public function decrement($key, $value=1) { return $this->_getConForKey($key)->decrement($key, $value); } } ?>