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  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  namespace core\local\guzzle;
  18  
  19  use Kevinrob\GuzzleCache\CacheEntry;
  20  use Kevinrob\GuzzleCache\Storage\CacheStorageInterface;
  21  
  22  /**
  23   * Cache storage handler to handle cache objects, TTL etc.
  24   *
  25   * @package    core
  26   * @copyright  2022 safatshahin <safat.shahin@moodle.com>
  27   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  28   */
  29  class cache_storage implements CacheStorageInterface {
  30  
  31      /**
  32       * The cache pool.
  33       *
  34       * @var cache_handler
  35       */
  36      protected $cachepool;
  37  
  38      /**
  39       * The last item retrieved from the cache.
  40       *
  41       * This item is transiently stored so that save() can reuse the cache item
  42       * usually retrieved by fetch() beforehand, instead of requesting it a second time.
  43       *
  44       * @var cache_item|null
  45       */
  46      protected $lastitem;
  47  
  48      /**
  49       * TTL for the cache.
  50       *
  51       * @var int|null time to live.
  52       */
  53      private int $ttl;
  54  
  55      public function __construct(cache_handler $cachepool, ?int $ttl = null) {
  56          $this->cachepool = $cachepool;
  57          $this->ttl = $ttl;
  58      }
  59  
  60      public function fetch($key): ?CacheEntry {
  61          // Refresh the cache files.
  62          if ($this->ttl) {
  63              $this->cachepool->refresh($this->ttl);
  64          }
  65          $item = $this->cachepool->get_item($key, $this->ttl);
  66          $this->lastitem = $item;
  67  
  68          $cache = $item->get();
  69  
  70          if ($cache instanceof CacheEntry) {
  71              return $cache;
  72          }
  73  
  74          return null;
  75      }
  76  
  77      public function save($key, CacheEntry $data): bool {
  78          if ($this->lastitem && $this->lastitem->get_key() === $key) {
  79              $item = $this->lastitem;
  80          } else {
  81              $item = $this->cachepool->get_item($key);
  82          }
  83  
  84          $this->lastitem = null;
  85  
  86          $item->set($data);
  87  
  88          // Check if the TTL is set, otherwise use from data.
  89          $ttl = $this->ttl ?? $data->getTTL();
  90  
  91          if ($ttl === 0) {
  92              // No expiration.
  93              $item->expires_after(null);
  94          } else {
  95              $item->expires_after($ttl);
  96          }
  97  
  98          return $this->cachepool->save($item);
  99      }
 100  
 101      public function delete($key): bool {
 102          if (null !== $this->lastitem && $this->lastitem->get_key() === $key) {
 103              $this->lastitem = null;
 104          }
 105  
 106          return $this->cachepool->delete_item($key);
 107      }
 108  
 109  }