[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/SimplePie/src/Cache/ -> Memcache.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 Memcache as NativeMemcache;
  48  
  49  /**
  50   * Caches data to memcache
  51   *
  52   * Registered for URLs with the "memcache" protocol
  53   *
  54   * For example, `memcache://localhost:11211/?timeout=3600&prefix=sp_` will
  55   * connect to memcache on `localhost` on port 11211. All tables will be
  56   * prefixed with `sp_` and data will expire after 3600 seconds
  57   *
  58   * @package SimplePie
  59   * @subpackage Caching
  60   * @uses Memcache
  61   * @deprecated since SimplePie 1.8.0, use implementation of "Psr\SimpleCache\CacheInterface" instead
  62   */
  63  class Memcache implements Base
  64  {
  65      /**
  66       * Memcache instance
  67       *
  68       * @var Memcache
  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, $type)
  94      {
  95          $this->options = [
  96              'host' => '127.0.0.1',
  97              'port' => 11211,
  98              'extras' => [
  99                  'timeout' => 3600, // one hour
 100                  'prefix' => 'simplepie_',
 101              ],
 102          ];
 103          $this->options = array_replace_recursive($this->options, \SimplePie\Cache::parse_URL($location));
 104  
 105          $this->name = $this->options['extras']['prefix'] . md5("$name:$type");
 106  
 107          $this->cache = new NativeMemcache();
 108          $this->cache->addServer($this->options['host'], (int) $this->options['port']);
 109      }
 110  
 111      /**
 112       * Save data to the cache
 113       *
 114       * @param array|\SimplePie\SimplePie $data Data to store in the cache. If passed a SimplePie object, only cache the $data property
 115       * @return bool Successfulness
 116       */
 117      public function save($data)
 118      {
 119          if ($data instanceof \SimplePie\SimplePie) {
 120              $data = $data->data;
 121          }
 122          return $this->cache->set($this->name, serialize($data), MEMCACHE_COMPRESSED, (int) $this->options['extras']['timeout']);
 123      }
 124  
 125      /**
 126       * Retrieve the data saved to the cache
 127       *
 128       * @return array Data for SimplePie::$data
 129       */
 130      public function load()
 131      {
 132          $data = $this->cache->get($this->name);
 133  
 134          if ($data !== false) {
 135              return unserialize($data);
 136          }
 137          return false;
 138      }
 139  
 140      /**
 141       * Retrieve the last modified time for the cache
 142       *
 143       * @return int Timestamp
 144       */
 145      public function mtime()
 146      {
 147          $data = $this->cache->get($this->name);
 148  
 149          if ($data !== false) {
 150              // essentially ignore the mtime because Memcache expires on its own
 151              return time();
 152          }
 153  
 154          return false;
 155      }
 156  
 157      /**
 158       * Set the last modified time to the current time
 159       *
 160       * @return bool Success status
 161       */
 162      public function touch()
 163      {
 164          $data = $this->cache->get($this->name);
 165  
 166          if ($data !== false) {
 167              return $this->cache->set($this->name, $data, MEMCACHE_COMPRESSED, (int) $this->options['extras']['timeout']);
 168          }
 169  
 170          return false;
 171      }
 172  
 173      /**
 174       * Remove the cache
 175       *
 176       * @return bool Success status
 177       */
 178      public function unlink()
 179      {
 180          return $this->cache->delete($this->name, 0);
 181      }
 182  }
 183  
 184  class_alias('SimplePie\Cache\Memcache', 'SimplePie_Cache_Memcache');


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