cache.inc

  1. Views
    1. 6 includes/cache.inc
    2. 7 includes/cache.inc
  2. drupal
    1. 5 includes/cache.inc
    2. 6 includes/cache.inc
    3. 7 includes/cache.inc
    4. 8 core/includes/cache.inc

Functions and interfaces for cache handling.

Functions & methods

NameDescription
cacheInstantiates and statically caches the correct class for a cache bin.
cache_clear_allExpires data from the block and page caches.
cache_get_backendsReturns a list of cache backends for this site.
cache_invalidateInvalidates the items associated with given list of tags.

File

core/includes/cache.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Functions and interfaces for cache handling.
  5. */
  6. /**
  7. * Instantiates and statically caches the correct class for a cache bin.
  8. *
  9. * By default, this returns an instance of the Drupal\Core\Cache\DatabaseBackend
  10. * class.
  11. *
  12. * Classes implementing Drupal\Core\Cache\CacheBackendInterface can register
  13. * themselves both as a default implementation and for specific bins.
  14. *
  15. * @param $bin
  16. * The cache bin for which the cache object should be returned, defaults to
  17. * 'cache'.
  18. *
  19. * @return Drupal\Core\Cache\CacheBackendInterface
  20. * The cache object associated with the specified bin.
  21. *
  22. * @see Drupal\Core\Cache\CacheBackendInterface
  23. */
  24. function cache($bin = 'cache') {
  25. // Temporary backwards compatibiltiy layer, allow old style prefixed cache
  26. // bin names to be passed as arguments.
  27. $bin = str_replace('cache_', '', $bin);
  28. // We do not use drupal_static() here because we do not want to change the
  29. // storage of a cache bin mid-request.
  30. static $cache_objects;
  31. if (!isset($cache_objects[$bin])) {
  32. $cache_backends = cache_get_backends();
  33. $class = isset($cache_backends[$bin]) ? $cache_backends[$bin] : $cache_backends['cache'];
  34. $cache_objects[$bin] = new $class($bin);
  35. }
  36. return $cache_objects[$bin];
  37. }
  38. /**
  39. * Invalidates the items associated with given list of tags.
  40. *
  41. * Many sites have more than one active cache backend, and each backend my use
  42. * a different strategy for storing tags against cache items, and invalidating
  43. * cache items associated with a given tag.
  44. *
  45. * When invalidating a given list of tags, we iterate over each cache backend,
  46. * and call invalidate on each.
  47. *
  48. * @param array $tags
  49. * The list of tags to invalidate cache items for.
  50. */
  51. function cache_invalidate(array $tags) {
  52. foreach (cache_get_backends() as $bin => $class) {
  53. cache($bin)->invalidateTags($tags);
  54. }
  55. }
  56. /**
  57. * Returns a list of cache backends for this site.
  58. *
  59. * @return
  60. * An associative array with cache bins as keys, and backend classes as value.
  61. */
  62. function cache_get_backends() {
  63. return variable_get('cache_classes', array('cache' => 'Drupal\Core\Cache\DatabaseBackend'));
  64. }
  65. /**
  66. * Expires data from the block and page caches.
  67. */
  68. function cache_clear_all() {
  69. // @todo: remove before release.
  70. if (func_get_args()) {
  71. throw new Exception(t('cache_clear_all() no longer takes arguments, use cache() instead.'));
  72. }
  73. // Clear the block cache first, so stale data will
  74. // not end up in the page cache.
  75. if (module_exists('block')) {
  76. cache('block')->expire();
  77. }
  78. cache('page')->expire();
  79. }
Login or register to post comments