vendor/pimcore/pimcore/lib/Cache.php line 59

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Commercial License (PCL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  13.  */
  14. namespace Pimcore;
  15. use Pimcore\Cache\Core\CoreHandlerInterface;
  16. use Pimcore\Event\CoreCacheEvents;
  17. use Symfony\Component\EventDispatcher\Event;
  18. /**
  19.  * This acts as facade for the actual cache implementation and exists primarily for BC reasons.
  20.  */
  21. class Cache
  22. {
  23.     /**
  24.      * @var CoreHandlerInterface
  25.      */
  26.     protected static $handler;
  27.     public static function getInstance()
  28.     {
  29.         throw new \RuntimeException('getInstance() is not supported anymore');
  30.     }
  31.     /**
  32.      * Get the cache handler implementation
  33.      *
  34.      * @return CoreHandlerInterface
  35.      */
  36.     public static function getHandler()
  37.     {
  38.         if (null === static::$handler) {
  39.             static::$handler = \Pimcore::getContainer()->get('pimcore.cache.core.handler');
  40.         }
  41.         return static::$handler;
  42.     }
  43.     /**
  44.      * Initialize the cache. This acts mainly as integration point with legacy caches.
  45.      */
  46.     public static function init()
  47.     {
  48.         if (\Pimcore::hasKernel()) {
  49.             \Pimcore::getContainer()
  50.                 ->get('event_dispatcher')
  51.                 ->dispatch(CoreCacheEvents::INIT, new Event());
  52.             if (isset($_REQUEST['pimcore_nocache']) && \Pimcore::inDebugMode()) {
  53.                 self::getHandler()->disable();
  54.             }
  55.         }
  56.     }
  57.     /**
  58.      * Returns the content of the requested cache entry
  59.      *
  60.      * @param string $key
  61.      *
  62.      * @return mixed
  63.      */
  64.     public static function load($key)
  65.     {
  66.         return static::getHandler()->load($key);
  67.     }
  68.     /**
  69.      * Save an item to the cache (deferred to shutdown if force is false and forceImmediateWrite is not set)
  70.      *
  71.      * @param mixed $data
  72.      * @param string $key
  73.      * @param array $tags
  74.      * @param int|\DateInterval|null $lifetime
  75.      * @param int $priority
  76.      * @param bool $force
  77.      *
  78.      * @return bool
  79.      */
  80.     public static function save($data$key$tags = [], $lifetime null$priority 0$force false)
  81.     {
  82.         return static::getHandler()->save($key$data$tags$lifetime$priority$force);
  83.     }
  84.     /**
  85.      * Remove an item from the cache
  86.      *
  87.      * @param string $key
  88.      *
  89.      * @return bool
  90.      */
  91.     public static function remove($key)
  92.     {
  93.         return static::getHandler()->remove($key);
  94.     }
  95.     /**
  96.      * Empty the cache
  97.      *
  98.      * @return bool
  99.      */
  100.     public static function clearAll()
  101.     {
  102.         return static::getHandler()->clearAll();
  103.     }
  104.     /**
  105.      * Removes entries from the cache matching the given tag
  106.      *
  107.      * @param string $tag
  108.      *
  109.      * @return bool
  110.      */
  111.     public static function clearTag($tag)
  112.     {
  113.         return static::getHandler()->clearTag($tag);
  114.     }
  115.     /**
  116.      * Removes entries from the cache matching the given tags
  117.      *
  118.      * @param array $tags
  119.      *
  120.      * @return bool
  121.      */
  122.     public static function clearTags($tags = [])
  123.     {
  124.         if (!empty($tags) && !is_array($tags)) {
  125.             $tags = [$tags];
  126.         }
  127.         return static::getHandler()->clearTags($tags);
  128.     }
  129.     /**
  130.      * Adds a tag to the shutdown queue
  131.      *
  132.      * @param string $tag
  133.      */
  134.     public static function addClearTagOnShutdown($tag)
  135.     {
  136.         static::getHandler()->addTagClearedOnShutdown($tag);
  137.     }
  138.     /**
  139.      * Add tag to the list ignored on save. Items with this tag won't be saved to cache.
  140.      *
  141.      * @param string $tag
  142.      */
  143.     public static function addIgnoredTagOnSave($tag)
  144.     {
  145.         static::getHandler()->addTagIgnoredOnSave($tag);
  146.     }
  147.     /**
  148.      * Remove tag from the list ignored on save
  149.      *
  150.      * @param string $tag
  151.      */
  152.     public static function removeIgnoredTagOnSave($tag)
  153.     {
  154.         static::getHandler()->removeTagIgnoredOnSave($tag);
  155.     }
  156.     /**
  157.      * Add tag to the list ignored on clear. Tags in this list won't be cleared via clearTags()
  158.      *
  159.      * @param string $tag
  160.      */
  161.     public static function addIgnoredTagOnClear($tag)
  162.     {
  163.         static::getHandler()->addTagIgnoredOnClear($tag);
  164.     }
  165.     /**
  166.      * Remove tag from the list ignored on clear
  167.      *
  168.      * @param string $tag
  169.      */
  170.     public static function removeIgnoredTagOnClear($tag)
  171.     {
  172.         static::getHandler()->removeTagIgnoredOnClear($tag);
  173.     }
  174.     /**
  175.      * Write and clean up cache
  176.      *
  177.      * @param bool $forceWrite
  178.      */
  179.     public static function shutdown($forceWrite false)
  180.     {
  181.         static::getHandler()->shutdown($forceWrite);
  182.     }
  183.     /**
  184.      * Disables the complete pimcore cache
  185.      */
  186.     public static function disable()
  187.     {
  188.         static::getHandler()->disable();
  189.     }
  190.     /**
  191.      * Enables the pimcore cache
  192.      */
  193.     public static function enable()
  194.     {
  195.         static::getHandler()->enable();
  196.     }
  197.     /**
  198.      * @return bool
  199.      */
  200.     public static function isEnabled()
  201.     {
  202.         return static::getHandler()->isEnabled();
  203.     }
  204.     /**
  205.      * @param bool $forceImmediateWrite
  206.      */
  207.     public static function setForceImmediateWrite($forceImmediateWrite)
  208.     {
  209.         static::getHandler()->setForceImmediateWrite($forceImmediateWrite);
  210.     }
  211.     /**
  212.      * @return bool
  213.      */
  214.     public static function getForceImmediateWrite()
  215.     {
  216.         return static::getHandler()->getForceImmediateWrite();
  217.     }
  218. }