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  namespace OpenSpout\Reader\XLSX\Manager\SharedStringsCaching;
   6  
   7  use OpenSpout\Reader\Exception\SharedStringNotFoundException;
   8  use RuntimeException;
   9  use SplFixedArray;
  10  
  11  /**
  12   * This class implements the in-memory caching strategy for shared strings.
  13   * This strategy is used when the number of unique strings is low, compared to the memory available.
  14   *
  15   * @internal
  16   */
  17  final class InMemoryStrategy implements CachingStrategyInterface
  18  {
  19      /** @var SplFixedArray<string> Array used to cache the shared strings */
  20      private SplFixedArray $inMemoryCache;
  21  
  22      /** @var bool Whether the cache has been closed */
  23      private bool $isCacheClosed;
  24  
  25      /**
  26       * @param int $sharedStringsUniqueCount Number of unique shared strings
  27       */
  28      public function __construct(int $sharedStringsUniqueCount)
  29      {
  30          $this->inMemoryCache = new SplFixedArray($sharedStringsUniqueCount);
  31          $this->isCacheClosed = false;
  32      }
  33  
  34      /**
  35       * Adds the given string to the cache.
  36       *
  37       * @param string $sharedString      The string to be added to the cache
  38       * @param int    $sharedStringIndex Index of the shared string in the sharedStrings.xml file
  39       */
  40      public function addStringForIndex(string $sharedString, int $sharedStringIndex): void
  41      {
  42          if (!$this->isCacheClosed) {
  43              $this->inMemoryCache->offsetSet($sharedStringIndex, $sharedString);
  44          }
  45      }
  46  
  47      /**
  48       * Closes the cache after the last shared string was added.
  49       * This prevents any additional string from being added to the cache.
  50       */
  51      public function closeCache(): void
  52      {
  53          $this->isCacheClosed = true;
  54      }
  55  
  56      /**
  57       * Returns the string located at the given index from the cache.
  58       *
  59       * @param int $sharedStringIndex Index of the shared string in the sharedStrings.xml file
  60       *
  61       * @return string The shared string at the given index
  62       *
  63       * @throws \OpenSpout\Reader\Exception\SharedStringNotFoundException If no shared string found for the given index
  64       */
  65      public function getStringAtIndex(int $sharedStringIndex): string
  66      {
  67          try {
  68              return $this->inMemoryCache->offsetGet($sharedStringIndex);
  69          } catch (RuntimeException $e) {
  70              throw new SharedStringNotFoundException("Shared string not found for index: {$sharedStringIndex}");
  71          }
  72      }
  73  
  74      /**
  75       * Destroys the cache, freeing memory and removing any created artifacts.
  76       */
  77      public function clearCache(): void
  78      {
  79          $this->inMemoryCache = new SplFixedArray(0);
  80          $this->isCacheClosed = false;
  81      }
  82  }