[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/maximebf/debugbar/src/DebugBar/Storage/ -> MemcachedStorage.php (source)

   1  <?php
   2  /*
   3   * This file is part of the DebugBar package.
   4   *
   5   * (c) 2013 Maxime Bouroumeau-Fuseau
   6   *
   7   * For the full copyright and license information, please view the LICENSE
   8   * file that was distributed with this source code.
   9   */
  10  
  11  namespace DebugBar\Storage;
  12  
  13  use Memcached;
  14  use ReflectionMethod;
  15  
  16  /**
  17   * Stores collected data into Memcache using the Memcached extension
  18   */
  19  class MemcachedStorage implements StorageInterface
  20  {
  21      protected $memcached;
  22  
  23      protected $keyNamespace;
  24  
  25      protected $expiration;
  26  
  27      protected $newGetMultiSignature;
  28  
  29      /**
  30       * @param Memcached $memcached
  31       * @param string $keyNamespace Namespace for Memcached key names (to avoid conflict with other Memcached users).
  32       * @param int $expiration Expiration for Memcached entries (see Expiration Times in Memcached documentation).
  33       */
  34      public function __construct(Memcached $memcached, $keyNamespace = 'phpdebugbar', $expiration = 0)
  35      {
  36          $this->memcached = $memcached;
  37          $this->keyNamespace = $keyNamespace;
  38          $this->expiration = $expiration;
  39      }
  40  
  41      /**
  42       * {@inheritdoc}
  43       */
  44      public function save($id, $data)
  45      {
  46          $key = $this->createKey($id);
  47          $this->memcached->set($key, $data, $this->expiration);
  48          if (!$this->memcached->append($this->keyNamespace, "|$key")) {
  49              $this->memcached->set($this->keyNamespace, $key, $this->expiration);
  50          } else if ($this->expiration) {
  51              // append doesn't support updating expiration, so do it here:
  52              $this->memcached->touch($this->keyNamespace, $this->expiration);
  53          }
  54      }
  55  
  56      /**
  57       * {@inheritdoc}
  58       */
  59      public function get($id)
  60      {
  61          return $this->memcached->get($this->createKey($id));
  62      }
  63  
  64      /**
  65       * {@inheritdoc}
  66       */
  67      public function find(array $filters = array(), $max = 20, $offset = 0)
  68      {
  69          if (!($keys = $this->memcached->get($this->keyNamespace))) {
  70              return array();
  71          }
  72  
  73          $results = array();
  74          $keys = array_reverse(explode('|', $keys)); // Reverse so newest comes first
  75          $keyPosition = 0; // Index in $keys to try to get next items from
  76          $remainingItems = $max + $offset; // Try to obtain this many remaining items
  77          // Loop until we've found $remainingItems matching items or no more items may exist.
  78          while ($remainingItems > 0 && $keyPosition < count($keys)) {
  79              // Consume some keys from $keys:
  80              $itemsToGet = array_slice($keys, $keyPosition, $remainingItems);
  81              $keyPosition += $remainingItems;
  82              // Try to get them, and filter them:
  83              $newItems = $this->memcachedGetMulti($itemsToGet, Memcached::GET_PRESERVE_ORDER);
  84              if ($newItems) {
  85                  foreach ($newItems as $data) {
  86                      $meta = $data['__meta'];
  87                      if ($this->filter($meta, $filters)) {
  88                          $remainingItems--;
  89                          // Keep the result only if we've discarded $offset items first
  90                          if ($offset <= 0) {
  91                              $results[] = $meta;
  92                          } else {
  93                              $offset--;
  94                          }
  95                      }
  96                  }
  97              }
  98          }
  99          return $results;
 100      }
 101  
 102      /**
 103       * Filter the metadata for matches.
 104       * 
 105       * @param  array $meta
 106       * @param  array $filters
 107       * @return bool
 108       */
 109      protected function filter($meta, $filters)
 110      {
 111          foreach ($filters as $key => $value) {
 112              if (!isset($meta[$key]) || fnmatch($value, $meta[$key]) === false) {
 113                  return false;
 114              }
 115          }
 116          return true;
 117      }
 118  
 119      /**
 120       * {@inheritdoc}
 121       */
 122      public function clear()
 123      {
 124          if (!($keys = $this->memcached->get($this->keyNamespace))) {
 125              return;
 126          }
 127          $this->memcached->delete($this->keyNamespace);
 128          $this->memcached->deleteMulti(explode('|', $keys));
 129      }
 130  
 131      /**
 132       * @param  string $id
 133       * @return string 
 134       */
 135      protected function createKey($id)
 136      {
 137          return md5("{$this->keyNamespace}.$id");
 138      }
 139  
 140      /**
 141       * The memcached getMulti function changed in version 3.0.0 to only have two parameters.
 142       *
 143       * @param array $keys
 144       * @param int $flags
 145       */
 146      protected function memcachedGetMulti($keys, $flags)
 147      {
 148          if ($this->newGetMultiSignature === null) {
 149              $this->newGetMultiSignature = (new ReflectionMethod('Memcached', 'getMulti'))->getNumberOfParameters() === 2;
 150          }
 151          if ($this->newGetMultiSignature) {
 152              return $this->memcached->getMulti($keys, $flags);
 153          } else {
 154              $null = null;
 155              return $this->memcached->getMulti($keys, $null, $flags);
 156          }
 157      }
 158  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer