[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/maximebf/debugbar/src/DebugBar/Storage/ -> RedisStorage.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  /**
  14   * Stores collected data into Redis
  15   */
  16  class RedisStorage implements StorageInterface
  17  {
  18      /** @var \Predis\Client|\Redis */
  19      protected $redis;
  20  
  21      /** @var string */
  22      protected $hash;
  23  
  24      /**
  25       * @param  \Predis\Client|\Redis $redis Redis Client
  26       * @param string $hash
  27       */
  28      public function __construct($redis, $hash = 'phpdebugbar')
  29      {
  30          $this->redis = $redis;
  31          $this->hash = $hash;
  32      }
  33  
  34      /**
  35       * {@inheritdoc}
  36       */
  37      public function save($id, $data)
  38      {
  39          $this->redis->hSet("$this->hash:meta", $id, serialize($data['__meta']));
  40          unset($data['__meta']);
  41          $this->redis->hSet("$this->hash:data", $id, serialize($data));
  42      }
  43  
  44      /**
  45       * {@inheritdoc}
  46       */
  47      public function get($id)
  48      {
  49          return array_merge(unserialize($this->redis->hGet("$this->hash:data", $id)),
  50              array('__meta' => unserialize($this->redis->hGet("$this->hash:meta", $id))));
  51      }
  52  
  53      /**
  54       * {@inheritdoc}
  55       */
  56      public function find(array $filters = [], $max = 20, $offset = 0)
  57      {
  58          $results = [];
  59          $cursor = "0";
  60          $isPhpRedis = get_class($this->redis) === 'Redis';
  61  
  62          do {
  63              if ($isPhpRedis) {
  64                  $data = $this->redis->hScan("$this->hash:meta", $cursor);
  65              } else {
  66                  [$cursor, $data] = $this->redis->hScan("$this->hash:meta", $cursor);
  67              }
  68  
  69              foreach ($data as $meta) {
  70                  if ($meta = unserialize($meta)) {
  71                      if ($this->filter($meta, $filters)) {
  72                          $results[] = $meta;
  73                      }
  74                  }
  75              }
  76          } while($cursor);
  77  
  78          usort($results, static function ($a, $b) {
  79              return $b['utime'] <=> $a['utime'];
  80          });
  81  
  82          return array_slice($results, $offset, $max);
  83      }
  84  
  85      /**
  86       * Filter the metadata for matches.
  87       */
  88      protected function filter($meta, $filters)
  89      {
  90          foreach ($filters as $key => $value) {
  91              if (!isset($meta[$key]) || fnmatch($value, $meta[$key]) === false) {
  92                  return false;
  93              }
  94          }
  95          return true;
  96      }
  97  
  98      /**
  99       * {@inheritdoc}
 100       */
 101      public function clear()
 102      {
 103          $this->redis->del("$this->hash:data");
 104          $this->redis->del("$this->hash:meta");
 105      }
 106  }


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