Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.
   1  <?php
   2  
   3  declare(strict_types=1);
   4  /**
   5   * SimplePie
   6   *
   7   * A PHP-Based RSS and Atom Feed Framework.
   8   * Takes the hard work out of managing a complete RSS/Atom solution.
   9   *
  10   * Copyright (c) 2004-2022, Ryan Parman, Sam Sneddon, Ryan McCue, and contributors
  11   * All rights reserved.
  12   *
  13   * Redistribution and use in source and binary forms, with or without modification, are
  14   * permitted provided that the following conditions are met:
  15   *
  16   * 	 * Redistributions of source code must retain the above copyright notice, this list of
  17   * 	   conditions and the following disclaimer.
  18   *
  19   * 	 * Redistributions in binary form must reproduce the above copyright notice, this list
  20   * 	   of conditions and the following disclaimer in the documentation and/or other materials
  21   * 	   provided with the distribution.
  22   *
  23   * 	 * Neither the name of the SimplePie Team nor the names of its contributors may be used
  24   * 	   to endorse or promote products derived from this software without specific prior
  25   * 	   written permission.
  26   *
  27   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
  28   * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  29   * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS
  30   * AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  31   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  32   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  33   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  34   * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  35   * POSSIBILITY OF SUCH DAMAGE.
  36   *
  37   * @package SimplePie
  38   * @copyright 2004-2022 Ryan Parman, Sam Sneddon, Ryan McCue
  39   * @author Ryan Parman
  40   * @author Sam Sneddon
  41   * @author Ryan McCue
  42   * @link http://simplepie.org/ SimplePie
  43   * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  44   */
  45  
  46  namespace SimplePie\Cache;
  47  
  48  use Psr\SimpleCache\CacheInterface;
  49  use Psr\SimpleCache\InvalidArgumentException;
  50  
  51  /**
  52   * Caches data into a PSR-16 cache implementation
  53   *
  54   * @package SimplePie
  55   * @subpackage Caching
  56   * @internal
  57   */
  58  final class Psr16 implements DataCache
  59  {
  60      /**
  61       * PSR-16 cache implementation
  62       *
  63       * @var CacheInterface
  64       */
  65      private $cache;
  66  
  67      /**
  68       * PSR-16 cache implementation
  69       *
  70       * @param CacheInterface $cache
  71       */
  72      public function __construct(CacheInterface $cache)
  73      {
  74          $this->cache = $cache;
  75      }
  76  
  77      /**
  78       * Fetches a value from the cache.
  79       *
  80       * Equivalent to \Psr\SimpleCache\CacheInterface::get()
  81       * <code>
  82       * public function get(string $key, mixed $default = null): mixed;
  83       * </code>
  84       *
  85       * @param string $key     The unique key of this item in the cache.
  86       * @param mixed  $default Default value to return if the key does not exist.
  87       *
  88       * @return array|mixed The value of the item from the cache, or $default in case of cache miss.
  89       *
  90       * @throws InvalidArgumentException
  91       *   MUST be thrown if the $key string is not a legal value.
  92       */
  93      public function get_data(string $key, $default = null)
  94      {
  95          $data = $this->cache->get($key, $default);
  96  
  97          if (! is_array($data) || $data === $default) {
  98              return $default;
  99          }
 100  
 101          return $data;
 102      }
 103  
 104      /**
 105       * Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time.
 106       *
 107       * Equivalent to \Psr\SimpleCache\CacheInterface::set()
 108       * <code>
 109       * public function set(string $key, mixed $value, null|int|\DateInterval $ttl = null): bool;
 110       * </code>
 111       *
 112       * @param string   $key   The key of the item to store.
 113       * @param array    $value The value of the item to store, must be serializable.
 114       * @param null|int $ttl   Optional. The TTL value of this item. If no value is sent and
 115       *                                      the driver supports TTL then the library may set a default value
 116       *                                      for it or let the driver take care of that.
 117       *
 118       * @return bool True on success and false on failure.
 119       *
 120       * @throws InvalidArgumentException
 121       *   MUST be thrown if the $key string is not a legal value.
 122       */
 123      public function set_data(string $key, array $value, ?int $ttl = null): bool
 124      {
 125          return $this->cache->set($key, $value, $ttl);
 126      }
 127  
 128      /**
 129       * Delete an item from the cache by its unique key.
 130       *
 131       * Equivalent to \Psr\SimpleCache\CacheInterface::delete()
 132       * <code>
 133       * public function delete(string $key): bool;
 134       * </code>
 135       *
 136       * @param string $key The unique cache key of the item to delete.
 137       *
 138       * @return bool True if the item was successfully removed. False if there was an error.
 139       *
 140       * @throws InvalidArgumentException
 141       *   MUST be thrown if the $key string is not a legal value.
 142       */
 143      public function delete_data(string $key): bool
 144      {
 145          return $this->cache->delete($key);
 146      }
 147  }