[ 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  /**
   4   * SimplePie
   5   *
   6   * A PHP-Based RSS and Atom Feed Framework.
   7   * Takes the hard work out of managing a complete RSS/Atom solution.
   8   *
   9   * Copyright (c) 2004-2022, Ryan Parman, Sam Sneddon, Ryan McCue, and contributors
  10   * All rights reserved.
  11   *
  12   * Redistribution and use in source and binary forms, with or without modification, are
  13   * permitted provided that the following conditions are met:
  14   *
  15   *     * Redistributions of source code must retain the above copyright notice, this list of
  16   *       conditions and the following disclaimer.
  17   *
  18   *     * Redistributions in binary form must reproduce the above copyright notice, this list
  19   *       of conditions and the following disclaimer in the documentation and/or other materials
  20   *       provided with the distribution.
  21   *
  22   *     * Neither the name of the SimplePie Team nor the names of its contributors may be used
  23   *       to endorse or promote products derived from this software without specific prior
  24   *       written permission.
  25   *
  26   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
  27   * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  28   * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS
  29   * AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  30   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  31   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  32   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  33   * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  34   * POSSIBILITY OF SUCH DAMAGE.
  35   *
  36   * @package SimplePie
  37   * @copyright 2004-2016 Ryan Parman, Sam Sneddon, Ryan McCue
  38   * @author Ryan Parman
  39   * @author Sam Sneddon
  40   * @author Ryan McCue
  41   * @link http://simplepie.org/ SimplePie
  42   * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  43   */
  44  
  45  namespace SimplePie\Cache;
  46  
  47  use Redis as NativeRedis;
  48  
  49  /**
  50   * Caches data to redis
  51   *
  52   * Registered for URLs with the "redis" protocol
  53   *
  54   * For example, `redis://localhost:6379/?timeout=3600&prefix=sp_&dbIndex=0` will
  55   * connect to redis on `localhost` on port 6379. All tables will be
  56   * prefixed with `simple_primary-` and data will expire after 3600 seconds
  57   *
  58   * @package SimplePie
  59   * @subpackage Caching
  60   * @uses Redis
  61   * @deprecated since SimplePie 1.8.0, use implementation of "Psr\SimpleCache\CacheInterface" instead
  62   */
  63  class Redis implements Base
  64  {
  65      /**
  66       * Redis instance
  67       *
  68       * @var NativeRedis
  69       */
  70      protected $cache;
  71  
  72      /**
  73       * Options
  74       *
  75       * @var array
  76       */
  77      protected $options;
  78  
  79      /**
  80       * Cache name
  81       *
  82       * @var string
  83       */
  84      protected $name;
  85  
  86      /**
  87       * Create a new cache object
  88       *
  89       * @param string $location Location string (from SimplePie::$cache_location)
  90       * @param string $name Unique ID for the cache
  91       * @param Base::TYPE_FEED|Base::TYPE_IMAGE $type Either TYPE_FEED for SimplePie data, or TYPE_IMAGE for image data
  92       */
  93      public function __construct($location, $name, $options = null)
  94      {
  95          //$this->cache = \flow\simple\cache\Redis::getRedisClientInstance();
  96          $parsed = \SimplePie\Cache::parse_URL($location);
  97          $redis = new NativeRedis();
  98          $redis->connect($parsed['host'], $parsed['port']);
  99          if (isset($parsed['pass'])) {
 100              $redis->auth($parsed['pass']);
 101          }
 102          if (isset($parsed['path'])) {
 103              $redis->select((int)substr($parsed['path'], 1));
 104          }
 105          $this->cache = $redis;
 106  
 107          if (!is_null($options) && is_array($options)) {
 108              $this->options = $options;
 109          } else {
 110              $this->options = [
 111                  'prefix' => 'rss:simple_primary:',
 112                  'expire' => 0,
 113              ];
 114          }
 115  
 116          $this->name = $this->options['prefix'] . $name;
 117      }
 118  
 119      /**
 120       * @param NativeRedis $cache
 121       */
 122      public function setRedisClient(NativeRedis $cache)
 123      {
 124          $this->cache = $cache;
 125      }
 126  
 127      /**
 128       * Save data to the cache
 129       *
 130       * @param array|\SimplePie\SimplePie $data Data to store in the cache. If passed a SimplePie object, only cache the $data property
 131       * @return bool Successfulness
 132       */
 133      public function save($data)
 134      {
 135          if ($data instanceof \SimplePie\SimplePie) {
 136              $data = $data->data;
 137          }
 138          $response = $this->cache->set($this->name, serialize($data));
 139          if ($this->options['expire']) {
 140              $this->cache->expire($this->name, $this->options['expire']);
 141          }
 142  
 143          return $response;
 144      }
 145  
 146      /**
 147       * Retrieve the data saved to the cache
 148       *
 149       * @return array Data for SimplePie::$data
 150       */
 151      public function load()
 152      {
 153          $data = $this->cache->get($this->name);
 154  
 155          if ($data !== false) {
 156              return unserialize($data);
 157          }
 158          return false;
 159      }
 160  
 161      /**
 162       * Retrieve the last modified time for the cache
 163       *
 164       * @return int Timestamp
 165       */
 166      public function mtime()
 167      {
 168          $data = $this->cache->get($this->name);
 169  
 170          if ($data !== false) {
 171              return time();
 172          }
 173  
 174          return false;
 175      }
 176  
 177      /**
 178       * Set the last modified time to the current time
 179       *
 180       * @return bool Success status
 181       */
 182      public function touch()
 183      {
 184          $data = $this->cache->get($this->name);
 185  
 186          if ($data !== false) {
 187              $return = $this->cache->set($this->name, $data);
 188              if ($this->options['expire']) {
 189                  return $this->cache->expire($this->name, $this->options['expire']);
 190              }
 191              return $return;
 192          }
 193  
 194          return false;
 195      }
 196  
 197      /**
 198       * Remove the cache
 199       *
 200       * @return bool Success status
 201       */
 202      public function unlink()
 203      {
 204          return $this->cache->set($this->name, null);
 205      }
 206  }
 207  
 208  class_alias('SimplePie\Cache\Redis', 'SimplePie_Cache_Redis');


Generated : Thu Oct 24 08:20:01 2024 Cross-referenced by PHPXref