dweller at devonweller dot com (2009-11-18 09:02:13)
In the memcache PECL extension version 2.2.5, the delete function does not work with memcached 1.4.3. To work around this, here is a version of the memcache PECL extension with a patch provided by harv at pringo dot com applied:
http://code.google.com/p/phpmemcachepatch/downloads/list
To install: download
http://phpmemcachepatch.googlecode.com/files/memcache-2.2.5b.tgz
and do:
pecl install memcache-2.2.5b.tgz
restlessmind at I dot gmail dot HATE dot com dot SPAM (2008-02-19 13:50:10)
If you are trying install this module for PHP 5.2 under Windows but it won't load on startup, make sure you have all your dependencies including the correct thread-safety build of PHP.
For hours I tried to track down why I couldn't load php_memcache.dll, and finally used Dependency Walker (from http://www.dependencywalker.com) on the .dll file. This made me realize that while I was using the version of PHP 5.2.5 that was non-thread safe, the .dll offered by pecl4win was expecting the thread safe version of PHP. Once I rectified this, everything worked fine.
contact at codeassembly dot com (2008-01-30 06:28:27)
Note that there is no way to have your own hashing mechanism, the best way to group a set of objects to a server is to manually select the server to use.
I want to keep all objects for a certain user on the same server.
user_54_description
user_54_info
user_54_comments
user_54_etc
.
.
If I have all these objects on the same server I can get all data with only one call to memcache -> get (get_multi), so this improves performance a lot.
The way I do this is by hashing the User/Object id to to the number of servers.
<?php
$memcache_hosts = array
(
'192.168.1.1',
'192.168.1.2',
'192.168.1.3',
'192.168.1.4',
'192.168.1.5',
);
$server = $id % count($memcache_hosts);
$memcache = new Memcache;
$memcache->connect($memcache_hosts[$server], 11211);
//get or set your objects here
?>
You can leave other objects that you don't want to group to memcache extension to hash and select the appropiate server.
mogmios at gmail dot com (2007-09-30 21:03:28)
When installing the memcache module for Apache/PHP with a non-standard location it seems like there is no way to define the location when configuring for build. You'll get an error complaining about being unable to find php_session.h. I modified configure to try the PREFIX location too and that seems to have worked fine.
if test -f "${prefix}/include/php/ext/session/php_session.h"; then
session_inc_path="${prefix}/include/php"
elif test -f "$abs_srcdir/include/php/ext/session/php_session.h"; then
session_inc_path="$abs_srcdir/include/php"
elif test -f "$abs_srcdir/ext/session/php_session.h"; then
session_inc_path="$abs_srcdir"
elif test -f "$phpincludedir/ext/session/php_session.h"; then
session_inc_path="$phpincludedir"
fi
Colin Guthrie (2007-08-23 02:57:58)
I have noticed a bug/documentation error:
The above docs say the following about the session.save_path arguement:
"Each url may contain parameters which are applied to that server, they are the same as for the Memcache::addServer() method. "
addServer method's docs say:
"port
Point to the port where memcached is listening for connections. This parameter is optional and its default value is 11211. Set this parameter to 0 when using UNIX domain sockets. "
However when specifying the save_path, I have found that with PHP 5.2.3 and memcache 2.1.2 that the port is a *required* part of the string.
Hope that helps some people scratching their heads.
markfrawley at gmail dot com (2007-06-26 01:10:09)
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 (2007-05-18 15:25:32)
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 (2007-04-10 14:45:48)
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;
}
jcastromail at yahoo dot es (2007-01-08 17:28:49)
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 (2006-10-26 14:47:20)
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 (2006-01-19 12:35:50)
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)
Ron (2005-09-14 13:19:49)
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 (2005-09-14 10:20:35)
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);
}
}
?>