[ 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  /**
   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-2022 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 InvalidArgumentException;
  48  
  49  /**
  50   * Adapter for deprecated \SimplePie\Cache\Base implementations
  51   *
  52   * @package SimplePie
  53   * @subpackage Caching
  54   * @internal
  55   */
  56  final class BaseDataCache implements DataCache
  57  {
  58      /**
  59       * @var Base
  60       */
  61      private $cache;
  62  
  63      public function __construct(Base $cache)
  64      {
  65          $this->cache = $cache;
  66      }
  67  
  68      /**
  69       * Fetches a value from the cache.
  70       *
  71       * Equivalent to \Psr\SimpleCache\CacheInterface::get()
  72       * <code>
  73       * public function get(string $key, mixed $default = null): mixed;
  74       * </code>
  75       *
  76       * @param string $key     The unique key of this item in the cache.
  77       * @param mixed  $default Default value to return if the key does not exist.
  78       *
  79       * @return array|mixed The value of the item from the cache, or $default in case of cache miss.
  80       *
  81       * @throws InvalidArgumentException
  82       *   MUST be thrown if the $key string is not a legal value.
  83       */
  84      public function get_data(string $key, $default = null)
  85      {
  86          $data = $this->cache->load();
  87  
  88          if (!is_array($data)) {
  89              return $default;
  90          }
  91  
  92          // ignore data if internal cache expiration time is not set
  93          if (!array_key_exists('__cache_expiration_time', $data)) {
  94              return $default;
  95          }
  96  
  97          // ignore data if internal cache expiration time is expired
  98          if ($data['__cache_expiration_time'] < time()) {
  99              return $default;
 100          }
 101  
 102          // remove internal cache expiration time
 103          unset($data['__cache_expiration_time']);
 104  
 105          return $data;
 106      }
 107  
 108      /**
 109       * Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time.
 110       *
 111       * Equivalent to \Psr\SimpleCache\CacheInterface::set()
 112       * <code>
 113       * public function set(string $key, mixed $value, null|int|\DateInterval $ttl = null): bool;
 114       * </code>
 115       *
 116       * @param string   $key   The key of the item to store.
 117       * @param array    $value The value of the item to store, must be serializable.
 118       * @param null|int $ttl   Optional. The TTL value of this item. If no value is sent and
 119       *                                      the driver supports TTL then the library may set a default value
 120       *                                      for it or let the driver take care of that.
 121       *
 122       * @return bool True on success and false on failure.
 123       *
 124       * @throws InvalidArgumentException
 125       *   MUST be thrown if the $key string is not a legal value.
 126       */
 127      public function set_data(string $key, array $value, ?int $ttl = null): bool
 128      {
 129          if ($ttl === null) {
 130              $ttl = 3600;
 131          }
 132  
 133          // place internal cache expiration time
 134          $value['__cache_expiration_time'] = time() + $ttl;
 135  
 136          return $this->cache->save($value);
 137      }
 138  
 139      /**
 140       * Delete an item from the cache by its unique key.
 141       *
 142       * Equivalent to \Psr\SimpleCache\CacheInterface::delete()
 143       * <code>
 144       * public function delete(string $key): bool;
 145       * </code>
 146       *
 147       * @param string $key The unique cache key of the item to delete.
 148       *
 149       * @return bool True if the item was successfully removed. False if there was an error.
 150       *
 151       * @throws InvalidArgumentException
 152       *   MUST be thrown if the $key string is not a legal value.
 153       */
 154      public function delete_data(string $key): bool
 155      {
 156          return $this->cache->unlink();
 157      }
 158  }


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