vendor/doctrine/cache/lib/Doctrine/Common/Cache/CacheProvider.php line 133

Open in your IDE?
  1. <?php
  2. /*
  3.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6.  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7.  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13.  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14.  *
  15.  * This software consists of voluntary contributions made by many individuals
  16.  * and is licensed under the MIT license. For more information, see
  17.  * <http://www.doctrine-project.org>.
  18.  */
  19. namespace Doctrine\Common\Cache;
  20. /**
  21.  * Base class for cache provider implementations.
  22.  *
  23.  * @since  2.2
  24.  * @author Benjamin Eberlei <kontakt@beberlei.de>
  25.  * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  26.  * @author Jonathan Wage <jonwage@gmail.com>
  27.  * @author Roman Borschel <roman@code-factory.org>
  28.  * @author Fabio B. Silva <fabio.bat.silva@gmail.com>
  29.  * @author Benoit Burnichon <bburnichon@gmail.com>
  30.  */
  31. abstract class CacheProvider implements CacheFlushableCacheClearableCacheMultiOperationCache
  32. {
  33.     const DOCTRINE_NAMESPACE_CACHEKEY 'DoctrineNamespaceCacheKey[%s]';
  34.     /**
  35.      * The namespace to prefix all cache ids with.
  36.      *
  37.      * @var string
  38.      */
  39.     private $namespace '';
  40.     /**
  41.      * The namespace version.
  42.      *
  43.      * @var integer|null
  44.      */
  45.     private $namespaceVersion;
  46.     /**
  47.      * Sets the namespace to prefix all cache ids with.
  48.      *
  49.      * @param string $namespace
  50.      *
  51.      * @return void
  52.      */
  53.     public function setNamespace($namespace)
  54.     {
  55.         $this->namespace        = (string) $namespace;
  56.         $this->namespaceVersion null;
  57.     }
  58.     /**
  59.      * Retrieves the namespace that prefixes all cache ids.
  60.      *
  61.      * @return string
  62.      */
  63.     public function getNamespace()
  64.     {
  65.         return $this->namespace;
  66.     }
  67.     /**
  68.      * {@inheritdoc}
  69.      */
  70.     public function fetch($id)
  71.     {
  72.         return $this->doFetch($this->getNamespacedId($id));
  73.     }
  74.     /**
  75.      * {@inheritdoc}
  76.      */
  77.     public function fetchMultiple(array $keys)
  78.     {
  79.         if (empty($keys)) {
  80.             return [];
  81.         }
  82.         // note: the array_combine() is in place to keep an association between our $keys and the $namespacedKeys
  83.         $namespacedKeys array_combine($keysarray_map([$this'getNamespacedId'], $keys));
  84.         $items          $this->doFetchMultiple($namespacedKeys);
  85.         $foundItems     = [];
  86.         // no internal array function supports this sort of mapping: needs to be iterative
  87.         // this filters and combines keys in one pass
  88.         foreach ($namespacedKeys as $requestedKey => $namespacedKey) {
  89.             if (isset($items[$namespacedKey]) || array_key_exists($namespacedKey$items)) {
  90.                 $foundItems[$requestedKey] = $items[$namespacedKey];
  91.             }
  92.         }
  93.         return $foundItems;
  94.     }
  95.     /**
  96.      * {@inheritdoc}
  97.      */
  98.     public function saveMultiple(array $keysAndValues$lifetime 0)
  99.     {
  100.         $namespacedKeysAndValues = [];
  101.         foreach ($keysAndValues as $key => $value) {
  102.             $namespacedKeysAndValues[$this->getNamespacedId($key)] = $value;
  103.         }
  104.         return $this->doSaveMultiple($namespacedKeysAndValues$lifetime);
  105.     }
  106.     /**
  107.      * {@inheritdoc}
  108.      */
  109.     public function contains($id)
  110.     {
  111.         return $this->doContains($this->getNamespacedId($id));
  112.     }
  113.     /**
  114.      * {@inheritdoc}
  115.      */
  116.     public function save($id$data$lifeTime 0)
  117.     {
  118.         return $this->doSave($this->getNamespacedId($id), $data$lifeTime);
  119.     }
  120.     /**
  121.      * {@inheritdoc}
  122.      */
  123.     public function deleteMultiple(array $keys)
  124.     {
  125.         return $this->doDeleteMultiple(array_map(array($this'getNamespacedId'), $keys));
  126.     }
  127.     /**
  128.      * {@inheritdoc}
  129.      */
  130.     public function delete($id)
  131.     {
  132.         return $this->doDelete($this->getNamespacedId($id));
  133.     }
  134.     /**
  135.      * {@inheritdoc}
  136.      */
  137.     public function getStats()
  138.     {
  139.         return $this->doGetStats();
  140.     }
  141.     /**
  142.      * {@inheritDoc}
  143.      */
  144.     public function flushAll()
  145.     {
  146.         return $this->doFlush();
  147.     }
  148.     /**
  149.      * {@inheritDoc}
  150.      */
  151.     public function deleteAll()
  152.     {
  153.         $namespaceCacheKey $this->getNamespaceCacheKey();
  154.         $namespaceVersion  $this->getNamespaceVersion() + 1;
  155.         if ($this->doSave($namespaceCacheKey$namespaceVersion)) {
  156.             $this->namespaceVersion $namespaceVersion;
  157.             return true;
  158.         }
  159.         return false;
  160.     }
  161.     /**
  162.      * Prefixes the passed id with the configured namespace value.
  163.      *
  164.      * @param string $id The id to namespace.
  165.      *
  166.      * @return string The namespaced id.
  167.      */
  168.     private function getNamespacedId(string $id) : string
  169.     {
  170.         $namespaceVersion  $this->getNamespaceVersion();
  171.         return sprintf('%s[%s][%s]'$this->namespace$id$namespaceVersion);
  172.     }
  173.     /**
  174.      * Returns the namespace cache key.
  175.      *
  176.      * @return string
  177.      */
  178.     private function getNamespaceCacheKey() : string
  179.     {
  180.         return sprintf(self::DOCTRINE_NAMESPACE_CACHEKEY$this->namespace);
  181.     }
  182.     /**
  183.      * Returns the namespace version.
  184.      *
  185.      * @return integer
  186.      */
  187.     private function getNamespaceVersion() : int
  188.     {
  189.         if (null !== $this->namespaceVersion) {
  190.             return $this->namespaceVersion;
  191.         }
  192.         $namespaceCacheKey      $this->getNamespaceCacheKey();
  193.         $this->namespaceVersion = (int) $this->doFetch($namespaceCacheKey) ?: 1;
  194.         return $this->namespaceVersion;
  195.     }
  196.     /**
  197.      * Default implementation of doFetchMultiple. Each driver that supports multi-get should owerwrite it.
  198.      *
  199.      * @param array $keys Array of keys to retrieve from cache
  200.      * @return array Array of values retrieved for the given keys.
  201.      */
  202.     protected function doFetchMultiple(array $keys)
  203.     {
  204.         $returnValues = [];
  205.         foreach ($keys as $key) {
  206.             if (false !== ($item $this->doFetch($key)) || $this->doContains($key)) {
  207.                 $returnValues[$key] = $item;
  208.             }
  209.         }
  210.         return $returnValues;
  211.     }
  212.     /**
  213.      * Fetches an entry from the cache.
  214.      *
  215.      * @param string $id The id of the cache entry to fetch.
  216.      *
  217.      * @return mixed|false The cached data or FALSE, if no cache entry exists for the given id.
  218.      */
  219.     abstract protected function doFetch($id);
  220.     /**
  221.      * Tests if an entry exists in the cache.
  222.      *
  223.      * @param string $id The cache id of the entry to check for.
  224.      *
  225.      * @return bool TRUE if a cache entry exists for the given cache id, FALSE otherwise.
  226.      */
  227.     abstract protected function doContains($id);
  228.     /**
  229.      * Default implementation of doSaveMultiple. Each driver that supports multi-put should override it.
  230.      *
  231.      * @param array $keysAndValues  Array of keys and values to save in cache
  232.      * @param int   $lifetime       The lifetime. If != 0, sets a specific lifetime for these
  233.      *                              cache entries (0 => infinite lifeTime).
  234.      *
  235.      * @return bool TRUE if the operation was successful, FALSE if it wasn't.
  236.      */
  237.     protected function doSaveMultiple(array $keysAndValues$lifetime 0)
  238.     {
  239.         $success true;
  240.         foreach ($keysAndValues as $key => $value) {
  241.             if (!$this->doSave($key$value$lifetime)) {
  242.                 $success false;
  243.             }
  244.         }
  245.         return $success;
  246.     }
  247.     /**
  248.      * Puts data into the cache.
  249.      *
  250.      * @param string $id       The cache id.
  251.      * @param string $data     The cache entry/data.
  252.      * @param int    $lifeTime The lifetime. If != 0, sets a specific lifetime for this
  253.      *                           cache entry (0 => infinite lifeTime).
  254.      *
  255.      * @return bool TRUE if the entry was successfully stored in the cache, FALSE otherwise.
  256.      */
  257.     abstract protected function doSave($id$data$lifeTime 0);
  258.     /**
  259.      * Default implementation of doDeleteMultiple. Each driver that supports multi-delete should override it.
  260.      *
  261.      * @param array $keys Array of keys to delete from cache
  262.      *
  263.      * @return bool TRUE if the operation was successful, FALSE if it wasn't
  264.      */
  265.     protected function doDeleteMultiple(array $keys)
  266.     {
  267.         $success true;
  268.         foreach ($keys as $key) {
  269.             if (! $this->doDelete($key)) {
  270.                 $success false;
  271.             }
  272.         }
  273.         return $success;
  274.     }
  275.     /**
  276.      * Deletes a cache entry.
  277.      *
  278.      * @param string $id The cache id.
  279.      *
  280.      * @return bool TRUE if the cache entry was successfully deleted, FALSE otherwise.
  281.      */
  282.     abstract protected function doDelete($id);
  283.     /**
  284.      * Flushes all cache entries.
  285.      *
  286.      * @return bool TRUE if the cache entries were successfully flushed, FALSE otherwise.
  287.      */
  288.     abstract protected function doFlush();
  289.     /**
  290.      * Retrieves cached information from the data store.
  291.      *
  292.      * @since 2.2
  293.      *
  294.      * @return array|null An associative array with server's statistics if available, NULL otherwise.
  295.      */
  296.     abstract protected function doGetStats();
  297. }