[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

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

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


Generated: Sat Nov 23 01:00:02 2024 Cross-referenced by PHPXref 0.7.1