PageRenderTime 32ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/184.168.182.1/wp-content/plugins/welcome-to-wordpress/resources/object_cache/apc.object-cache.php

https://gitlab.com/endomorphosis/falkenstein
PHP | 380 lines | 274 code | 85 blank | 21 comment | 51 complexity | 7aba9c31d335abaa7b9f4570c89ffb8c MD5 | raw file
  1. <?php
  2. /*
  3. Plugin Name: APC Object Cache
  4. Description: APC backend for the WP Object Cache.
  5. Version: 2.0.5
  6. URI: http://txfx.net/wordpress-plugins/apc/
  7. Author: Mark Jaquith
  8. Author URI: http://coveredwebservices.com/
  9. Install this file to wp-content/object-cache.php
  10. Based on Ryan Boren's Memcached object cache backend
  11. http://wordpress.org/extend/plugins/memcached/
  12. */
  13. if ( function_exists( 'apc_fetch' ) ) :
  14. if ( version_compare( '5.2.4', phpversion(), '>=' ) ) {
  15. wp_die( 'The APC object cache backend requires PHP 5.2 or higher. You are running ' . phpversion() . '. Please remove the <code>object-cache.php</code> file from your content directory.' );
  16. }
  17. if ( function_exists( 'wp_cache_add' ) ) {
  18. // Regular die, not wp_die(), because it gets sandboxed and shown in a small iframe
  19. die( '<strong>ERROR:</strong> This is <em>not</em> a plugin, and it should not be activated as one.<br /><br />Instead, <code>' . str_replace( $_SERVER['DOCUMENT_ROOT'], '', __FILE__ ) . '</code> must be moved to <code>' . str_replace( $_SERVER['DOCUMENT_ROOT'], '', trailingslashit( WP_CONTENT_DIR ) ) . 'object-cache.php</code>' );
  20. } else {
  21. // Users with setups where multiple installs share a common wp-config.php can use this
  22. // to guarantee uniqueness for the keys generated by this object cache
  23. if ( !defined( 'WP_APC_KEY_SALT' ) )
  24. define( 'WP_APC_KEY_SALT', 'wp' );
  25. function wp_cache_add( $key, $data, $group = '', $expire = 0 ) {
  26. global $wp_object_cache;
  27. return $wp_object_cache->add( $key, $data, $group, $expire );
  28. }
  29. function wp_cache_incr( $key, $n = 1, $group = '' ) {
  30. global $wp_object_cache;
  31. return $wp_object_cache->incr2( $key, $n, $group );
  32. }
  33. function wp_cache_decr( $key, $n = 1, $group = '' ) {
  34. global $wp_object_cache;
  35. return $wp_object_cache->decr( $key, $n, $group );
  36. }
  37. function wp_cache_close() {
  38. return true;
  39. }
  40. function wp_cache_delete( $key, $group = '' ) {
  41. global $wp_object_cache;
  42. return $wp_object_cache->delete( $key, $group );
  43. }
  44. function wp_cache_flush() {
  45. global $wp_object_cache;
  46. return $wp_object_cache->flush();
  47. }
  48. function wp_cache_get( $key, $group = '', $force = false ) {
  49. global $wp_object_cache;
  50. return $wp_object_cache->get( $key, $group, $force );
  51. }
  52. function wp_cache_init() {
  53. global $wp_object_cache;
  54. $wp_object_cache = new APC_Object_Cache();
  55. }
  56. function wp_cache_replace( $key, $data, $group = '', $expire = 0 ) {
  57. global $wp_object_cache;
  58. return $wp_object_cache->replace( $key, $data, $group, $expire );
  59. }
  60. function wp_cache_set( $key, $data, $group = '', $expire = 0 ) {
  61. global $wp_object_cache;
  62. if ( defined('WP_INSTALLING') == false )
  63. return $wp_object_cache->set( $key, $data, $group, $expire );
  64. else
  65. return $wp_object_cache->delete( $key, $group );
  66. }
  67. function wp_cache_switch_to_blog( $blog_id ) {
  68. global $wp_object_cache;
  69. return $wp_object_cache->switch_to_blog( $blog_id );
  70. }
  71. function wp_cache_add_global_groups( $groups ) {
  72. global $wp_object_cache;
  73. $wp_object_cache->add_global_groups( $groups );
  74. }
  75. function wp_cache_add_non_persistent_groups( $groups ) {
  76. global $wp_object_cache;
  77. $wp_object_cache->add_non_persistent_groups( $groups );
  78. }
  79. class WP_Object_Cache {
  80. var $global_groups = array();
  81. var $no_mc_groups = array();
  82. var $cache = array();
  83. var $stats = array( 'get' => 0, 'delete' => 0, 'add' => 0 );
  84. var $group_ops = array();
  85. var $cache_enabled = true;
  86. var $default_expiration = 0;
  87. var $abspath = '';
  88. var $debug = false;
  89. function add( $id, $data, $group = 'default', $expire = 0 ) {
  90. $key = $this->key( $id, $group );
  91. if ( is_object( $data ) )
  92. $data = clone $data;
  93. $store_data = $data;
  94. if ( is_array( $data ) )
  95. $store_data = new ArrayObject( $data );
  96. if ( in_array( $group, $this->no_mc_groups ) ) {
  97. $this->cache[$key] = $data;
  98. return true;
  99. } elseif ( isset( $this->cache[$key] ) && $this->cache[$key] !== false ) {
  100. return false;
  101. }
  102. $expire = ( $expire == 0 ) ? $this->default_expiration : $expire;
  103. $result = apc_add( $key, $store_data, $expire );
  104. if ( false !== $result ) {
  105. @ ++$this->stats['add'];
  106. $this->group_ops[$group][] = "add $id";
  107. $this->cache[$key] = $data;
  108. }
  109. return $result;
  110. }
  111. function add_global_groups( $groups ) {
  112. if ( !is_array( $groups ) )
  113. $groups = (array) $groups;
  114. $this->global_groups = array_merge( $this->global_groups, $groups );
  115. $this->global_groups = array_unique( $this->global_groups );
  116. }
  117. function add_non_persistent_groups( $groups ) {
  118. if ( !is_array( $groups ) )
  119. $groups = (array) $groups;
  120. $this->no_mc_groups = array_merge( $this->no_mc_groups, $groups );
  121. $this->no_mc_groups = array_unique( $this->no_mc_groups );
  122. }
  123. // This is named incr2 because Batcache looks for incr
  124. // We will define that in a class extension if it is available (APC 3.1.1 or higher)
  125. function incr2( $id, $n = 1, $group = 'default' ) {
  126. $key = $this->key( $id, $group );
  127. if ( function_exists( 'apc_inc' ) )
  128. return apc_inc( $key, $n );
  129. else
  130. return false;
  131. }
  132. function decr( $id, $n = 1, $group = 'default' ) {
  133. $key = $this->key( $id, $group );
  134. if ( function_exists( 'apc_dec' ) )
  135. return apc_dec( $id, $n );
  136. else
  137. return false;
  138. }
  139. function close() {
  140. return true;
  141. }
  142. function delete( $id, $group = 'default' ) {
  143. $key = $this->key( $id, $group );
  144. if ( in_array( $group, $this->no_mc_groups ) ) {
  145. unset( $this->cache[$key] );
  146. return true;
  147. }
  148. $result = apc_delete( $key );
  149. @ ++$this->stats['delete'];
  150. $this->group_ops[$group][] = "delete $id";
  151. if ( false !== $result )
  152. unset( $this->cache[$key] );
  153. return $result;
  154. }
  155. function flush() {
  156. // Don't flush if multi-blog.
  157. if ( function_exists( 'is_site_admin' ) || defined( 'CUSTOM_USER_TABLE' ) && defined( 'CUSTOM_USER_META_TABLE' ) )
  158. return true;
  159. $this->cache = array();
  160. return apc_clear_cache( 'user' );
  161. }
  162. function get($id, $group = 'default', $force = false) {
  163. $key = $this->key($id, $group);
  164. if ( isset($this->cache[$key]) && ( !$force || in_array($group, $this->no_mc_groups) ) ) {
  165. if ( is_object( $this->cache[$key] ) )
  166. $value = clone $this->cache[$key];
  167. else
  168. $value = $this->cache[$key];
  169. } else if ( in_array($group, $this->no_mc_groups) ) {
  170. $this->cache[$key] = $value = false;
  171. } else {
  172. $value = apc_fetch( $key );
  173. if ( is_object( $value ) && 'ArrayObject' == get_class( $value ) )
  174. $value = $value->getArrayCopy();
  175. if ( NULL === $value )
  176. $value = false;
  177. $this->cache[$key] = ( is_object( $value ) ) ? clone $value : $value;
  178. }
  179. @ ++$this->stats['get'];
  180. $this->group_ops[$group][] = "get $id";
  181. if ( 'checkthedatabaseplease' === $value ) {
  182. unset( $this->cache[$key] );
  183. $value = false;
  184. }
  185. return $value;
  186. }
  187. function key( $key, $group ) {
  188. if ( empty( $group ) )
  189. $group = 'default';
  190. if ( false !== array_search( $group, $this->global_groups ) )
  191. $prefix = $this->global_prefix;
  192. else
  193. $prefix = $this->blog_prefix;
  194. return WP_APC_KEY_SALT . ':' . $this->abspath . ":$prefix$group:$key";
  195. }
  196. function replace( $id, $data, $group = 'default', $expire = 0 ) {
  197. return $this->set( $id, $data, $group, $expire );
  198. }
  199. function set( $id, $data, $group = 'default', $expire = 0 ) {
  200. $key = $this->key( $id, $group );
  201. if ( isset( $this->cache[$key] ) && ('checkthedatabaseplease' === $this->cache[$key] ) )
  202. return false;
  203. if ( is_object( $data ) )
  204. $data = clone $data;
  205. $store_data = $data;
  206. if ( is_array( $data ) )
  207. $store_data = new ArrayObject( $data );
  208. $this->cache[$key] = $data;
  209. if ( in_array( $group, $this->no_mc_groups ) )
  210. return true;
  211. $expire = ( $expire == 0 ) ? $this->default_expiration : $expire;
  212. $result = apc_store( $key, $store_data, $expire );
  213. return $result;
  214. }
  215. function switch_to_blog( $blog_id ) {
  216. $blog_id = (int) $blog_id;
  217. $this->blog_prefix = ( is_multisite() ? $blog_id : $table_prefix ) . ':';
  218. }
  219. function colorize_debug_line( $line ) {
  220. $colors = array(
  221. 'get' => 'green',
  222. 'set' => 'purple',
  223. 'add' => 'blue',
  224. 'delete' => 'red');
  225. $cmd = substr( $line, 0, strpos( $line, ' ' ) );
  226. $cmd2 = "<span style='color:{$colors[$cmd]}'>$cmd</span>";
  227. return $cmd2 . substr( $line, strlen( $cmd ) ) . "\n";
  228. }
  229. function stats() {
  230. echo "<p>\n";
  231. foreach ( $this->stats as $stat => $n ) {
  232. echo "<strong>$stat</strong> $n";
  233. echo "<br/>\n";
  234. }
  235. echo "</p>\n";
  236. echo "<h3>APC:</h3>";
  237. foreach ( $this->group_ops as $group => $ops ) {
  238. if ( !isset( $_GET['debug_queries'] ) && 500 < count( $ops ) ) {
  239. $ops = array_slice( $ops, 0, 500 );
  240. echo "<big>Too many to show! <a href='" . add_query_arg( 'debug_queries', 'true' ) . "'>Show them anyway</a>.</big>\n";
  241. }
  242. echo "<h4>$group commands</h4>";
  243. echo "<pre>\n";
  244. $lines = array();
  245. foreach ( $ops as $op ) {
  246. $lines[] = $this->colorize_debug_line($op);
  247. }
  248. print_r($lines);
  249. echo "</pre>\n";
  250. }
  251. if ( $this->debug ) {
  252. $apc_info = apc_cache_info();
  253. echo "<p>";
  254. echo "<strong>Cache Hits:</strong> {$apc_info['num_hits']}<br/>\n";
  255. echo "<strong>Cache Misses:</strong> {$apc_info['num_misses']}\n";
  256. echo "</p>\n";
  257. }
  258. }
  259. function WP_Object_Cache() {
  260. $this->abspath = md5( ABSPATH );
  261. global $blog_id, $table_prefix;
  262. $this->global_prefix = '';
  263. $this->blog_prefix = '';
  264. if ( function_exists( 'is_multisite' ) ) {
  265. $this->global_prefix = ( is_multisite() || defined('CUSTOM_USER_TABLE') && defined('CUSTOM_USER_META_TABLE') ) ? '' : $table_prefix;
  266. $this->blog_prefix = ( is_multisite() ? $blog_id : $table_prefix ) . ':';
  267. }
  268. $this->cache_hits =& $this->stats['get'];
  269. $this->cache_misses =& $this->stats['add'];
  270. }
  271. }
  272. if ( function_exists( 'apc_inc' ) ) {
  273. class APC_Object_Cache extends WP_Object_Cache {
  274. function incr( $id, $n = 1, $group = 'default' ) {
  275. return parent::incr2( $id, $n, $group );
  276. }
  277. }
  278. } else {
  279. class APC_Object_Cache extends WP_Object_Cache {
  280. // Blank
  281. }
  282. }
  283. } // !function_exists( 'wp_cache_add' )
  284. else : // No APC
  285. function apc_not_actually_running() {
  286. $GLOBALS['_wp_using_ext_object_cache'] = false;
  287. unset( $GLOBALS['wp_filter']['all'][-100]['apc_not_actually_running'] );
  288. }
  289. $GLOBALS['_wp_using_ext_object_cache'] = false; // This will get overridden as of WP 3.5, so we have to hook in to 'all':
  290. $GLOBALS['wp_filter']['all'][-100]['apc_not_actually_running'] = array( 'function' => 'apc_not_actually_running', 'accepted_args' => 0 );
  291. require_once ( ABSPATH . WPINC . '/cache.php' );
  292. endif;