[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

/vendor/simplepie/simplepie/src/Cache/ -> Memcached.php (source)

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