[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/SimplePie/src/Cache/ -> Redis.php (source)

   1  <?php
   2  
   3  // SPDX-FileCopyrightText: 2004-2023 Ryan Parman, Sam Sneddon, Ryan McCue
   4  // SPDX-FileCopyrightText: 2015 Jan Kozak <galvani78@gmail.com>
   5  // SPDX-License-Identifier: BSD-3-Clause
   6  
   7  declare(strict_types=1);
   8  
   9  namespace SimplePie\Cache;
  10  
  11  use Redis as NativeRedis;
  12  
  13  /**
  14   * Caches data to redis
  15   *
  16   * Registered for URLs with the "redis" protocol
  17   *
  18   * For example, `redis://localhost:6379/?timeout=3600&prefix=sp_&dbIndex=0` will
  19   * connect to redis on `localhost` on port 6379. All tables will be
  20   * prefixed with `simple_primary-` and data will expire after 3600 seconds
  21   *
  22   * @uses Redis
  23   * @deprecated since SimplePie 1.8.0, use implementation of "Psr\SimpleCache\CacheInterface" instead
  24   */
  25  class Redis implements Base
  26  {
  27      /**
  28       * Redis instance
  29       *
  30       * @var NativeRedis
  31       */
  32      protected $cache;
  33  
  34      /**
  35       * Options
  36       *
  37       * @var array<string, mixed>
  38       */
  39      protected $options;
  40  
  41      /**
  42       * Cache name
  43       *
  44       * @var string
  45       */
  46      protected $name;
  47  
  48      /**
  49       * Create a new cache object
  50       *
  51       * @param string $location Location string (from SimplePie::$cache_location)
  52       * @param string $name Unique ID for the cache
  53       * @param Base::TYPE_FEED|Base::TYPE_IMAGE|array<string, mixed>|null $options Either TYPE_FEED for SimplePie data, or TYPE_IMAGE for image data
  54       */
  55      public function __construct(string $location, string $name, $options = null)
  56      {
  57          //$this->cache = \flow\simple\cache\Redis::getRedisClientInstance();
  58          $parsed = \SimplePie\Cache::parse_URL($location);
  59          $redis = new NativeRedis();
  60          $redis->connect($parsed['host'], $parsed['port']);
  61          if (isset($parsed['pass'])) {
  62              $redis->auth($parsed['pass']);
  63          }
  64          if (isset($parsed['path'])) {
  65              $redis->select((int)substr($parsed['path'], 1));
  66          }
  67          $this->cache = $redis;
  68  
  69          if (!is_null($options) && is_array($options)) {
  70              $this->options = $options;
  71          } else {
  72              $this->options = [
  73                  'prefix' => 'rss:simple_primary:',
  74                  'expire' => 0,
  75              ];
  76          }
  77  
  78          $this->name = $this->options['prefix'] . $name;
  79      }
  80  
  81      /**
  82       * @param NativeRedis $cache
  83       * @return void
  84       */
  85      public function setRedisClient(NativeRedis $cache)
  86      {
  87          $this->cache = $cache;
  88      }
  89  
  90      /**
  91       * Save data to the cache
  92       *
  93       * @param array<mixed>|\SimplePie\SimplePie $data Data to store in the cache. If passed a SimplePie object, only cache the $data property
  94       * @return bool Successfulness
  95       */
  96      public function save($data)
  97      {
  98          if ($data instanceof \SimplePie\SimplePie) {
  99              $data = $data->data;
 100          }
 101          $response = $this->cache->set($this->name, serialize($data));
 102          if ($this->options['expire']) {
 103              $this->cache->expire($this->name, $this->options['expire']);
 104          }
 105  
 106          return $response;
 107      }
 108  
 109      /**
 110       * Retrieve the data saved to the cache
 111       *
 112       * @return array<mixed>|false Data for SimplePie::$data
 113       */
 114      public function load()
 115      {
 116          $data = $this->cache->get($this->name);
 117  
 118          if ($data !== false) {
 119              return unserialize($data);
 120          }
 121          return false;
 122      }
 123  
 124      /**
 125       * Retrieve the last modified time for the cache
 126       *
 127       * @return int|false Timestamp
 128       */
 129      public function mtime()
 130      {
 131          $data = $this->cache->get($this->name);
 132  
 133          if ($data !== false) {
 134              return time();
 135          }
 136  
 137          return false;
 138      }
 139  
 140      /**
 141       * Set the last modified time to the current time
 142       *
 143       * @return bool Success status
 144       */
 145      public function touch()
 146      {
 147          $data = $this->cache->get($this->name);
 148  
 149          if ($data !== false) {
 150              $return = $this->cache->set($this->name, $data);
 151              if ($this->options['expire']) {
 152                  return $this->cache->expire($this->name, $this->options['expire']);
 153              }
 154              return $return;
 155          }
 156  
 157          return false;
 158      }
 159  
 160      /**
 161       * Remove the cache
 162       *
 163       * @return bool Success status
 164       */
 165      public function unlink()
 166      {
 167          return $this->cache->set($this->name, null);
 168      }
 169  }
 170  
 171  class_alias('SimplePie\Cache\Redis', 'SimplePie_Cache_Redis');


Generated : Sat Oct 18 08:20:04 2025 Cross-referenced by PHPXref