Advertisement
tonnynathan

Untitled

Jan 23rd, 2013
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.08 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. Plugin Name: Memcached
  5. Description: Memcached backend for the WP Object Cache.
  6. Version: 2.0.1
  7. Plugin URI: http://wordpress.org/extend/plugins/memcached/
  8. Author: Ryan Boren, Denis de Bernardy, Matt Martz
  9.  
  10. Install this file to wp-content/object-cache.php
  11. */
  12.  
  13. function wp_cache_add($key, $data, $flag = '', $expire = 0) {
  14. global $wp_object_cache;
  15.  
  16. return $wp_object_cache->add($key, $data, $flag, $expire);
  17. }
  18.  
  19. function wp_cache_incr($key, $n = 1, $flag = '') {
  20. global $wp_object_cache;
  21.  
  22. return $wp_object_cache->incr($key, $n, $flag);
  23. }
  24.  
  25. function wp_cache_decr($key, $n = 1, $flag = '') {
  26. global $wp_object_cache;
  27.  
  28. return $wp_object_cache->decr($key, $n, $flag);
  29. }
  30.  
  31. function wp_cache_close() {
  32. global $wp_object_cache;
  33.  
  34. return $wp_object_cache->close();
  35. }
  36.  
  37. function wp_cache_delete($id, $flag = '') {
  38. global $wp_object_cache;
  39.  
  40. return $wp_object_cache->delete($id, $flag);
  41. }
  42.  
  43. function wp_cache_key_delete($key) {
  44. global $wp_object_cache;
  45.  
  46. return $wp_object_cache->key_delete($key);
  47. }
  48.  
  49. function wp_cache_flush() {
  50. global $wp_object_cache;
  51.  
  52. return $wp_object_cache->flush();
  53. }
  54.  
  55. function wp_cache_get($id, $flag = '') {
  56. global $wp_object_cache;
  57.  
  58. return $wp_object_cache->get($id, $flag);
  59. }
  60.  
  61. function wp_cache_init() {
  62. global $wp_object_cache;
  63.  
  64. $wp_object_cache = new WP_Object_Cache();
  65. }
  66.  
  67. function wp_cache_replace($key, $data, $flag = '', $expire = 0) {
  68. global $wp_object_cache;
  69.  
  70. return $wp_object_cache->replace($key, $data, $flag, $expire);
  71. }
  72.  
  73. function wp_cache_set($key, $data, $flag = '', $expire = 0) {
  74. global $wp_object_cache;
  75.  
  76. if ( defined('WP_INSTALLING') == false )
  77. return $wp_object_cache->set($key, $data, $flag, $expire);
  78. else
  79. return $wp_object_cache->delete($key, $flag);
  80. }
  81.  
  82. function wp_cache_add_global_groups( $groups ) {
  83. global $wp_object_cache;
  84.  
  85. $wp_object_cache->add_global_groups($groups);
  86. }
  87.  
  88. function wp_cache_add_non_persistent_groups( $groups ) {
  89. global $wp_object_cache;
  90.  
  91. $wp_object_cache->add_non_persistent_groups($groups);
  92. }
  93.  
  94. class WP_Object_Cache {
  95. var $global_groups = array ('users', 'userlogins', 'usermeta', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss');
  96.  
  97. var $no_mc_groups = array( 'comment', 'counts' );
  98.  
  99. var $autoload_groups = array ('options');
  100.  
  101. var $cache = array();
  102. var $mc = array();
  103. var $stats = array();
  104. var $group_ops = array();
  105.  
  106. var $cache_enabled = true;
  107. var $default_expiration = 0;
  108.  
  109. function add($id, $data, $group = 'default', $expire = 0) {
  110. $key = $this->key($id, $group);
  111.  
  112. if ( in_array($group, $this->no_mc_groups) ) {
  113. $this->cache[$key] = $data;
  114. return true;
  115. } elseif ( isset($this->cache[$key]) && $this->cache[$key] !== false ) {
  116. return false;
  117. }
  118.  
  119. $mc =& $this->get_mc($group);
  120. $expire = ($expire == 0) ? $this->default_expiration : $expire;
  121. $result = $mc->add($key, $data, false, $expire);
  122.  
  123. if ( false !== $result ) {
  124. @ ++$this->stats['add'];
  125. $this->group_ops[$group][] = "add $id";
  126. $this->cache[$key] = $data;
  127. }
  128.  
  129. return $result;
  130. }
  131.  
  132. function add_global_groups($groups) {
  133. if ( ! is_array($groups) )
  134. $groups = (array) $groups;
  135.  
  136. $this->global_groups = array_merge($this->global_groups, $groups);
  137. $this->global_groups = array_unique($this->global_groups);
  138. }
  139.  
  140. function add_non_persistent_groups($groups) {
  141. if ( ! is_array($groups) )
  142. $groups = (array) $groups;
  143.  
  144. $this->no_mc_groups = array_merge($this->no_mc_groups, $groups);
  145. $this->no_mc_groups = array_unique($this->no_mc_groups);
  146. }
  147.  
  148. function incr($id, $n, $group) {
  149. $key = $this->key($id, $group);
  150. $mc =& $this->get_mc($group);
  151.  
  152. return $mc->increment($key, $n);
  153. }
  154.  
  155. function decr($id, $n, $group) {
  156. $key = $this->key($id, $group);
  157. $mc =& $this->get_mc($group);
  158.  
  159. return $mc->decrement($key, $n);
  160. }
  161.  
  162. function close() {
  163.  
  164. foreach ( $this->mc as $bucket => $mc )
  165. $mc->close();
  166. }
  167.  
  168. function delete($id, $group = 'default') {
  169. $key = $this->key($id, $group);
  170.  
  171. if ( in_array($group, $this->no_mc_groups) ) {
  172. unset($this->cache[$key]);
  173. return true;
  174. }
  175.  
  176. $mc =& $this->get_mc($group);
  177.  
  178. $result = $mc->delete($key);
  179.  
  180. @ ++$this->stats['delete'];
  181. $this->group_ops[$group][] = "delete $id";
  182.  
  183. if ( false !== $result )
  184. unset($this->cache[$key]);
  185.  
  186. return $result;
  187. }
  188.  
  189. function key_delete($key) {
  190.  
  191. $mc =& $this->get_mc($group);
  192.  
  193. $result = $mc->delete($key);
  194.  
  195. @ ++$this->stats['delete'];
  196. $this->group_ops[$group][] = "delete $id";
  197.  
  198. if ( false !== $result )
  199. unset($this->cache[$key]);
  200.  
  201. return $result;
  202. }
  203.  
  204. function flush() {
  205. // Don't flush if multi-blog.
  206. if ( function_exists('is_site_admin') || defined('CUSTOM_USER_TABLE') && defined('CUSTOM_USER_META_TABLE') )
  207. return true;
  208.  
  209. $ret = true;
  210. foreach ( array_keys($this->mc) as $group )
  211. $ret &= $this->mc[$group]->flush();
  212. return $ret;
  213. }
  214.  
  215. function get($id, $group = 'default') {
  216. $key = $this->key($id, $group);
  217. $mc =& $this->get_mc($group);
  218.  
  219. if ( isset($this->cache[$key]) )
  220. $value = $this->cache[$key];
  221. else if ( in_array($group, $this->no_mc_groups) )
  222. $value = false;
  223. else
  224. $value = $mc->get($key);
  225.  
  226. @ ++$this->stats['get'];
  227. $this->group_ops[$group][] = "get $id";
  228.  
  229. if ( NULL === $value )
  230. $value = false;
  231.  
  232. $this->cache[$key] = $value;
  233.  
  234. if ( 'checkthedatabaseplease' == $value )
  235. $value = false;
  236.  
  237. return $value;
  238. }
  239.  
  240. function get_multi( $groups ) {
  241. /*
  242. format: $get['group-name'] = array( 'key1', 'key2' );
  243. */
  244. $return = array();
  245. foreach ( $groups as $group => $ids ) {
  246. $mc =& $this->get_mc($group);
  247. foreach ( $ids as $id ) {
  248. $key = $this->key($id, $group);
  249. if ( isset($this->cache[$key]) ) {
  250. $return[$key] = $this->cache[$key];
  251. continue;
  252. } else if ( in_array($group, $this->no_mc_groups) ) {
  253. $return[$key] = false;
  254. continue;
  255. } else {
  256. $return[$key] = $mc->get($key);
  257. }
  258. }
  259. if ( $to_get ) {
  260. $vals = $mc->get_multi( $to_get );
  261. $return = array_merge( $return, $vals );
  262. }
  263. }
  264. @ ++$this->stats['get_multi'];
  265. $this->group_ops[$group][] = "get_multi $id";
  266. $this->cache = array_merge( $this->cache, $return );
  267. return $return;
  268. }
  269.  
  270. function key($key, $group) {
  271. if ( empty($group) )
  272. $group = 'default';
  273.  
  274. if ( false !== array_search($group, $this->global_groups) )
  275. $prefix = $this->global_prefix;
  276. else
  277. $prefix = $this->blog_prefix;
  278.  
  279. return preg_replace('/\s+/', '', "$prefix$group:$key");
  280. }
  281.  
  282. function replace($id, $data, $group = 'default', $expire = 0) {
  283. $key = $this->key($id, $group);
  284. $expire = ($expire == 0) ? $this->default_expiration : $expire;
  285. $mc =& $this->get_mc($group);
  286. $result = $mc->replace($key, $data, false, $expire);
  287. if ( false !== $result )
  288. $this->cache[$key] = $data;
  289. return $result;
  290. }
  291.  
  292. function set($id, $data, $group = 'default', $expire = 0) {
  293. $key = $this->key($id, $group);
  294. if ( isset($this->cache[$key]) && ('checkthedatabaseplease' == $this->cache[$key]) )
  295. return false;
  296. $this->cache[$key] = $data;
  297.  
  298. if ( in_array($group, $this->no_mc_groups) )
  299. return true;
  300.  
  301. $expire = ($expire == 0) ? $this->default_expiration : $expire;
  302. $mc =& $this->get_mc($group);
  303. $result = $mc->set($key, $data, false, $expire);
  304.  
  305. return $result;
  306. }
  307.  
  308. function colorize_debug_line($line) {
  309. $colors = array(
  310. 'get' => 'green',
  311. 'set' => 'purple',
  312. 'add' => 'blue',
  313. 'delete' => 'red');
  314.  
  315. $cmd = substr($line, 0, strpos($line, ' '));
  316.  
  317. $cmd2 = "<span style='color:{$colors[$cmd]}'>$cmd</span>";
  318.  
  319. return $cmd2 . substr($line, strlen($cmd)) . "\n";
  320. }
  321.  
  322. function stats() {
  323. echo "<p>\n";
  324. foreach ( $this->stats as $stat => $n ) {
  325. echo "$stat $n";
  326. echo "
  327. \n";
  328. }
  329. echo "</p>\n";
  330. echo "<h3>Memcached:</h3>";
  331. foreach ( $this->group_ops as $group => $ops ) {
  332. if ( !isset($_GET['debug_queries']) && 500 < count($ops) ) {
  333. $ops = array_slice( $ops, 0, 500 );
  334. echo "<big>Too many to show! Show them anyway.</big>\n";
  335. }
  336. echo "<h4>$group commands</h4>";
  337. echo "
  338. \n";
  339.             $lines = array();
  340.             foreach ( $ops as $op ) {
  341.                 $lines[] = $this->colorize_debug_line($op);
  342.             }
  343.             print_r($lines);
  344.             echo "
  345. \n";
  346. }
  347. if ( $this->debug )
  348. var_dump($this->memcache_debug);
  349. }
  350.  
  351. function &get_mc($group) {
  352. if ( isset($this->mc[$group]) )
  353. return $this->mc[$group];
  354. return $this->mc['default'];
  355. }
  356.  
  357. function failure_callback($host, $port) {
  358. //error_log("Connection failure for $host:$port\n", 3, '/tmp/memcached.txt');
  359. }
  360.  
  361. function WP_Object_Cache() {
  362. global $memcached_servers;
  363.  
  364. if ( isset($memcached_servers) )
  365. $buckets = $memcached_servers;
  366. else
  367. $buckets = array('127.0.0.1');
  368.  
  369. reset($buckets);
  370. if ( is_int(key($buckets)) )
  371. $buckets = array('default' => $buckets);
  372.  
  373. foreach ( $buckets as $bucket => $servers) {
  374. $this->mc[$bucket] = new Memcache();
  375. foreach ( $servers as $server ) {
  376. list ( $node, $port ) = explode(':', $server);
  377. if ( !$port )
  378. $port = ini_get('memcache.default_port');
  379. $port = intval($port);
  380. if ( !$port )
  381. $port = 11211;
  382. $this->mc[$bucket]->addServer($node, $port, true, 1, 1, 15, true, array($this, 'failure_callback'));
  383. $this->mc[$bucket]->setCompressThreshold(20000, 0.2);
  384. }
  385. }
  386.  
  387. global $blog_id, $table_prefix;
  388. $this->global_prefix = ( is_multisite() || defined('CUSTOM_USER_TABLE') && defined('CUSTOM_USER_META_TABLE') ) ? '' : $table_prefix;
  389. $this->blog_prefix = ( is_multisite() ? $blog_id : $table_prefix ) . ':';
  390.  
  391. $this->cache_hits =& $this->stats['get'];
  392. $this->cache_misses =& $this->stats['add'];
  393. }
  394. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement