Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP session and memcacheD

Everybody knows there are two extensions for memcache on PHP:

  • memcache
  • memcached

You can use memcache the PHP extension as a session_handler for PHP like so :

session.save_handler = memcache
session.save_path = "tcp://serv01:11211,tcp://serv02:11211,tcp://serv03:11211"

Or you can use memcached like so:

session.save_handler = memcached
session.save_path = "serv01:11211,serv02:11211,serv03:11211"

But how to set other parameters to memcached such as:

  • Memcached::OPT_DISTRIBUTION
  • Memcached::OPT_RETRY_TIMEOUT
  • Memcached::OPT_CONNECT_TIMEOUT

In PHP I will do like so:

$cache = new Memcached();
$cache->addServer('serv01', 11211);
$cache->addServer('serv02', 11211);
$cache->addServer('serv03', 11211);
$cache->setOption(Memcached::OPT_HASH, Memcached::HASH_MD5);
$cache->setOption(Memcached::OPT_DISTRIBUTION, Memcached::DISTRIBUTION_CONSISTENT);
$cache->setOption(Memcached::OPT_CONNECT_TIMEOUT, 150);
$cache->setOption(Memcached::OPT_RETRY_TIMEOUT, 0);

But so far I have not found any documentation or examples of how to set those variables when they are use by the session_handler.

like image 326
zzarbi Avatar asked Jul 12 '11 17:07

zzarbi


2 Answers

After looking trough the source code of both PECL extension and libmemcached itself I finally found my answer in the comment of the blog of the author of the memcached extension.

http://zmievski.org/2009/01/new-memcached-extension

I quote in case his blog disappeared some day:

Andrei said: @Ash, the session save path syntax is not quite the same as the other memcache extension. Try:

session.save_path="127.0.0.1:11211"

Ash Searle said: Is there any documentation for the syntax – i.e. does it handle multiple servers and optional parameters like the earlier memcache implementations? (e.g. save_path="127.0.0.1:11211?persistent=1&timeout=1&retry_interval=15")

Andrei said: @Ash, no, it’s not that advanced. For now, the syntax is the default one that libmemcached parser supports: basically, a comma-separated list of hostname:port entries, where :port is optional.

OR

Rich Choy said: Is there a reference on the Web that explains each connection parameter that appears after host:port? For example what exactly does “timeout=1″ mean?

Andrei said: @Rich, which extension are you using? :) Mine doesn’t support those extra parameters, you must be talking about pecl/memcache one.

AND

Frank Orson said: 1) Does pecl/memcached support UDP on the client? I could not find any info about this. I know that pecl/memcache 3.0.4 supports it.

2) Does pecl/memcached have failover support in the client?

Andrei said: Frank, I’m working on next version (2.0) of the memcached extension. It’ll have UDP support and replication (failover).

If you check the source code of version 2 you can see for example that you can append in the save_path string "PERSISTENT=" and "--SERVER" ((which I don't know how it would be used)

like image 149
zzarbi Avatar answered Oct 16 '22 22:10

zzarbi


You need to write your own session handler and wrap Memcache(d) methods around it.

http://www.php.net/manual/en/function.session-set-save-handler.php

like image 3
Aleksey Korzun Avatar answered Oct 17 '22 00:10

Aleksey Korzun