[ Index ]

PHP Cross Reference of MediaWiki-1.24.0

title

Body

[close]

/includes/objectcache/ -> MemcachedBagOStuff.php (source)

   1  <?php
   2  /**
   3   * Base class for memcached clients.
   4   *
   5   * This program is free software; you can redistribute it and/or modify
   6   * it under the terms of the GNU General Public License as published by
   7   * the Free Software Foundation; either version 2 of the License, or
   8   * (at your option) any later version.
   9   *
  10   * This program is distributed in the hope that it will be useful,
  11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13   * GNU General Public License for more details.
  14   *
  15   * You should have received a copy of the GNU General Public License along
  16   * with this program; if not, write to the Free Software Foundation, Inc.,
  17   * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18   * http://www.gnu.org/copyleft/gpl.html
  19   *
  20   * @file
  21   * @ingroup Cache
  22   */
  23  
  24  /**
  25   * Base class for memcached clients.
  26   *
  27   * @ingroup Cache
  28   */
  29  class MemcachedBagOStuff extends BagOStuff {
  30      protected $client;
  31  
  32      /**
  33       * Fill in the defaults for any parameters missing from $params, using the
  34       * backwards-compatible global variables
  35       * @param array $params
  36       * @return array
  37       */
  38  	protected function applyDefaultParams( $params ) {
  39          if ( !isset( $params['servers'] ) ) {
  40              $params['servers'] = $GLOBALS['wgMemCachedServers'];
  41          }
  42          if ( !isset( $params['debug'] ) ) {
  43              $params['debug'] = $GLOBALS['wgMemCachedDebug'];
  44          }
  45          if ( !isset( $params['persistent'] ) ) {
  46              $params['persistent'] = $GLOBALS['wgMemCachedPersistent'];
  47          }
  48          if ( !isset( $params['compress_threshold'] ) ) {
  49              $params['compress_threshold'] = 1500;
  50          }
  51          if ( !isset( $params['timeout'] ) ) {
  52              $params['timeout'] = $GLOBALS['wgMemCachedTimeout'];
  53          }
  54          if ( !isset( $params['connect_timeout'] ) ) {
  55              $params['connect_timeout'] = 0.5;
  56          }
  57          return $params;
  58      }
  59  
  60      /**
  61       * @param string $key
  62       * @param mixed $casToken [optional]
  63       * @return mixed
  64       */
  65  	public function get( $key, &$casToken = null ) {
  66          return $this->client->get( $this->encodeKey( $key ), $casToken );
  67      }
  68  
  69      /**
  70       * @param string $key
  71       * @param mixed $value
  72       * @param int $exptime
  73       * @return bool
  74       */
  75  	public function set( $key, $value, $exptime = 0 ) {
  76          return $this->client->set( $this->encodeKey( $key ), $value,
  77              $this->fixExpiry( $exptime ) );
  78      }
  79  
  80      /**
  81       * @param mixed $casToken
  82       * @param string $key
  83       * @param mixed $value
  84       * @param int $exptime
  85       * @return bool
  86       */
  87  	public function cas( $casToken, $key, $value, $exptime = 0 ) {
  88          return $this->client->cas( $casToken, $this->encodeKey( $key ),
  89              $value, $this->fixExpiry( $exptime ) );
  90      }
  91  
  92      /**
  93       * @param string $key
  94       * @param int $time
  95       * @return bool
  96       */
  97  	public function delete( $key, $time = 0 ) {
  98          return $this->client->delete( $this->encodeKey( $key ), $time );
  99      }
 100  
 101      /**
 102       * @param string $key
 103       * @param int $value
 104       * @param int $exptime (default 0)
 105       * @return mixed
 106       */
 107  	public function add( $key, $value, $exptime = 0 ) {
 108          return $this->client->add( $this->encodeKey( $key ), $value,
 109              $this->fixExpiry( $exptime ) );
 110      }
 111  
 112      /**
 113       * Get the underlying client object. This is provided for debugging
 114       * purposes.
 115       * @return BagOStuff
 116       */
 117  	public function getClient() {
 118          return $this->client;
 119      }
 120  
 121      /**
 122       * Encode a key for use on the wire inside the memcached protocol.
 123       *
 124       * We encode spaces and line breaks to avoid protocol errors. We encode
 125       * the other control characters for compatibility with libmemcached
 126       * verify_key. We leave other punctuation alone, to maximise backwards
 127       * compatibility.
 128       * @param string $key
 129       * @return string
 130       */
 131  	public function encodeKey( $key ) {
 132          return preg_replace_callback( '/[\x00-\x20\x25\x7f]+/',
 133              array( $this, 'encodeKeyCallback' ), $key );
 134      }
 135  
 136      /**
 137       * @param array $m
 138       * @return string
 139       */
 140  	protected function encodeKeyCallback( $m ) {
 141          return rawurlencode( $m[0] );
 142      }
 143  
 144      /**
 145       * TTLs higher than 30 days will be detected as absolute TTLs
 146       * (UNIX timestamps), and will result in the cache entry being
 147       * discarded immediately because the expiry is in the past.
 148       * Clamp expiries >30d at 30d, unless they're >=1e9 in which
 149       * case they are likely to really be absolute (1e9 = 2011-09-09)
 150       * @param int $expiry
 151       * @return int
 152       */
 153  	function fixExpiry( $expiry ) {
 154          if ( $expiry > 2592000 && $expiry < 1000000000 ) {
 155              $expiry = 2592000;
 156          }
 157          return (int)$expiry;
 158      }
 159  
 160      /**
 161       * Decode a key encoded with encodeKey(). This is provided as a convenience
 162       * function for debugging.
 163       *
 164       * @param string $key
 165       *
 166       * @return string
 167       */
 168  	public function decodeKey( $key ) {
 169          return urldecode( $key );
 170      }
 171  
 172      /**
 173       * Send a debug message to the log
 174       * @param string $text
 175       */
 176  	protected function debugLog( $text ) {
 177          wfDebugLog( 'memcached', $text );
 178      }
 179  }


Generated: Fri Nov 28 14:03:12 2014 Cross-referenced by PHPXref 0.7.1