Cache.php 3.91 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Contains \Drupal\Core\Cache\Cache.
 */

namespace Drupal\Core\Cache;

10 11
use Drupal\Core\Database\Query\SelectInterface;

12 13
/**
 * Helper methods for cache.
14 15
 *
 * @ingroup cache
16 17 18
 */
class Cache {

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

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
  /**
   * 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 = [];
    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.
84 85
   * @param string $glue
   *   A string to be used as glue for concatenation. Defaults to a colon.
86 87 88 89
   *
   * @return string[]
   *   An array of cache tags.
   */
90
  public static function buildTags($prefix, array $suffixes, $glue = ':') {
91 92
    $tags = [];
    foreach ($suffixes as $suffix) {
93
      $tags[] = $prefix . $glue . $suffix;
94 95 96 97
    }
    return $tags;
  }

98 99 100
  /**
   * Marks cache items from all bins with any of the specified tags as invalid.
   *
101
   * @param string[] $tags
102 103 104
   *   The list of tags to invalidate cache items for.
   */
  public static function invalidateTags(array $tags) {
105
    \Drupal::service('cache_tags.invalidator')->invalidateTags($tags);
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
  }

  /**
   * 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;
  }

123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
  /**
   * Generates a hash from a query object, to be used as part of the cache key.
   *
   * This smart caching strategy saves Drupal from querying and rendering to
   * HTML when the underlying query is unchanged.
   *
   * Expensive queries should use the query builder to create the query and then
   * call this function. Executing the query and formatting results should
   * happen in a #pre_render callback.
   *
   * @param \Drupal\Core\Database\Query\SelectInterface $query
   *   A select query object.
   *
   * @return string
   *   A hash of the query arguments.
   */
  public static function keyFromQuery(SelectInterface $query) {
    $query->preExecute();
    $keys = array((string) $query, $query->getArguments());
    return hash('sha256', serialize($keys));
  }

145
}