"Fossies" - the Fresh Open Source Software Archive

Member "drupal-8.9.10/core/lib/Drupal/Core/Cache/BackendChain.php" (26 Nov 2020, 5580 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 "BackendChain.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 
    3 namespace Drupal\Core\Cache;
    4 
    5 /**
    6  * Defines a chained cache implementation for combining multiple cache backends.
    7  *
    8  * Can be used to combine two or more backends together to behave as if they
    9  * were a single backend.
   10  *
   11  * For example a slower, persistent storage engine could be combined with a
   12  * faster, volatile storage engine. When retrieving items from cache, they will
   13  * be fetched from the volatile backend first, only falling back to the
   14  * persistent backend if an item is not available. An item not present in the
   15  * volatile backend but found in the persistent one will be propagated back up
   16  * to ensure fast retrieval on the next request. On cache sets and deletes, both
   17  * backends will be invoked to ensure consistency.
   18  *
   19  * @see \Drupal\Core\Cache\ChainedFastBackend
   20  *
   21  * @ingroup cache
   22  */
   23 class BackendChain implements CacheBackendInterface, CacheTagsInvalidatorInterface {
   24 
   25   /**
   26    * Ordered list of CacheBackendInterface instances.
   27    *
   28    * @var array
   29    */
   30   protected $backends = [];
   31 
   32   /**
   33    * Constructs a new backend chain service instance.
   34    *
   35    * @param string|null $bin
   36    *   (deprecated) The cache bin for which the object is created. The $bin
   37    *   parameter is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0.
   38    *
   39    * @see https://www.drupal.org/node/3061125
   40    */
   41   public function __construct($bin = NULL) {
   42     if ($bin) {
   43       @trigger_error('The $bin parameter is deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Omit the first parameter. See https://www.drupal.org/node/3061125', E_USER_DEPRECATED);
   44     }
   45   }
   46 
   47   /**
   48    * Appends a cache backend to the cache chain.
   49    *
   50    * @param CacheBackendInterface $backend
   51    *   The cache backend to be appended to the cache chain.
   52    *
   53    * @return $this
   54    *   The called object.
   55    */
   56   public function appendBackend(CacheBackendInterface $backend) {
   57     $this->backends[] = $backend;
   58 
   59     return $this;
   60   }
   61 
   62   /**
   63    * Prepends a cache backend to the cache chain.
   64    *
   65    * @param CacheBackendInterface $backend
   66    *   The backend to be prepended to the cache chain.
   67    *
   68    * @return $this
   69    *   The called object.
   70    */
   71   public function prependBackend(CacheBackendInterface $backend) {
   72     array_unshift($this->backends, $backend);
   73 
   74     return $this;
   75   }
   76 
   77   /**
   78    * {@inheritdoc}
   79    */
   80   public function get($cid, $allow_invalid = FALSE) {
   81     foreach ($this->backends as $index => $backend) {
   82       if (($return = $backend->get($cid, $allow_invalid)) !== FALSE) {
   83         // We found a result, propagate it to all missed backends.
   84         if ($index > 0) {
   85           for ($i = ($index - 1); 0 <= $i; --$i) {
   86             $this->backends[$i]->set($cid, $return->data, $return->expire, $return->tags);
   87           }
   88         }
   89 
   90         return $return;
   91       }
   92     }
   93 
   94     return FALSE;
   95   }
   96 
   97   /**
   98    * {@inheritdoc}
   99    */
  100   public function getMultiple(&$cids, $allow_invalid = FALSE) {
  101     $return = [];
  102 
  103     foreach ($this->backends as $index => $backend) {
  104       $items = $backend->getMultiple($cids, $allow_invalid);
  105 
  106       // Propagate the values that could be retrieved from the current cache
  107       // backend to all missed backends.
  108       if ($index > 0 && !empty($items)) {
  109         for ($i = ($index - 1); 0 <= $i; --$i) {
  110           foreach ($items as $cached) {
  111             $this->backends[$i]->set($cached->cid, $cached->data, $cached->expire, $cached->tags);
  112           }
  113         }
  114       }
  115 
  116       // Append the values to the previously retrieved ones.
  117       $return += $items;
  118 
  119       if (empty($cids)) {
  120         // No need to go further if we don't have any cid to fetch left.
  121         break;
  122       }
  123     }
  124 
  125     return $return;
  126   }
  127 
  128   /**
  129    * {@inheritdoc}
  130    */
  131   public function set($cid, $data, $expire = Cache::PERMANENT, array $tags = []) {
  132     foreach ($this->backends as $backend) {
  133       $backend->set($cid, $data, $expire, $tags);
  134     }
  135   }
  136 
  137   /**
  138    * {@inheritdoc}
  139    */
  140   public function setMultiple(array $items) {
  141     foreach ($this->backends as $backend) {
  142       $backend->setMultiple($items);
  143     }
  144   }
  145 
  146   /**
  147    * {@inheritdoc}
  148    */
  149   public function delete($cid) {
  150     foreach ($this->backends as $backend) {
  151       $backend->delete($cid);
  152     }
  153   }
  154 
  155   /**
  156    * {@inheritdoc}
  157    */
  158   public function deleteMultiple(array $cids) {
  159     foreach ($this->backends as $backend) {
  160       $backend->deleteMultiple($cids);
  161     }
  162   }
  163 
  164   /**
  165    * {@inheritdoc}
  166    */
  167   public function deleteAll() {
  168     foreach ($this->backends as $backend) {
  169       $backend->deleteAll();
  170     }
  171   }
  172 
  173   /**
  174    * {@inheritdoc}
  175    */
  176   public function invalidate($cid) {
  177     foreach ($this->backends as $backend) {
  178       $backend->invalidate($cid);
  179     }
  180   }
  181 
  182   /**
  183    * {@inheritdoc}
  184    */
  185   public function invalidateMultiple(array $cids) {
  186     foreach ($this->backends as $backend) {
  187       $backend->invalidateMultiple($cids);
  188     }
  189   }
  190 
  191   /**
  192    * {@inheritdoc}
  193    */
  194   public function invalidateTags(array $tags) {
  195     foreach ($this->backends as $backend) {
  196       if ($backend instanceof CacheTagsInvalidatorInterface) {
  197         $backend->invalidateTags($tags);
  198       }
  199     }
  200   }
  201 
  202   /**
  203    * {@inheritdoc}
  204    */
  205   public function invalidateAll() {
  206     foreach ($this->backends as $backend) {
  207       $backend->invalidateAll();
  208     }
  209   }
  210 
  211   /**
  212    * {@inheritdoc}
  213    */
  214   public function garbageCollection() {
  215     foreach ($this->backends as $backend) {
  216       $backend->garbageCollection();
  217     }
  218   }
  219 
  220   /**
  221    * {@inheritdoc}
  222    */
  223   public function removeBin() {
  224     foreach ($this->backends as $backend) {
  225       $backend->removeBin();
  226     }
  227   }
  228 
  229 }