[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/SimplePie/src/Cache/ -> BaseDataCache.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 InvalidArgumentException;
  11  
  12  /**
  13   * Adapter for deprecated \SimplePie\Cache\Base implementations
  14   *
  15   * @internal
  16   */
  17  final class BaseDataCache implements DataCache
  18  {
  19      /**
  20       * @var Base
  21       */
  22      private $cache;
  23  
  24      public function __construct(Base $cache)
  25      {
  26          $this->cache = $cache;
  27      }
  28  
  29      /**
  30       * Fetches a value from the cache.
  31       *
  32       * Equivalent to \Psr\SimpleCache\CacheInterface::get()
  33       * <code>
  34       * public function get(string $key, mixed $default = null): mixed;
  35       * </code>
  36       *
  37       * @param string $key     The unique key of this item in the cache.
  38       * @param mixed  $default Default value to return if the key does not exist.
  39       *
  40       * @return array|mixed The value of the item from the cache, or $default in case of cache miss.
  41       *
  42       * @throws InvalidArgumentException
  43       *   MUST be thrown if the $key string is not a legal value.
  44       */
  45      public function get_data(string $key, $default = null)
  46      {
  47          $data = $this->cache->load();
  48  
  49          if (!is_array($data)) {
  50              return $default;
  51          }
  52  
  53          // ignore data if internal cache expiration time is not set
  54          if (!array_key_exists('__cache_expiration_time', $data)) {
  55              return $default;
  56          }
  57  
  58          // ignore data if internal cache expiration time is expired
  59          if ($data['__cache_expiration_time'] < time()) {
  60              return $default;
  61          }
  62  
  63          // remove internal cache expiration time
  64          unset($data['__cache_expiration_time']);
  65  
  66          return $data;
  67      }
  68  
  69      /**
  70       * Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time.
  71       *
  72       * Equivalent to \Psr\SimpleCache\CacheInterface::set()
  73       * <code>
  74       * public function set(string $key, mixed $value, null|int|\DateInterval $ttl = null): bool;
  75       * </code>
  76       *
  77       * @param string   $key   The key of the item to store.
  78       * @param array<mixed> $value The value of the item to store, must be serializable.
  79       * @param null|int $ttl   Optional. The TTL value of this item. If no value is sent and
  80       *                                      the driver supports TTL then the library may set a default value
  81       *                                      for it or let the driver take care of that.
  82       *
  83       * @return bool True on success and false on failure.
  84       *
  85       * @throws InvalidArgumentException
  86       *   MUST be thrown if the $key string is not a legal value.
  87       */
  88      public function set_data(string $key, array $value, ?int $ttl = null): bool
  89      {
  90          if ($ttl === null) {
  91              $ttl = 3600;
  92          }
  93  
  94          // place internal cache expiration time
  95          $value['__cache_expiration_time'] = time() + $ttl;
  96  
  97          return $this->cache->save($value);
  98      }
  99  
 100      /**
 101       * Delete an item from the cache by its unique key.
 102       *
 103       * Equivalent to \Psr\SimpleCache\CacheInterface::delete()
 104       * <code>
 105       * public function delete(string $key): bool;
 106       * </code>
 107       *
 108       * @param string $key The unique cache key of the item to delete.
 109       *
 110       * @return bool True if the item was successfully removed. False if there was an error.
 111       *
 112       * @throws InvalidArgumentException
 113       *   MUST be thrown if the $key string is not a legal value.
 114       */
 115      public function delete_data(string $key): bool
 116      {
 117          return $this->cache->unlink();
 118      }
 119  }


Generated : Thu Sep 18 08:20:05 2025 Cross-referenced by PHPXref