function cache

Instantiates and statically caches the correct class for a cache bin.

By default, this returns an instance of the Drupal\Core\Cache\DatabaseBackend class.

Classes implementing Drupal\Core\Cache\CacheBackendInterface can register themselves both as a default implementation and for specific bins.

Parameters

$bin: The cache bin for which the cache object should be returned, defaults to 'cache'.

Return value

Drupal\Core\Cache\CacheBackendInterface The cache object associated with the specified bin.

See also

Drupal\Core\Cache\CacheBackendInterface

3 calls to cache()
CacheDecoratorTest::testCachedDefinitions in drupal/core/modules/system/lib/Drupal/system/Tests/Plugin/CacheDecoratorTest.php
Tests that discovered definitions are properly cached.
CacheDecoratorTest::testClearCachedDefinitions in drupal/core/modules/system/lib/Drupal/system/Tests/Plugin/CacheDecoratorTest.php
Tests CacheDecorator::clearCachedDefinitions().
TwigEnvironment::__construct in drupal/core/lib/Drupal/Core/Template/TwigEnvironment.php
Constructs a TwigEnvironment object and stores cache and storage internally.
37 string references to 'cache'
block_add_block_form_submit in drupal/core/modules/block/block.admin.inc
Form submission handler for block_add_block_form().
block_schema in drupal/core/modules/block/block.install
Implements hook_schema().
CacheTest::testHeaderStorage in drupal/core/modules/views/lib/Drupal/views/Tests/Plugin/CacheTest.php
Tests css/js storage and restoring mechanism.
CacheTest::testNoneCaching in drupal/core/modules/views/lib/Drupal/views/Tests/Plugin/CacheTest.php
Tests no caching.
CacheTest::testTimeCaching in drupal/core/modules/views/lib/Drupal/views/Tests/Plugin/CacheTest.php
Tests time based caching.

... See full list

File

drupal/core/includes/cache.inc, line 28
Functions and interfaces for cache handling.

Code

function cache($bin = 'cache') {

  // 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'];

  // Temporary backwards compatibiltiy layer, allow old style prefixed cache
  // bin names to be passed as arguments.
  $bin = str_replace('cache_', '', $bin);
  if (!isset($cache_objects[$bin])) {
    $cache_objects[$bin] = CacheFactory::get($bin);
  }
  return $cache_objects[$bin];
}