1 Ryan Gilfether <hotrodder@rocketmail.com>
2 http://www.gilfether.com
3 This module is Copyright (c) 2003 Ryan Gilfether.
6 You may distribute under the terms of the GNU General Public License
7 This is free software. IT COMES WITHOUT WARRANTY OF ANY KIND.
9 See the memcached website: http://www.danga.com/memcached/
12 // Takes one parameter, a array of options. The most important key is
13 // options["servers"], but that can also be set later with the set_servers()
14 // method. The servers must be an array of hosts, each of which is
15 // either a scalar of the form <10.0.0.10:11211> or an array of the
16 // former and an integer weight value. (the default weight if
17 // unspecified is 1.) It's recommended that weight values be kept as low
18 // as possible, as this module currently allocates memory for bucket
19 // distribution proportional to the total host weights.
20 // $options["debug"] turns the debugging on if set to true
21 MemCachedClient::MemCachedClient($options);
23 // sets up the list of servers and the ports to connect to
24 // takes an array of servers in the same format as in the constructor
25 MemCachedClient::set_servers($servers);
27 // Retrieves a key from the memcache. Returns the value (automatically
28 // unserialized, if necessary) or FALSE if it fails.
29 // The $key can optionally be an array, with the first element being the
30 // hash value, if you want to avoid making this module calculate a hash
31 // value. You may prefer, for example, to keep all of a given user's
32 // objects on the same memcache server, so you could use the user's
33 // unique id as the hash value.
34 // Possible errors set are:
36 MemCachedClient::get($key);
38 // just like get(), but takes an array of keys, returns FALSE on error
39 // Possible errors set are:
41 MemCachedClient::get_multi($keys)
43 // Unconditionally sets a key to a given value in the memcache. Returns true
44 // if it was stored successfully.
45 // The $key can optionally be an arrayref, with the first element being the
46 // hash value, as described above.
47 // returns TRUE on success else FALSE
48 // Possible errors set are:
51 // MC_ERR_SOCKET_WRITE
54 MemCachedClient::set($key, $value, $exptime);
56 // Like set(), but only stores in memcache if the key doesn't already exist.
57 // returns TRUE on success else FALSE
58 // Possible errors set are:
61 // MC_ERR_SOCKET_WRITE
64 MemCachedClient::add($key, $value, $exptime);
66 // Like set(), but only stores in memcache if the key already exists.
67 // returns TRUE on success else FALSE
68 // Possible errors set are:
71 // MC_ERR_SOCKET_WRITE
74 MemCachedClient::replace($key, $value, $exptime);
76 // removes the key from the MemCache
77 // $time is the amount of time in seconds (or Unix time) until which
78 // the client wishes the server to refuse "add" and "replace" commands
79 // with this key. For this amount of item, the item is put into a
80 // delete queue, which means that it won't possible to retrieve it by
81 // the "get" command, but "add" and "replace" command with this key
82 // will also fail (the "set" command will succeed, however). After the
83 // time passes, the item is finally deleted from server memory.
84 // The parameter $time is optional, and, if absent, defaults to 0
85 // (which means that the item will be deleted immediately and further
86 // storage commands with this key will succeed).
87 // returns TRUE on success else returns FALSE
88 // Possible errors set are:
91 // MC_ERR_SOCKET_WRITE
94 MemCachedClient::delete($key, $time = 0);
96 // Sends a command to the server to atomically increment the value for
97 // $key by $value, or by 1 if $value is undefined. Returns FALSE if $key
98 // doesn't exist on server, otherwise it returns the new value after
99 // incrementing. Value should be zero or greater. Overflow on server
100 // is not checked. Be aware of values approaching 2**32. See decr.
101 // Possible errors set are:
104 // MC_ERR_SOCKET_WRITE
105 // MC_ERR_SOCKET_READ
106 // returns new value on success, else returns FALSE
107 // ONLY WORKS WITH NUMERIC VALUES
108 MemCachedClient::incr($key[, $value]);
110 // Like incr, but decrements. Unlike incr, underflow is checked and new
111 // values are capped at 0. If server value is 1, a decrement of 2
112 // returns 0, not -1.
113 // Possible errors set are:
116 // MC_ERR_SOCKET_WRITE
117 // MC_ERR_SOCKET_READ
118 // returns new value on success, else returns FALSE
119 // ONLY WORKS WITH NUMERIC VALUES
120 MemCachedClient::decr($key[, $value]);
122 // disconnects from all servers
123 MemCachedClient::disconnect_all();
125 // if $do_debug is set to true, will print out
126 // debugging info, else debug is turned off
127 MemCachedClient::set_debug($do_debug);
129 // remove all cached hosts that are no longer good
130 MemCachedClient::forget_dead_hosts();
132 // When a function returns FALSE, an error code is set.
133 // This funtion will return the error code.
134 // See error_string()
135 // returns last error code set
136 MemCachedClient::error()
138 // Returns a string describing the error set in error()
140 // returns a string describing the error code given
141 MemCachedClient::error_string()
143 // Resets the error number and error string
144 MemCachedClient::error_clear()
146 Error codes are as follows:
147 MC_ERR_NOT_ACTIVE // no active servers
148 MC_ERR_SOCKET_WRITE // socket_write() failed
149 MC_ERR_SOCKET_READ // socket_read() failed
150 MC_ERR_SOCKET_CONNECT // failed to connect to host
151 MC_ERR_DELETE // delete() did not recieve DELETED command
152 MC_ERR_HOST_FORMAT // sock_to_host() invalid host format
153 MC_ERR_HOST_DEAD // sock_to_host() host is dead
154 MC_ERR_GET_SOCK // get_sock() failed to find a valid socket
155 MC_ERR_SET // _set() failed to receive the STORED response
156 MC_ERR_LOADITEM_HEADER // _load_items failed to receive valid data header
157 MC_ERR_LOADITEM_END // _load_items failed to receive END response
158 MC_ERR_LOADITEM_BYTES // _load_items bytes read larger than bytes available
159 MC_ERR_GET // failed to get value associated with key
161 // Turns compression on or off; 0=off, 1=on
162 MemCacheClient::set_compression($setting)
166 require 'MemCachedClient.inc.php';
168 // set the servers, with the last one having an integer weight value of 3
169 $options["servers"] = array("10.0.0.15:11000","10.0.0.16:11001",array("10.0.0.17:11002", 3));
170 $options["debug"] = false;
172 $memc = new MemCachedClient($options);
175 /***********************
177 ***********************/
178 $myarr = array("one","two", 3);
179 $memc->set("key_one", $myarr);
180 $val = $memc->get("key_one");
181 print $val[0]."\n"; // prints 'one'
182 print $val[1]."\n"; // prints 'two'
183 print $val[2]."\n"; // prints 3
189 /***********************
191 ***********************/
203 $memc->set("key_two", $t);
204 $val = $memc->get("key_two");
205 print $val->one."\n";
206 print $val->two."\n";
207 print $val->three."\n";
213 /***********************
215 ***********************/
216 $memc->set("key_three", "my string");
217 $val = $memc->get("key_three");
218 print $val; // prints 'my string'
220 $memc->delete("key_one");
221 $memc->delete("key_two");
222 $memc->delete("key_three");
224 $memc->disconnect_all();
231 /***********************
232 * STORE A BINARY FILE
233 ***********************/
235 // first read the file and save it in memcache
236 $fp = fopen( "./image.jpg", "rb" ) ;
239 print "Could not open ./file.dat!\n" ;
242 $data = fread( $fp, filesize( "./image.jpg" ) ) ;
244 print "Data length is " . strlen( $data ) . "\n" ;
245 $memc->set( "key", $data ) ;
247 // now open a file for writing and write the data
248 // retrieved from memcache
249 $fp = fopen("./test.jpg","wb");
250 $data = $memc->get( "key" ) ;
251 print "Data length is " . strlen( $data ) . "\n" ;
252 fwrite($fp,$data,strlen( $data ));