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


Generated : Fri Oct 10 08:20:03 2025 Cross-referenced by PHPXref