2006-06-14 14:01:12 +00:00
|
|
|
<?php
|
|
|
|
|
2009-06-16 23:48:09 +00:00
|
|
|
/**
|
2011-12-05 12:52:27 +00:00
|
|
|
* @file
|
|
|
|
* Functions and interfaces for cache handling.
|
|
|
|
*/
|
|
|
|
|
2012-09-04 13:51:51 +00:00
|
|
|
use Drupal\Core\Cache\CacheFactory;
|
|
|
|
|
2011-12-05 12:52:27 +00:00
|
|
|
/**
|
|
|
|
* Instantiates and statically caches the correct class for a cache bin.
|
2009-06-16 23:48:09 +00:00
|
|
|
*
|
2012-02-01 03:22:12 +00:00
|
|
|
* By default, this returns an instance of the Drupal\Core\Cache\DatabaseBackend
|
2012-01-13 03:19:33 +00:00
|
|
|
* class.
|
|
|
|
*
|
2012-02-01 03:22:12 +00:00
|
|
|
* Classes implementing Drupal\Core\Cache\CacheBackendInterface can register
|
|
|
|
* themselves both as a default implementation and for specific bins.
|
2009-06-16 23:48:09 +00:00
|
|
|
*
|
|
|
|
* @param $bin
|
2011-09-07 18:38:31 +00:00
|
|
|
* The cache bin for which the cache object should be returned, defaults to
|
|
|
|
* 'cache'.
|
2011-12-05 12:52:27 +00:00
|
|
|
*
|
2012-02-01 03:22:12 +00:00
|
|
|
* @return Drupal\Core\Cache\CacheBackendInterface
|
2009-12-04 16:31:04 +00:00
|
|
|
* The cache object associated with the specified bin.
|
2011-12-05 12:52:27 +00:00
|
|
|
*
|
2012-02-01 03:22:12 +00:00
|
|
|
* @see Drupal\Core\Cache\CacheBackendInterface
|
2009-06-16 23:48:09 +00:00
|
|
|
*/
|
2011-09-07 18:38:31 +00:00
|
|
|
function cache($bin = 'cache') {
|
2012-09-24 21:08:10 +00:00
|
|
|
// Use the advanced drupal_static() pattern, since this is called very often.
|
|
|
|
static $drupal_static_fast;
|
|
|
|
if (!isset($drupal_static_fast)) {
|
|
|
|
$drupal_static_fast['cache'] = &drupal_static(__FUNCTION__, array());
|
|
|
|
}
|
|
|
|
$cache_objects = &$drupal_static_fast['cache'];
|
|
|
|
|
2011-09-07 18:38:31 +00:00
|
|
|
// Temporary backwards compatibiltiy layer, allow old style prefixed cache
|
|
|
|
// bin names to be passed as arguments.
|
|
|
|
$bin = str_replace('cache_', '', $bin);
|
|
|
|
|
2009-06-16 23:48:09 +00:00
|
|
|
if (!isset($cache_objects[$bin])) {
|
2012-09-04 13:51:51 +00:00
|
|
|
$cache_objects[$bin] = CacheFactory::get($bin);
|
2009-06-16 23:48:09 +00:00
|
|
|
}
|
|
|
|
return $cache_objects[$bin];
|
|
|
|
}
|
|
|
|
|
2012-04-13 01:49:34 +00:00
|
|
|
/**
|
|
|
|
* Invalidates the items associated with given list of tags.
|
|
|
|
*
|
|
|
|
* 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 invalidate on each.
|
|
|
|
*
|
|
|
|
* @param array $tags
|
|
|
|
* The list of tags to invalidate cache items for.
|
|
|
|
*/
|
|
|
|
function cache_invalidate(array $tags) {
|
2012-09-04 13:51:51 +00:00
|
|
|
foreach (CacheFactory::getBackends() as $bin => $class) {
|
2012-04-13 01:49:34 +00:00
|
|
|
cache($bin)->invalidateTags($tags);
|
|
|
|
}
|
|
|
|
}
|