"Fossies" - the Fresh Open Source Software Archive

Member "drupal-8.9.10/core/lib/Drupal/Core/Cache/MemoryBackend.php" (26 Nov 2020, 5324 Bytes) of package /linux/www/drupal-8.9.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "MemoryBackend.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 
    3 namespace Drupal\Core\Cache;
    4 
    5 use Drupal\Component\Assertion\Inspector;
    6 
    7 /**
    8  * Defines a memory cache implementation.
    9  *
   10  * Stores cache items in memory using a PHP array.
   11  *
   12  * Should be used for unit tests and specialist use-cases only, does not
   13  * store cached items between requests.
   14  *
   15  * The functions ::prepareItem()/::set() use unserialize()/serialize(). It
   16  * behaves as an external cache backend to avoid changing the cached data by
   17  * reference. In ::prepareItem(), the object is not modified by the call to
   18  * unserialize() because we make a clone of it.
   19  *
   20  * @ingroup cache
   21  */
   22 class MemoryBackend implements CacheBackendInterface, CacheTagsInvalidatorInterface {
   23 
   24   /**
   25    * Array to store cache objects.
   26    */
   27   protected $cache = [];
   28 
   29   /**
   30    * {@inheritdoc}
   31    */
   32   public function get($cid, $allow_invalid = FALSE) {
   33     if (isset($this->cache[$cid])) {
   34       return $this->prepareItem($this->cache[$cid], $allow_invalid);
   35     }
   36     else {
   37       return FALSE;
   38     }
   39   }
   40 
   41   /**
   42    * {@inheritdoc}
   43    */
   44   public function getMultiple(&$cids, $allow_invalid = FALSE) {
   45     $ret = [];
   46 
   47     $items = array_intersect_key($this->cache, array_flip($cids));
   48 
   49     foreach ($items as $item) {
   50       $item = $this->prepareItem($item, $allow_invalid);
   51       if ($item) {
   52         $ret[$item->cid] = $item;
   53       }
   54     }
   55 
   56     $cids = array_diff($cids, array_keys($ret));
   57 
   58     return $ret;
   59   }
   60 
   61   /**
   62    * Prepares a cached item.
   63    *
   64    * Checks that items are either permanent or did not expire, and returns data
   65    * as appropriate.
   66    *
   67    * @param object $cache
   68    *   An item loaded from self::get() or self::getMultiple().
   69    * @param bool $allow_invalid
   70    *   (optional) If TRUE, cache items may be returned even if they have expired
   71    *   or been invalidated.
   72    *
   73    * @return mixed
   74    *   The item with data as appropriate or FALSE if there is no
   75    *   valid item to load.
   76    */
   77   protected function prepareItem($cache, $allow_invalid) {
   78     if (!isset($cache->data)) {
   79       return FALSE;
   80     }
   81     // The object passed into this function is the one stored in $this->cache.
   82     // We must clone it as part of the preparation step so that the actual
   83     // cache object is not affected by the unserialize() call or other
   84     // manipulations of the returned object.
   85 
   86     $prepared = clone $cache;
   87     $prepared->data = unserialize($prepared->data);
   88 
   89     // Check expire time.
   90     $prepared->valid = $prepared->expire == Cache::PERMANENT || $prepared->expire >= $this->getRequestTime();
   91 
   92     if (!$allow_invalid && !$prepared->valid) {
   93       return FALSE;
   94     }
   95 
   96     return $prepared;
   97   }
   98 
   99   /**
  100    * {@inheritdoc}
  101    */
  102   public function set($cid, $data, $expire = Cache::PERMANENT, array $tags = []) {
  103     assert(Inspector::assertAllStrings($tags), 'Cache Tags must be strings.');
  104     $tags = array_unique($tags);
  105     // Sort the cache tags so that they are stored consistently in the database.
  106     sort($tags);
  107     $this->cache[$cid] = (object) [
  108       'cid' => $cid,
  109       'data' => serialize($data),
  110       'created' => $this->getRequestTime(),
  111       'expire' => $expire,
  112       'tags' => $tags,
  113     ];
  114   }
  115 
  116   /**
  117    * {@inheritdoc}
  118    */
  119   public function setMultiple(array $items = []) {
  120     foreach ($items as $cid => $item) {
  121       $this->set($cid, $item['data'], isset($item['expire']) ? $item['expire'] : CacheBackendInterface::CACHE_PERMANENT, isset($item['tags']) ? $item['tags'] : []);
  122     }
  123   }
  124 
  125   /**
  126    * {@inheritdoc}
  127    */
  128   public function delete($cid) {
  129     unset($this->cache[$cid]);
  130   }
  131 
  132   /**
  133    * {@inheritdoc}
  134    */
  135   public function deleteMultiple(array $cids) {
  136     $this->cache = array_diff_key($this->cache, array_flip($cids));
  137   }
  138 
  139   /**
  140    * {@inheritdoc}
  141    */
  142   public function deleteAll() {
  143     $this->cache = [];
  144   }
  145 
  146   /**
  147    * {@inheritdoc}
  148    */
  149   public function invalidate($cid) {
  150     if (isset($this->cache[$cid])) {
  151       $this->cache[$cid]->expire = $this->getRequestTime() - 1;
  152     }
  153   }
  154 
  155   /**
  156    * {@inheritdoc}
  157    */
  158   public function invalidateMultiple(array $cids) {
  159     $items = array_intersect_key($this->cache, array_flip($cids));
  160     foreach ($items as $cid => $item) {
  161       $this->cache[$cid]->expire = $this->getRequestTime() - 1;
  162     }
  163   }
  164 
  165   /**
  166    * {@inheritdoc}
  167    */
  168   public function invalidateTags(array $tags) {
  169     foreach ($this->cache as $cid => $item) {
  170       if (array_intersect($tags, $item->tags)) {
  171         $this->cache[$cid]->expire = $this->getRequestTime() - 1;
  172       }
  173     }
  174   }
  175 
  176   /**
  177    * {@inheritdoc}
  178    */
  179   public function invalidateAll() {
  180     foreach ($this->cache as $cid => $item) {
  181       $this->cache[$cid]->expire = $this->getRequestTime() - 1;
  182     }
  183   }
  184 
  185   /**
  186    * {@inheritdoc}
  187    */
  188   public function garbageCollection() {
  189   }
  190 
  191   /**
  192    * {@inheritdoc}
  193    */
  194   public function removeBin() {
  195     $this->cache = [];
  196   }
  197 
  198   /**
  199    * Wrapper method for REQUEST_TIME constant.
  200    *
  201    * @return int
  202    */
  203   protected function getRequestTime() {
  204     return defined('REQUEST_TIME') ? REQUEST_TIME : (int) $_SERVER['REQUEST_TIME'];
  205   }
  206 
  207   /**
  208    * Prevents data stored in memory backends from being serialized.
  209    */
  210   public function __sleep() {
  211     return [];
  212   }
  213 
  214   /**
  215    * Reset statically cached variables.
  216    *
  217    * This is only used by tests.
  218    */
  219   public function reset() {
  220     $this->cache = [];
  221   }
  222 
  223 }