LXXXVI. Memcache Functions

简介

Memcache module provides handy procedural and object oriented interface to memcached, highly effective caching daemon, which was especially designed to decrease database load in dynamic web applications.

More information about memcached can be found at http://www.danga.com/memcached/.

需求

This module uses functions of zlib to support on-the-fly data compression. Zlib is required to install this module.

PHP 4.3.3 or newer is required to use the memcache extension.

安装

PECL 扩展未绑定于 PHP 中。 安装此 PECL 扩展库的信息可在手册中标题为 PECL 扩展库安装的一章中找到。 更多信息如新版本,下载,源文件,维护者信息以及更新日志等可以在这里找到: http://pecl.php.net/package/memcache.

In order to use these functions you must compile PHP with Memcache support by using the --enable-memcache[=DIR] option.

Windows users will enable php_memcache.dll inside of php.ini in order to use these functions. 可以从 PHP 下载页面或者 http://snaps.php.net/ 下载此 PECL 扩展的 DLL 文件。

运行时配置

这些函数的行为受 php.ini 的影响。

表 1. Memcache Configuration Options

NameDefaultChangeableChangelog
memcache.allow_failover"1"PHP_INI_ALLAvailable since Memcache 2.0.2
memcache.max_failover_attempts"20"PHP_INI_ALLAvailable since Memcache 2.1.0
memcache.chunk_size"8192"PHP_INI_ALLAvailable since Memcache 2.0.2
memcache.default_port"11211"PHP_INI_ALLAvailable since Memcache 2.0.2
有关 PHP_INI_* 常量进一步的细节与定义参见附录 H

以下是配置选项的简要解释。

memcache.allow_failover boolean

Whether to transparently failover to other servers on errors.

memcache.max_failover_attempts integer

Defines how many servers to try when setting and getting data. Used only in conjunction with memcache.allow_failover.

memcache.chunk_size integer

Data will be transferred in chunks of this size, setting the value lower requires more network writes. Try increasing this value to 32768 if noticing otherwise inexplicable slowdowns.

memcache.default_port string

The default TCP port number to use when connecting to the memcached server if no other port is specified.

资源类型

There is only one resource type used in memcache module - it's the link identifier for a cache server connection.

预定义常量

表 2. MemCache Constants

NameDescription
MEMCACHE_COMPRESSED (integer) Used to turn on-the-fly data compression on with Memcache::set(), Memcache::add()Memcache::replace().

范例

例 1. memcache extension overview example

<?php

$memcache
= new Memcache;
$memcache->connect('localhost', 11211) or die ("Could not connect");

$version = $memcache->getVersion();
echo
"Server's version: ".$version."<br/>\n";

$tmp_object = new stdClass;
$tmp_object->str_attr = 'test';
$tmp_object->int_attr = 123;

$memcache->set('key', $tmp_object, false, 10) or die ("Failed to save data at the server");
echo
"Store data in the cache (data will expire in 10 seconds)<br/>\n";

$get_result = $memcache->get('key');
echo
"Data from the cache:<br/>\n";

var_dump($get_result);

?>

In the above example, an object is being saved in the cache and then retrieved back. Object and other non-scalar types are serialized before saving, so it's impossible to store resources (i.e. connection identifiers and others) in the cache.

目录
Memcache::add -- Add an item to the server
Memcache::addServer -- Add a memcached server to connection pool
Memcache::close -- Close memcached server connection
Memcache::connect -- Open memcached server connection
memcache_debug -- Turn debug output on/off
Memcache::decrement -- Decrement item's value
Memcache::delete -- Delete item from the server
Memcache::flush -- Flush all existing items at the server
Memcache::get -- Retrieve item from the server
Memcache::getExtendedStats -- Get statistics from all servers in pool
Memcache::getServerStatus -- Returns server status
Memcache::getStats -- Get statistics of the server
Memcache::getVersion -- Return version of the server
Memcache::increment -- Increment item's value
Memcache::pconnect -- Open memcached server persistent connection
Memcache::replace -- Replace value of the existing item
Memcache::set -- Store data at the server
Memcache::setCompressThreshold -- Enable automatic compression of large values
Memcache::setServerParams -- Changes server parameters and status at runtime

add a note add a note User Contributed Notes
markfrawley at gmail dot com
26-Jun-2007 08:10
Here is a PHP4 backport of the above MemcacheAggregator class:

<?php
class MemcachedAggregator {
   var
$connections;
   var
$_servers;

   function
MemcachedAggregator($servers) {
      
$this->_servers = $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_connect($server['host']);
          
//memcache_debug(1);
           //$memcache = &new Memcache;
           //$con = $memcache->connect($server['host']);
          
if (!($con == false)) {
              
$this->connections[] = &$con;
           }
          
       }
   }

   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 $this->connections[0];
      
}
       return
false;
   }

   function
debug($on_off) {
      
$result = false;
       for (
$i = 0; $i < count($this->connections); $i++) {
           if (
$this->connections[$i]->debug($on_off)) $result = true;
       }
       return
$result;
   }

   function
flush() {
      
$result = false;
       for (
$i = 0; $i < count($this->connections); $i++) {
           if (
$this->connections[$i]->flush()) $result = true;
       }
       return
$result;
   }

  
/// The following are not implemented:
   ///getStats()
   ///getVersion()

  
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 {
          
$conn = &$this->_getConForKey($key);
           return
$conn->get($key);
       }
   }

   function
set($key, $var, $compress=0, $expire=0) {
      
$conn = &$this->_getConForKey($key);
       return
$conn->set($key, $var, $compress, $expire);
   }

   function
add($key, $var, $compress=0, $expire=0) {
      
$conn = &$this->_getConForKey($key);
       return
$conn->add($key, $var, $compress, $expire);
   }

   function
replace($key, $var, $compress=0, $expire=0) {
      
$conn = &$this->_getConForKey($key);
       return
$conn->replace($key, $var, $compress, $expire);
   }

   function
delete($key, $timeout=0) {
      
$conn = &$this->_getConForKey($key);
       return
$conn->delete($key, $timeout);
   }

   function
increment($key, $value=1) {
      
$conn = &$this->_getConForKey($key);
       return
$conn->increment($key, $value);
   }

   function
decrement($key, $value=1) {
      
$conn = &$this->_getConForKey($key);
       return
$conn->decrement($key, $value);
   }

   function
showStats($server=null) {
      
$stats_out = '';
       if(
$server == null) {
          
$i=0;
           foreach(
$this->connections as $conn) { 
              
$server = $this->_servers[$i];
              
$stats_array = memcache_get_stats($conn);
              
$stats_out .= "</br><b>Server: ".$server['host'].": </b><br/>";
               foreach(
$stats_array as $key => $val) {
                  
$stats_out .= "$key => $val <br/>";
               } 
              
$i++;
           }
       }
       return
$stats_out;
   }   
}
?>
moazzamk at gmail dot com
18-May-2007 10:25
You can't call parent::__construct() in the constructor of your class if you are extending the memcache class. However, not running the constructor still initiates it and works fine.

<?php

// wrong

class a extends Memcache {
  function
__construct($host, $port)
  {
  
parent::__construct();
  
$this->connect($host, $port);
  }

//correct
class a extends Memcache {
  function
__construct($host, $port)
  {
  
parent::__construct();
  
$this->connect($host, $port);
  }

?>

I am guessing this applies to all native/PECL classes.
gil at squidoo dot com
10-Apr-2007 09:45
There is a tiny bug in the Aggregator class when using the flush() function. The reference to $connections should be $this->connections.

Change:

public function flush() {
  $result = false;
  for ($i = 0; $i < count($connections); $i++) {
   if ($this->connections[$i]->flush()) {
     $result = true;
   }
  }
  return $result;
}

To:

public function flush() {
  $result = false;
  for ($i = 0; $i < count($this->connections); $i++) {
   if ($this->connections[$i]->flush()) {
     $result = true;
   }
  }
  return $result;
}
Roberto Spadim
12-Jan-2007 10:14
You can use tugela cache if you want disk and memory cache

http://meta.wikimedia.org/wiki/Tugela_Cache
jcastromail at yahoo dot es
09-Jan-2007 01:28
Hi there:

For run memcached in a windows box: (tested with latest php,apache and memcache in xp sp2)

a) download the php_memcache.dll it can be found in the pecl file.

b) put the dll in the extension folder (c:/php/extension for example). You cannot miss this folder because they are filled with php*.dll files.  In some cases the extension folder used is the system32, a non-standard way to put dll but still works.

c)configure the php.ini
; i put this like the latest extension
extension=php_memcache.dll

; i'm not sure about this but don't hurts..
[Memcache]
memcache.allow_failover = 1
memcache.max_failover_attempts=20
memcache.chunk_size =8192
memcache.default_port = 11211

d)This is important, memcached works with a EXTERNAL service. This service must be downloaded and installed prior to use the memcache.  I use:  http://jehiah.cz/projects/memcached-win32/

e)Remember to install the service and to start the service memcached.exe -d install for install and run services.msc for start the memcached service (or restart the system).

f) check the firewall ports.

Finally restart the apache/iis and runs some test. At least in phpinfo must show some info about the memcache.

Final notes :The "awe" about memcache is not only can help for speed some process (or reduce the cpu use), also can be used like a global session for store a whole object also this "global session" is shared among all the users, like APPLICATION used in ASP.  So (for example) it's possible to do a user counter without needing of database or writing a file.
ed at me3inc dot com
26-Oct-2006 09:47
Re. Installing the memcache extension:

I had all kinds of troubles getting it hooked up, because in all of the instructions I read I never got the last, most important step - you must have a php.ini and have in it "extension=memcache.so."

So, the steps:
First - ./configure with --enable-memcache. This should show in your phpinfo() at the top (even though nothing of the memcache extension works yet).

Second:
either pecl install memcache
OR
download the source
tar -xzvf [thesourcetarball]
phpize
./configure
make
make install

Finally: Add extension=memcache.so to your php.ini. (If you don't have one, it should go in the root of where php is called ie., /usr/local/lib)

Call phpinfo() and you should see a memcache section.
iliya at pisem dot net
19-Jan-2006 08:35
one more "intelligent" cache aggregator:
https://svn.shadanakar.org/onPHP/ trunk/core/Cache/AggregateCache.class.php
can be used with several cache connectors - memcached, filesystem, etc.
(remove whitespace manually)
Gregor J. Rothfuss
21-Nov-2005 05:18
The next version will have failover. It's been committed three weeks ago. Usage notes here: http://www.codecomments.com/archive367-2005-10-659421.html
Ron
14-Sep-2005 08:19
An improvement to the above:

The above class will cause an error if all cache servers are down.  The preferred behavior is to just have a cache miss (or take no action in the case of write operations) and return false, so the app can run in non-cached mode if all cache servers are down.

To make this happen, simply change the connection usage to look something like this in each affected function.  This code is for the get() function:

       $con = $this->_getConForKey($key);
       if ($con === false) return false;
       return $con->get($key);

Similarly, the affected code in the set() function would look like this:
   $con = $this->_getConForKey($key);
   if ($con === false) return false;
   return $con->set($key, $var, $compress, $expire);

Modify each function accordingly, and if all of your cache servers are down, you can still function (although more slowly due to the 100% cache miss rate).
Ron
14-Sep-2005 05:20
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);
   }

}
?>