Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.
   1  <?php
   2  
   3  namespace Kevinrob\GuzzleCache\Storage;
   4  
   5  use Kevinrob\GuzzleCache\CacheEntry;
   6  
   7  class WordPressObjectCacheStorage implements CacheStorageInterface
   8  {
   9      /**
  10       * @var string
  11       */
  12      private $group;
  13  
  14      /**
  15       * @param string $group
  16       */
  17      public function __construct($group = 'guzzle')
  18      {
  19          $this->group = $group;
  20      }
  21  
  22      /**
  23       * @param string $key
  24       *
  25       * @return CacheEntry|null the data or false
  26       */
  27      public function fetch($key)
  28      {
  29          try {
  30              $cache = unserialize(wp_cache_get($key, $this->group));
  31              if ($cache instanceof CacheEntry) {
  32                  return $cache;
  33              }
  34          } catch (\Exception $ignored) {
  35              // Don't fail if we can't load it
  36          }
  37  
  38          return null;
  39      }
  40  
  41      /**
  42       * @param string $key
  43       * @param CacheEntry $data
  44       *
  45       * @return bool
  46       */
  47      public function save($key, CacheEntry $data)
  48      {
  49          try {
  50              return wp_cache_set($key, serialize($data), $this->group, $data->getTTL());
  51          } catch (\Exception $ignored) {
  52              // Don't fail if we can't save it
  53          }
  54  
  55          return false;
  56      }
  57  
  58      /**
  59       * @param string $key
  60       *
  61       * @return bool
  62       */
  63      public function delete($key)
  64      {
  65          try {
  66              return wp_cache_delete($key, $this->group);
  67          } catch (\Exception $ignored) {
  68              // Don't fail if we can't delete it
  69          }
  70  
  71          return false;
  72      }
  73  }