[ Index ]

PHP Cross Reference of GlotPress

title

Body

[close]

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


Generated: Thu May 23 03:59:56 2013 Hosted by follow the white rabbit.