[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-forums/bbpress/bb-includes/backpress/ -> class.wp-object-cache-memcached.php (source)

   1  <?php
   2  
   3  class WP_Object_Cache
   4  {
   5      // WordPress would need to be initialised with this:
   6      // wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss' ) );
   7      var $global_groups = array (
   8          '_cache_keys'
   9      );
  10  
  11      // WordPress would need to be initialised with this:
  12      // wp_cache_add_non_persistent_groups( array( 'comment', 'counts' ) );
  13      var $no_mc_groups = array();
  14  
  15      var $cache = array();
  16      var $mc = array();
  17      var $stats = array();
  18      var $group_ops = array();
  19  
  20      var $default_expiration = 0;
  21  
  22  	function __construct()
  23      {
  24          global $memcached_servers;
  25  
  26          if ( isset( $memcached_servers ) ) {
  27              $buckets = $memcached_servers;
  28          } else {
  29              $buckets = array('default' => array('127.0.0.1:11211'));
  30          }
  31  
  32          foreach ( $buckets as $bucket => $servers ) {
  33              $this->mc[$bucket] = new Memcache();
  34              foreach ( $servers as $server ) {
  35                  list ( $node, $port ) = explode( ':', $server );
  36                  $this->mc[$bucket]->addServer( $node, $port, true, 1, 1, 15, true, array( $this, 'failure_callback' ) );
  37                  $this->mc[$bucket]->setCompressThreshold( 20000, 0.2 );
  38              }
  39          }
  40      }
  41  
  42  	function WP_Object_Cache()
  43      {
  44          $this->__construct();
  45      }
  46  
  47      function &get_mc( $group )
  48      {
  49          if ( isset( $this->mc[$group] ) ) {
  50              return $this->mc[$group];
  51          }
  52          return $this->mc['default'];
  53      }
  54  
  55  	function failure_callback( $host, $port )
  56      {
  57          //error_log( "Connection failure for $host:$port\n", 3, '/tmp/memcached.txt' );
  58      }
  59  
  60  	function close()
  61      {
  62          foreach ( $this->mc as $bucket => $mc ) {
  63              $mc->close();
  64          }
  65      }
  66  
  67  	function add_global_groups( $groups )
  68      {
  69          if ( !is_array( $groups ) ) {
  70              $groups = (array) $groups;
  71          }
  72  
  73          $this->global_groups = array_merge( $this->global_groups, $groups );
  74          $this->global_groups = array_unique( $this->global_groups );
  75      }
  76  
  77  	function add_non_persistent_groups( $groups )
  78      {
  79          if ( !is_array( $groups ) ) {
  80              $groups = (array) $groups;
  81          }
  82  
  83          $this->no_mc_groups = array_merge( $this->no_mc_groups, $groups );
  84          $this->no_mc_groups = array_unique( $this->no_mc_groups );
  85      }
  86  
  87  	function key( $key, $group )
  88      {
  89          if ( empty( $group ) ) {
  90              $group = 'default';
  91          }
  92  
  93          if ( false !== array_search( $group, $this->global_groups ) ) {
  94              $prefix = '';
  95          } else {
  96              $prefix = backpress_get_option( 'application_id' ) . ':';
  97          }
  98  
  99          return preg_replace( '/\s+/', '', $prefix . $group . ':' . $key );
 100      }
 101  
 102  	function get( $id, $group = 'default' )
 103      {
 104          $key = $this->key( $id, $group );
 105          $mc =& $this->get_mc( $group );
 106  
 107          if ( isset( $this->cache[$key] ) ) {
 108              $value = $this->cache[$key];
 109          } elseif ( in_array( $group, $this->no_mc_groups ) ) {
 110              $value = false;
 111          } else {
 112              $value = $mc->get($key);
 113          }
 114  
 115          @ ++$this->stats['get'];
 116          $this->group_ops[$group][] = "get $id";
 117  
 118          if ( NULL === $value ) {
 119              $value = false;
 120          }
 121  
 122          $this->cache[$key] = $value;
 123  
 124          if ( 'checkthedatabaseplease' === $value ) {
 125              $value = false;
 126          }
 127  
 128          return $value;
 129      }
 130  
 131      /*
 132      format: $get['group-name'] = array( 'key1', 'key2' );
 133      */
 134  	function get_multi( $groups )
 135      {
 136          $return = array();
 137          foreach ( $groups as $group => $ids ) {
 138              $mc =& $this->get_mc( $group );
 139              foreach ( $ids as $id ) {
 140                  $key = $this->key( $id, $group );
 141                  if ( isset( $this->cache[$key] ) ) {
 142                      $return[$key] = $this->cache[$key];
 143                      continue;
 144                  } elseif ( in_array( $group, $this->no_mc_groups ) ) {
 145                      $return[$key] = false;
 146                      continue;
 147                  } else {
 148                      $return[$key] = $mc->get( $key );
 149                  }
 150              }
 151              if ( $to_get ) {
 152                  $vals = $mc->get_multi( $to_get );
 153                  $return = array_merge( $return, $vals );
 154              }
 155          }
 156  
 157          @ ++$this->stats['get_multi'];
 158          $this->group_ops[$group][] = "get_multi $id";
 159  
 160          $this->cache = array_merge( $this->cache, $return );
 161  
 162          return $return;
 163      }
 164  
 165  	function add( $id, $data, $group = 'default', $expire = 0 )
 166      {
 167          $key = $this->key( $id, $group );
 168  
 169          if ( in_array( $group, $this->no_mc_groups ) ) {
 170              $this->cache[$key] = $data;
 171              return true;
 172          }
 173  
 174          $mc =& $this->get_mc( $group );
 175          $expire = ( $expire == 0 ) ? $this->default_expiration : $expire;
 176          $result = $mc->add( $key, $data, false, $expire );
 177  
 178          @ ++$this->stats['add'];
 179          $this->group_ops[$group][] = "add $id";
 180  
 181          if ( false !== $result ) {
 182              $this->cache[$key] = $data;
 183              $this->add_key_to_group_keys_cache( $key, $group );
 184          }
 185  
 186          return $result;
 187      }
 188  
 189  	function set( $id, $data, $group = 'default', $expire = 0 )
 190      {
 191          $key = $this->key($id, $group);
 192  
 193          if ( isset( $this->cache[$key] ) && 'checkthedatabaseplease' === $this->cache[$key] ) {
 194              return false;
 195          }
 196          $this->cache[$key] = $data;
 197  
 198          if ( in_array( $group, $this->no_mc_groups ) ) {
 199              return true;
 200          }
 201  
 202          $expire = ( $expire == 0 ) ? $this->default_expiration : $expire;
 203          $mc =& $this->get_mc( $group );
 204          $result = $mc->set( $key, $data, false, $expire );
 205  
 206          if ( false !== $result ) {
 207              $this->add_key_to_group_keys_cache($key, $group);
 208          }
 209  
 210          return $result;
 211      }
 212  
 213  	function replace($id, $data, $group = 'default', $expire = 0) {
 214          $key = $this->key($id, $group);
 215          if ( in_array( $group, $this->no_mc_groups ) ) {
 216              $this->cache[$key] = $data;
 217              return true;
 218          }
 219          $expire = ($expire == 0) ? $this->default_expiration : $expire;
 220          $mc =& $this->get_mc($group);
 221          $result = $mc->replace($key, $data, false, $expire);
 222          if ( false !== $result ) {
 223              $this->cache[$key] = $data;
 224              $this->add_key_to_group_keys_cache( $key, $group );
 225          }
 226          return $result;
 227      }
 228  
 229  	function delete( $id, $group = 'default' )
 230      {
 231          $key = $this->key( $id, $group );
 232  
 233          if ( in_array( $group, $this->no_mc_groups ) ) {
 234              unset( $this->cache[$key] );
 235              return true;
 236          }
 237  
 238          $mc =& $this->get_mc( $group );
 239  
 240          $result = $mc->delete( $key );
 241  
 242          @ ++$this->stats['delete'];
 243          $this->group_ops[$group][] = "delete $id";
 244  
 245          if ( false !== $result ) {
 246              unset( $this->cache[$key] );
 247              $this->remove_key_from_group_keys_cache( $key, $group );
 248          }
 249  
 250          return $result; 
 251      }
 252  
 253  	function flush( $group = null )
 254      {
 255          // Get all the group keys
 256          if ( !$_groups = $this->get( 1, '_group_keys' ) ) {
 257              return true;
 258          }
 259  
 260          if ( !is_array( $_groups ) || !count( $_groups ) ) {
 261              return $this->delete( 1, '_group_keys' );
 262          }
 263  
 264          if ( is_null( $group ) ) {
 265              $results = array();
 266              foreach ( $_groups as $_group => $_keys ) {
 267                  $results[] = $this->delete_all_keys_in_group_key_cache( $_group );
 268              }
 269              if ( in_array( false, $results ) ) {
 270                  return false;
 271              }
 272              return true;
 273          }
 274  
 275          return $this->delete_all_keys_in_group_key_cache( $group );
 276      }
 277  
 278      // Update the cache of group keys or add a new cache if it isn't there
 279  	function add_key_to_group_keys_cache( $key, $group )
 280      {
 281          if ( '_group_keys' === $group ) {
 282              return;
 283          }
 284  
 285          //error_log( 'Adding key ' . $key . ' to group ' . $group );
 286  
 287          // Get all the group keys
 288          if ( !$_groups = $this->get( 1, '_group_keys' ) ) {
 289              $_groups = array( $group => array( $key ) );
 290              return $this->add( 1, $_groups, '_group_keys' );
 291          }
 292  
 293          // Don't blow up if it isn't an array
 294          if ( !is_array( $_groups ) ) {
 295              $_groups = array();
 296          }
 297  
 298          // If this group isn't in there, then insert it
 299          if ( !isset( $_groups[$group] ) || !is_array( $_groups[$group] ) ) {
 300              $_groups[$group] = array();
 301          }
 302  
 303          // If it's already there then do nothing
 304          if ( in_array( $key, $_groups[$group] ) ) {
 305              return true;
 306          }
 307  
 308          $_groups[$group][] = $key;
 309  
 310          // Remove duplicates
 311          $_groups[$group] = array_unique( $_groups[$group] );
 312  
 313          return $this->replace( 1, $_groups, '_group_keys' );
 314      }
 315  
 316      // Remove the key from the cache of group keys, delete the cache if it is emptied
 317  	function remove_key_from_group_keys_cache( $key, $group )
 318      {
 319          if ( '_group_keys' === $group ) {
 320              return;
 321          }
 322  
 323          //error_log( 'Removing key ' . $key . ' from group ' . $group );
 324  
 325          // Get all the group keys
 326          if ( !$_groups = $this->get( 1, '_group_keys' ) ) {
 327              return true;
 328          }
 329  
 330          // If group keys are somehow borked delete it all
 331          if ( !is_array( $_groups ) ) {
 332              return $this->delete( 1, '_group_keys' );
 333          }
 334  
 335          // If it's not there, we're good
 336          if (
 337              !isset( $_groups[$group] ) ||
 338              !is_array( $_groups[$group] ) ||
 339              !in_array( $key, $_groups[$group] )
 340          ) {
 341              return true;
 342          }
 343  
 344          // Remove duplicates
 345          $_groups[$group] = array_unique( $_groups[$group] );
 346  
 347          // If there is only one key or no keys in the group then delete the group
 348          if ( 2 > count( $_groups[$group] ) ) {
 349              unset( $_groups[$group] );
 350              return $this->replace( 1, $_groups, '_group_keys' );
 351          }
 352  
 353          // array_unique() made sure there is only one
 354          if ( $_key = array_search( $key, $_groups[$group] ) ) {
 355              unset( $_groups[$group][$_key] );
 356          }
 357  
 358          return $this->replace( 1, $_groups, '_group_keys' );
 359      }
 360  
 361  	function delete_all_keys_in_group_key_cache( $group )
 362      {
 363          if ( '_group_keys' === $group ) {
 364              return;
 365          }
 366  
 367          //error_log( 'Deleting all keys in group ' . $group );
 368  
 369          // Get all the group keys
 370          if ( !$_groups = $this->get( 1, '_group_keys' ) ) {
 371              //error_log( '--> !!!! No groups' );
 372              return true;
 373          }
 374  
 375          // Check that what we want to loop over is there
 376          if ( !is_array( $_groups ) ) {
 377              //error_log( '--> !!!! Groups is not an array, delete whole key' );
 378              return $this->delete( 1, '_group_keys' );
 379          }
 380  
 381          // Check that what we want to loop over is there
 382          if (
 383              !isset( $_groups[$group] ) ||
 384              !is_array( $_groups[$group] )
 385          ) {
 386              //error_log( '--> !!!! No specific group' );
 387              return true;
 388          }
 389  
 390          $_groups[$group] = array_unique( $_groups[$group] );
 391  
 392          $_remaining_keys = array();
 393          $mc =& $this->get_mc($group);
 394          foreach ( $_groups[$group] as $_key ) {
 395              //error_log( '--> Deleting key ' . $_key );
 396              if ( false !== $mc->delete( $_key ) ) {
 397                  //error_log( '--> Deleted key ' . $_key );
 398                  unset( $this->cache[$_key] );
 399              }
 400          }
 401  
 402          unset( $_groups[$group] );
 403          if ( count( $_groups ) ) {
 404              //error_log( '--> Remove single group' );
 405              return $this->replace( 1, $_groups, '_group_keys' );
 406          }
 407  
 408          //error_log( '--> No groups left, delete whole key' );
 409          return $this->delete( 1, '_group_keys' );
 410      }
 411  
 412  	function incr( $id, $n, $group )
 413      {
 414          $key = $this->key( $id, $group );
 415          $mc =& $this->get_mc( $group );
 416  
 417          return $mc->increment( $key, $n );
 418      }
 419  
 420  	function decr( $id, $n, $group )
 421      {
 422          $key = $this->key( $id, $group );
 423          $mc =& $this->get_mc( $group );
 424  
 425          return $mc->decrement( $key, $n );
 426      }
 427  
 428  	function colorize_debug_line( $line )
 429      {
 430          $colors = array(
 431              'get' => 'green',
 432              'set' => 'purple',
 433              'add' => 'blue',
 434              'delete' => 'red'
 435          );
 436  
 437          $cmd = substr( $line, 0, strpos( $line, ' ' ) );
 438  
 439          $cmd2 = "<span style='color:{$colors[$cmd]}'>$cmd</span>";
 440  
 441          return $cmd2 . substr( $line, strlen( $cmd ) ) . "\n";
 442      }
 443  
 444  	function stats()
 445      {
 446          echo "<p>\n";
 447          foreach ( $this->stats as $stat => $n ) {
 448              echo "<strong>$stat</strong> $n";
 449              echo "<br/>\n";
 450          }
 451          echo "</p>\n";
 452          echo "<h3>Memcached:</h3>";
 453          foreach ( $this->group_ops as $group => $ops ) {
 454              if ( !isset( $_GET['debug_queries'] ) && 500 < count( $ops ) ) { 
 455                  $ops = array_slice( $ops, 0, 500 ); 
 456                  echo "<big>Too many to show! <a href='" . add_query_arg( 'debug_queries', 'true' ) . "'>Show them anyway</a>.</big>\n";
 457              } 
 458              echo "<h4>$group commands</h4>";
 459              echo "<pre>\n";
 460              $lines = array();
 461              foreach ( $ops as $op ) {
 462                  $lines[] = $this->colorize_debug_line( $op ); 
 463              }
 464              print_r( $lines );
 465              echo "</pre>\n";
 466          }
 467  
 468          if ( $this->debug ) {
 469              var_dump( $this->memcache_debug );
 470          }
 471      }
 472  }


Generated: Thu Nov 21 01:00:57 2024 Cross-referenced by PHPXref 0.7.1