[ 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  // SPDX-FileCopyrightText: 2004-2023 Ryan Parman, Sam Sneddon, Ryan McCue
   4  // SPDX-License-Identifier: BSD-3-Clause
   5  
   6  declare(strict_types=1);
   7  
   8  namespace SimplePie\Cache;
   9  
  10  use Memcache as NativeMemcache;
  11  
  12  /**
  13   * Caches data to memcache
  14   *
  15   * Registered for URLs with the "memcache" protocol
  16   *
  17   * For example, `memcache://localhost:11211/?timeout=3600&prefix=sp_` will
  18   * connect to memcache on `localhost` on port 11211. All tables will be
  19   * prefixed with `sp_` and data will expire after 3600 seconds
  20   *
  21   * @uses Memcache
  22   * @deprecated since SimplePie 1.8.0, use implementation of "Psr\SimpleCache\CacheInterface" instead
  23   */
  24  class Memcache implements Base
  25  {
  26      /**
  27       * Memcache instance
  28       *
  29       * @var NativeMemcache
  30       */
  31      protected $cache;
  32  
  33      /**
  34       * Options
  35       *
  36       * @var array<string, mixed>
  37       */
  38      protected $options;
  39  
  40      /**
  41       * Cache name
  42       *
  43       * @var string
  44       */
  45      protected $name;
  46  
  47      /**
  48       * Create a new cache object
  49       *
  50       * @param string $location Location string (from SimplePie::$cache_location)
  51       * @param string $name Unique ID for the cache
  52       * @param Base::TYPE_FEED|Base::TYPE_IMAGE $type Either TYPE_FEED for SimplePie data, or TYPE_IMAGE for image data
  53       */
  54      public function __construct(string $location, string $name, $type)
  55      {
  56          $this->options = [
  57              'host' => '127.0.0.1',
  58              'port' => 11211,
  59              'extras' => [
  60                  'timeout' => 3600, // one hour
  61                  'prefix' => 'simplepie_',
  62              ],
  63          ];
  64          $this->options = array_replace_recursive($this->options, \SimplePie\Cache::parse_URL($location));
  65  
  66          $this->name = $this->options['extras']['prefix'] . md5("$name:$type");
  67  
  68          $this->cache = new NativeMemcache();
  69          $this->cache->addServer($this->options['host'], (int) $this->options['port']);
  70      }
  71  
  72      /**
  73       * Save data to the cache
  74       *
  75       * @param array<mixed>|\SimplePie\SimplePie $data Data to store in the cache. If passed a SimplePie object, only cache the $data property
  76       * @return bool Successfulness
  77       */
  78      public function save($data)
  79      {
  80          if ($data instanceof \SimplePie\SimplePie) {
  81              $data = $data->data;
  82          }
  83          return $this->cache->set($this->name, serialize($data), MEMCACHE_COMPRESSED, (int) $this->options['extras']['timeout']);
  84      }
  85  
  86      /**
  87       * Retrieve the data saved to the cache
  88       *
  89       * @return array<mixed>|false Data for SimplePie::$data
  90       */
  91      public function load()
  92      {
  93          $data = $this->cache->get($this->name);
  94  
  95          if ($data !== false) {
  96              return unserialize($data);
  97          }
  98          return false;
  99      }
 100  
 101      /**
 102       * Retrieve the last modified time for the cache
 103       *
 104       * @return int|false Timestamp
 105       */
 106      public function mtime()
 107      {
 108          $data = $this->cache->get($this->name);
 109  
 110          if ($data !== false) {
 111              // essentially ignore the mtime because Memcache expires on its own
 112              return time();
 113          }
 114  
 115          return false;
 116      }
 117  
 118      /**
 119       * Set the last modified time to the current time
 120       *
 121       * @return bool Success status
 122       */
 123      public function touch()
 124      {
 125          $data = $this->cache->get($this->name);
 126  
 127          if ($data !== false) {
 128              return $this->cache->set($this->name, $data, MEMCACHE_COMPRESSED, (int) $this->options['extras']['timeout']);
 129          }
 130  
 131          return false;
 132      }
 133  
 134      /**
 135       * Remove the cache
 136       *
 137       * @return bool Success status
 138       */
 139      public function unlink()
 140      {
 141          return $this->cache->delete($this->name, 0);
 142      }
 143  }
 144  
 145  class_alias('SimplePie\Cache\Memcache', 'SimplePie_Cache_Memcache');


Generated : Wed Sep 17 08:20:04 2025 Cross-referenced by PHPXref