1
memcached module for PHP
2
------------------------
3
This module requires zlib library, used for on-the-fly data (de)compression.
4
Also, you'll need memcached to use it =)
6
The memcached website is here:
7
http://www.danga.com/memcached/
9
You will probably need libevent to install memcached:
10
You can download it here: http://www.monkey.org/~provos/libevent/
14
------------------------
16
Version 3 introduces a new class "MemcachePool" which implements the new API, the
17
old class "Memcache" is still retained (but is deprecated) with the same interface
18
for backwards compatibility. Please note that you need a new memcached version to
19
use the CAS, default value to increment/decrement, append and prepend, and binary
22
New INI directives are available to allow control over protocol, redundancy and hash
23
strategy selection. These are
25
# The binary protocol results in less traffic and is more efficient
26
# for the client and server to generate/parse
28
memcache.protocol = {ascii, binary} # default ascii
30
# When enabled the client sends requests to N servers in parallel, resulting in
31
# a somewhat crude reduncancy or mirroring, suitable when used as a session
34
# If data integrity is of greater importance a real replicating memcached
35
# backend such as "repcached" (http://sourceforge.net/projects/repcached/) is
38
memcache.redundancy = <int> # default 1
39
memcache.session_redundancy = <int> # default 2
41
# Hash strategy and function selection. The consistent hashing strategy
42
# is now the default as it allows servers to be added and removed from
43
# the pool without resulting in all or most keys being re-mapped to
44
# other server (ie. voiding the cache)
46
memcache.hash_strategy = {standard, consistent} # default consistent
47
memcache.hash_function = {crc32, fnv} # default crc32
49
# Compression is enabled by default, the threshold which control the minimum
50
# string length which triggers compresssion can be changed as
52
memcache.compress_threshold = <int> # default 20000
55
The directives are used by the MemcachePool constructor so you can instantiate
56
several pools with different settings by using ini_set() creativly. For example
58
ini_set('memcache.protocol', 'binary');
60
$binarypool = new MemcachePool();
61
$binarypool->addServer(...)
63
ini_set('memcache.protocol', 'ascii');
64
ini_set('memcache.redundancy', '2');
66
$redundantpool = new MemcachePool();
67
$redundantpool->addServer(...)
69
ini_set('memcache.redundancy', '1');
72
The new interface looks like
74
class MemcachePool() {
75
bool connect(string host, int tcp_port = 11211, int udp_port = 0, bool persistent = true, int weight = 1, int timeout = 1, int retry_interval = 15)
76
bool addServer(string host, int tcp_port = 11211, int udp_port = 0, bool persistent = true, int weight = 1, int timeout = 1, int retry_interval = 15, bool status = true)
77
bool setServerParams(string host, int tcp_port = 11211, int timeout = 1, int retry_interval = 15, bool status = true)
80
* Supports fetching flags and CAS values
82
mixed get(mixed key, mixed &flags = null, mixed &cas = null)
85
* Supports multi-set, for example
86
* $memcache->set(array('key1' => 'val1', 'key2' => 'val1'), null, 0, 60)
88
bool add(mixed key, mixed var = null, int flag = 0, int exptime = 0)
89
bool set(mixed key, mixed var = null, int flag = 0, int exptime = 0)
90
bool replace(mixed key, mixed var = null, int flag = 0, int exptime = 0)
93
* Compare-and-Swap, uses the CAS param from MemcachePool::get()
95
bool cas(mixed key, mixed var = null, int flag = 0, int exptime = 0, int cas = 0)
98
* Prepends/appends a value to an existing one
100
bool append(mixed key, mixed var = null, int flag = 0, int exptime = 0)
101
bool prepend(mixed key, mixed var = null, int flag = 0, int exptime = 0)
104
* Supports multi-key operations, for example
105
* $memcache->delete(array('key1', 'key2'))
107
bool delete(mixed key, int exptime = 0)
110
* Supports multi-key operations, for example
111
* $memcache->increment(array('key1', 'key2'), 1, 0, 0)
113
* The new defval (default value) and exptime (expiration time) are used
114
* if the key doesn't already exist. They must be supplied (even if 0) for
115
* this to be enabled.
117
* Returns an integer with the new value if key is a string
118
* Returns an array of integers if the key is an array
120
mixed increment(mixed key, int value = 1, int defval = 0, int exptime = 0)
121
mixed decrement(mixed key, int value = 1, int defval = 0, int exptime = 0)
124
* Assigns a pool-specific failure callback which will be called when
125
* a request fails. May be null in order to disable callbacks. The callback
126
* receive arguments like
128
* function mycallback($host, $tcp_port, $udp_port, $error, $errnum)
130
* Where $host and $error are strings or null, the other params are integers.
132
bool setFailureCallback(function callback)
135
* Locates the server a given would be hashed to
137
* Returns a string "hostname:port" on success
138
* Returns false on failure such as invalid key
140
string findServer(string key)
145
Mikael Johansson mikael at synd dot info
146
Antony Dovgal tony2001 at phpclub dot net