You are here

class Cache in Render cache 7.2

Same name in this branch
  1. 7.2 src/Cache/Cache.php \Drupal\render_cache\Cache\Cache
  2. 7.2 lib/Drupal/Core/Cache/Cache.php \Drupal\Core\Cache\Cache

Helper methods for cache.

Hierarchy

  • class \Drupal\Core\Cache\Cache

Expanded class hierarchy of Cache

1 file declares its use of Cache
Cache.php in src/Cache/Cache.php
Contains \Drupal\render_cache\Cache\Cache.

File

lib/Drupal/Core/Cache/Cache.php, line 17
Contains \Drupal\Core\Cache\Cache.

Namespace

Drupal\Core\Cache
View source
class Cache {

  /**
   * Indicates that the item should never be removed unless explicitly deleted.
   */
  const PERMANENT = CacheBackendInterface::CACHE_PERMANENT;

  /**
   * Merges arrays of cache tags and removes duplicates.
   *
   * The cache tags array is returned in a format that is valid for
   * \Drupal\Core\Cache\CacheBackendInterface::set().
   *
   * When caching elements, it is necessary to collect all cache tags into a
   * single array, from both the element itself and all child elements. This
   * allows items to be invalidated based on all tags attached to the content
   * they're constituted from.
   *
   * @param string[] …
   *   Arrays of cache tags to merge.
   *
   * @return string[]
   *   The merged array of cache tags.
   */
  public static function mergeTags() {
    $cache_tag_arrays = func_get_args();
    $cache_tags = array();
    foreach ($cache_tag_arrays as $tags) {
      static::validateTags($tags);
      $cache_tags = array_merge($cache_tags, $tags);
    }
    $cache_tags = array_unique($cache_tags);
    sort($cache_tags);
    return $cache_tags;
  }

  /**
   * Validates an array of cache tags.
   *
   * Can be called before using cache tags in operations, to ensure validity.
   *
   * @param string[] $tags
   *   An array of cache tags.
   *
   * @throws \LogicException
   */
  public static function validateTags(array $tags) {
    if (empty($tags)) {
      return;
    }
    foreach ($tags as $value) {
      if (!is_string($value)) {
        throw new \LogicException('Cache tags must be strings, ' . gettype($value) . ' given.');
      }
    }
  }

  /**
   * Build an array of cache tags from a given prefix and an array of suffixes.
   *
   * Each suffix will be converted to a cache tag by appending it to the prefix,
   * with a colon between them.
   *
   * @param string $prefix
   *   A prefix string.
   * @param array $suffixes
   *   An array of suffixes. Will be cast to strings.
   *
   * @return string[]
   *   An array of cache tags.
   */
  public static function buildTags($prefix, array $suffixes) {
    $tags = array();
    foreach ($suffixes as $suffix) {
      $tags[] = $prefix . ':' . $suffix;
    }
    return $tags;
  }

  /**
   * Deletes items from all bins with any of the specified tags.
   *
   * Many sites have more than one active cache backend, and each backend may
   * use a different strategy for storing tags against cache items, and
   * deleting cache items associated with a given tag.
   *
   * When deleting a given list of tags, we iterate over each cache backend, and
   * and call deleteTags() on each.
   *
   * @param string[] $tags
   *   The list of tags to delete cache items for.
   */
  public static function deleteTags(array $tags) {
    static::validateTags($tags);
    foreach (static::getBins() as $cache_backend) {
      $cache_backend
        ->deleteTags($tags);
    }
  }

  /**
   * Marks cache items from all bins with any of the specified tags as invalid.
   *
   * Many sites have more than one active cache backend, and each backend my use
   * a different strategy for storing tags against cache items, and invalidating
   * cache items associated with a given tag.
   *
   * When invalidating a given list of tags, we iterate over each cache backend,
   * and call invalidateTags() on each.
   *
   * @param string[] $tags
   *   The list of tags to invalidate cache items for.
   */
  public static function invalidateTags(array $tags) {
    static::validateTags($tags);
    foreach (static::getBins() as $cache_backend) {
      $cache_backend
        ->invalidateTags($tags);
    }
  }

  /**
   * Gets all cache bin services.
   *
   * @return array
   *  An array of cache backend objects keyed by cache bins.
   */
  public static function getBins() {
    $bins = array();
    $container = \Drupal::getContainer();
    foreach ($container
      ->getParameter('cache_bins') as $service_id => $bin) {
      $bins[$bin] = $container
        ->get($service_id);
    }
    return $bins;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Cache::buildTags public static function Build an array of cache tags from a given prefix and an array of suffixes.
Cache::deleteTags public static function Deletes items from all bins with any of the specified tags.
Cache::getBins public static function Gets all cache bin services.
Cache::invalidateTags public static function Marks cache items from all bins with any of the specified tags as invalid.
Cache::mergeTags public static function Merges arrays of cache tags and removes duplicates.
Cache::PERMANENT constant Indicates that the item should never be removed unless explicitly deleted.
Cache::validateTags public static function Validates an array of cache tags.