Documentation 8.0 KB

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