[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/SimplePie/src/Cache/ -> Memcached.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 Memcached as NativeMemcached;
  48  
  49  /**
  50   * Caches data to memcached
  51   *
  52   * Registered for URLs with the "memcached" protocol
  53   *
  54   * For example, `memcached://localhost:11211/?timeout=3600&prefix=sp_` will
  55   * connect to memcached 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   * @author     Paul L. McNeely
  61   * @uses       Memcached
  62   * @deprecated since SimplePie 1.8.0, use implementation of "Psr\SimpleCache\CacheInterface" instead
  63   */
  64  class Memcached implements Base
  65  {
  66      /**
  67       * NativeMemcached instance
  68       * @var NativeMemcached
  69       */
  70      protected $cache;
  71  
  72      /**
  73       * Options
  74       * @var array
  75       */
  76      protected $options;
  77  
  78      /**
  79       * Cache name
  80       * @var string
  81       */
  82      protected $name;
  83  
  84      /**
  85       * Create a new cache object
  86       * @param string $location Location string (from SimplePie::$cache_location)
  87       * @param string $name Unique ID for the cache
  88       * @param Base::TYPE_FEED|Base::TYPE_IMAGE $type Either TYPE_FEED for SimplePie data, or TYPE_IMAGE for image data
  89       */
  90      public function __construct($location, $name, $type)
  91      {
  92          $this->options = [
  93              'host'   => '127.0.0.1',
  94              'port'   => 11211,
  95              'extras' => [
  96                  'timeout' => 3600, // one hour
  97                  'prefix'  => 'simplepie_',
  98              ],
  99          ];
 100          $this->options = array_replace_recursive($this->options, \SimplePie\Cache::parse_URL($location));
 101  
 102          $this->name = $this->options['extras']['prefix'] . md5("$name:$type");
 103  
 104          $this->cache = new NativeMemcached();
 105          $this->cache->addServer($this->options['host'], (int)$this->options['port']);
 106      }
 107  
 108      /**
 109       * Save data to the cache
 110       * @param array|\SimplePie\SimplePie $data Data to store in the cache. If passed a SimplePie object, only cache the $data property
 111       * @return bool Successfulness
 112       */
 113      public function save($data)
 114      {
 115          if ($data instanceof \SimplePie\SimplePie) {
 116              $data = $data->data;
 117          }
 118  
 119          return $this->setData(serialize($data));
 120      }
 121  
 122      /**
 123       * Retrieve the data saved to the cache
 124       * @return array Data for SimplePie::$data
 125       */
 126      public function load()
 127      {
 128          $data = $this->cache->get($this->name);
 129  
 130          if ($data !== false) {
 131              return unserialize($data);
 132          }
 133          return false;
 134      }
 135  
 136      /**
 137       * Retrieve the last modified time for the cache
 138       * @return int Timestamp
 139       */
 140      public function mtime()
 141      {
 142          $data = $this->cache->get($this->name . '_mtime');
 143          return (int) $data;
 144      }
 145  
 146      /**
 147       * Set the last modified time to the current time
 148       * @return bool Success status
 149       */
 150      public function touch()
 151      {
 152          $data = $this->cache->get($this->name);
 153          return $this->setData($data);
 154      }
 155  
 156      /**
 157       * Remove the cache
 158       * @return bool Success status
 159       */
 160      public function unlink()
 161      {
 162          return $this->cache->delete($this->name, 0);
 163      }
 164  
 165      /**
 166       * Set the last modified time and data to NativeMemcached
 167       * @return bool Success status
 168       */
 169      private function setData($data)
 170      {
 171          if ($data !== false) {
 172              $this->cache->set($this->name . '_mtime', time(), (int)$this->options['extras']['timeout']);
 173              return $this->cache->set($this->name, $data, (int)$this->options['extras']['timeout']);
 174          }
 175  
 176          return false;
 177      }
 178  }
 179  
 180  class_alias('SimplePie\Cache\Memcached', 'SimplePie_Cache_Memcached');


Generated : Thu Nov 21 08:20:01 2024 Cross-referenced by PHPXref