Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   1  <?php
   2  
   3  namespace Box\Spout\Reader\XLSX\Manager\SharedStringsCaching;
   4  
   5  use Box\Spout\Reader\Exception\SharedStringNotFoundException;
   6  
   7  /**
   8   * Class InMemoryStrategy
   9   *
  10   * This class implements the in-memory caching strategy for shared strings.
  11   * This strategy is used when the number of unique strings is low, compared to the memory available.
  12   */
  13  class InMemoryStrategy implements CachingStrategyInterface
  14  {
  15      /** @var \SplFixedArray Array used to cache the shared strings */
  16      protected $inMemoryCache;
  17  
  18      /** @var bool Whether the cache has been closed */
  19      protected $isCacheClosed;
  20  
  21      /**
  22       * @param int $sharedStringsUniqueCount Number of unique shared strings
  23       */
  24      public function __construct($sharedStringsUniqueCount)
  25      {
  26          $this->inMemoryCache = new \SplFixedArray($sharedStringsUniqueCount);
  27          $this->isCacheClosed = false;
  28      }
  29  
  30      /**
  31       * Adds the given string to the cache.
  32       *
  33       * @param string $sharedString The string to be added to the cache
  34       * @param int $sharedStringIndex Index of the shared string in the sharedStrings.xml file
  35       * @return void
  36       */
  37      public function addStringForIndex($sharedString, $sharedStringIndex)
  38      {
  39          if (!$this->isCacheClosed) {
  40              $this->inMemoryCache->offsetSet($sharedStringIndex, $sharedString);
  41          }
  42      }
  43  
  44      /**
  45       * Closes the cache after the last shared string was added.
  46       * This prevents any additional string from being added to the cache.
  47       *
  48       * @return void
  49       */
  50      public function closeCache()
  51      {
  52          $this->isCacheClosed = true;
  53      }
  54  
  55      /**
  56       * Returns the string located at the given index from the cache.
  57       *
  58       * @param int $sharedStringIndex Index of the shared string in the sharedStrings.xml file
  59       * @throws \Box\Spout\Reader\Exception\SharedStringNotFoundException If no shared string found for the given index
  60       * @return string The shared string at the given index
  61       */
  62      public function getStringAtIndex($sharedStringIndex)
  63      {
  64          try {
  65              return $this->inMemoryCache->offsetGet($sharedStringIndex);
  66          } catch (\RuntimeException $e) {
  67              throw new SharedStringNotFoundException("Shared string not found for index: $sharedStringIndex");
  68          }
  69      }
  70  
  71      /**
  72       * Destroys the cache, freeing memory and removing any created artifacts
  73       *
  74       * @return void
  75       */
  76      public function clearCache()
  77      {
  78          unset($this->inMemoryCache);
  79          $this->isCacheClosed = false;
  80      }
  81  }