diff --git a/core/includes/cache.inc b/core/includes/cache.inc index 5d82fa0d91b63254df0c47e26da366acf66ed21c..e8c74772d7f59316ad2e917fbe44538ab86f5bd2 100644 --- a/core/includes/cache.inc +++ b/core/includes/cache.inc @@ -40,158 +40,19 @@ function cache($bin = 'cache') { } /** - * Returns data from the persistent cache. - * - * Data may be stored as either plain text or as serialized data. cache_get - * will automatically return unserialized objects and arrays. - * - * @param $cid - * The cache ID of the data to retrieve. - * @param $bin - * The cache bin to store the data in. Valid core values are 'cache_block', - * 'cache_bootstrap', 'cache_field', 'cache_filter', 'cache_form', - * 'cache_menu', 'cache_page', 'cache_path', 'cache_update' or 'cache' for - * the default cache. - * - * @return - * The cache or FALSE on failure. - */ -function cache_get($cid, $bin = 'cache') { - return cache($bin)->get($cid); -} - -/** - * Returns data from the persistent cache when given an array of cache IDs. - * - * @param $cids - * An array of cache IDs for the data to retrieve. This is passed by - * reference, and will have the IDs successfully returned from cache removed. - * @param $bin - * The cache bin where the data is stored. - * - * @return - * An array of the items successfully returned from cache indexed by cid. - */ -function cache_get_multiple(array &$cids, $bin = 'cache') { - return cache($bin)->getMultiple($cids); -} - -/** - * Stores data in the persistent cache. - * - * The persistent cache is split up into several cache bins. In the default - * cache implementation, each cache bin corresponds to a database table by the - * same name. Other implementations might want to store several bins in data - * structures that get flushed together. While it is not a problem for most - * cache bins if the entries in them are flushed before their expire time, some - * might break functionality or are extremely expensive to recalculate. These - * will be marked with a (*). The other bins expired automatically by core. - * Contributed modules can add additional bins and get them expired - * automatically by implementing hook_flush_caches(). - * - * - cache: Generic cache storage bin (used for variables, theme registry, - * locale date, list of simpletest tests etc). - * - * - cache_block: Stores the content of various blocks. - * - * - cache field: Stores the field data belonging to a given object. - * - * - cache_filter: Stores filtered pieces of content. - * - * - cache_form(*): Stores multistep forms. Flushing this bin means that some - * forms displayed to users lose their state and the data already submitted - * to them. - * - * - cache_menu: Stores the structure of visible navigation menus per page. - * - * - cache_page: Stores generated pages for anonymous users. It is flushed - * very often, whenever a page changes, at least for every ode and comment - * submission. This is the only bin affected by the page cache setting on - * the administrator panel. - * - * - cache path: Stores the system paths that have an alias. - * - * - cache update(*): Stores available releases. The update server (for - * example, drupal.org) needs to produce the relevant XML for every project - * installed on the current site. As this is different for (almost) every - * site, it's very expensive to recalculate for the update server. - * - * The reasons for having several bins are as follows: - * - * - smaller bins mean smaller database tables and allow for faster selects and - * inserts - * - we try to put fast changing cache items and rather static ones into - * different bins. The effect is that only the fast changing bins will need a - * lot of writes to disk. The more static bins will also be better cacheable - * with MySQL's query cache. - * - * @param $cid - * The cache ID of the data to store. - * @param $data - * The data to store in the cache. Complex data types will be automatically - * serialized before insertion. - * Strings will be stored as plain text and not serialized. - * @param $bin - * The cache bin to store the data in. Valid core values are 'cache_block', - * 'cache_bootstrap', 'cache_field', 'cache_filter', 'cache_form', - * 'cache_menu', 'cache_page', 'cache_update' or 'cache' for the default - * cache. - * @param $expire - * One of the following values: - * - CACHE_PERMANENT: Indicates that the item should never be removed unless - * explicitly told to using cache_clear_all() with a cache ID. - * - CACHE_TEMPORARY: Indicates that the item should be removed at the next - * general cache wipe. - * - A Unix timestamp: Indicates that the item should be kept at least until - * the given time, after which it behaves like CACHE_TEMPORARY. - */ -function cache_set($cid, $data, $bin = 'cache', $expire = CACHE_PERMANENT) { - return cache($bin)->set($cid, $data, $expire); -} - -/** - * Expires data from the cache. - * - * If called without arguments, expirable entries will be cleared from the - * cache_page and cache_block bins. - * - * @param $cid - * If set, the cache ID to delete. Otherwise, all cache entries that can - * expire are deleted. - * @param $bin - * If set, the cache bin to delete from. Mandatory argument if $cid is set. - * @param $wildcard - * If TRUE, cache IDs starting with $cid are deleted in addition to the - * exact cache ID specified by $cid. If $wildcard is TRUE and $cid is '*', - * the entire cache bin is emptied. + * Expires data from the block and page caches. */ -function cache_clear_all($cid = NULL, $bin = NULL, $wildcard = FALSE) { - if (!isset($cid) && !isset($bin)) { - // Clear the block cache first, so stale data will - // not end up in the page cache. - if (module_exists('block')) { - cache('block')->expire(); - } - cache('page')->expire(); - return; +function cache_clear_all() { + // @todo: remove before release. + if (func_get_args()) { + throw new Exception(t('cache_clear_all() no longer takes arguments, use cache() instead.')); } - return cache($bin)->clear($cid, $wildcard); -} - -/** - * Checks if a cache bin is empty. - * - * A cache bin is considered empty if it does not contain any valid data for any - * cache ID. - * - * @param $bin - * The cache bin to check. - * - * @return - * TRUE if the cache bin specified is empty. - */ -function cache_is_empty($bin) { - return cache($bin)->isEmpty(); + // Clear the block cache first, so stale data will + // not end up in the page cache. + if (module_exists('block')) { + cache('block')->expire(); + } + cache('page')->expire(); } /** @@ -326,25 +187,6 @@ function expire(); */ function garbageCollection(); - /** - * Expires data from the cache. - * - * If called without arguments, expirable entries will be cleared from the - * cache_page and cache_block bins. - * - * @param $cid - * If set, the cache ID to delete. Otherwise, all cache entries that can - * expire are deleted. - * @param $wildcard - * If set to TRUE, the $cid is treated as a substring - * to match rather than a complete ID. The match is a right hand - * match. If '*' is given as $cid, the bin $bin will be emptied. - * - * @todo: This method is deprecated, as its functionality is covered by more - * targeted methods in the interface. - */ - function clear($cid = NULL, $wildcard = FALSE); - /** * Checks if a cache bin is empty. * @@ -424,11 +266,6 @@ function expire() {} */ function garbageCollection() {} - /** - * Implements DrupalCacheInterface::clear(). - */ - function clear($cid = NULL, $wildcard = FALSE) {} - /** * Implements DrupalCacheInterface::isEmpty(). */ @@ -450,8 +287,8 @@ class DrupalDatabaseCache implements DrupalCacheInterface { * Constructs a new DrupalDatabaseCache object. */ function __construct($bin) { - // All cache tables should be prefixed with 'cache_', apart from the - // default 'cache' bin, which would look silly. + // All cache tables should be prefixed with 'cache_', except for the + // default 'cache' bin. if ($bin != 'cache') { $bin = 'cache_' . $bin; } @@ -522,9 +359,9 @@ protected function prepareItem($cache) { // If enforcing a minimum cache lifetime, validate that the data is // currently valid for this user before we return it by making sure the cache // entry was created before the timestamp in the current session's cache - // timer. The cache variable is loaded into the $user object by _drupal_session_read() - // in session.inc. If the data is permanent or we're not enforcing a minimum - // cache lifetime always return the cached data. + // timer. The cache variable is loaded into the $user object by + // _drupal_session_read() in session.inc. If the data is permanent or we're + // not enforcing a minimum cache lifetime always return the cached data. if ($cache->expire != CACHE_PERMANENT && variable_get('cache_lifetime', 0) && $user->cache > $cache->created) { // This cache data is too old and thus not valid for us, ignore it. return FALSE; @@ -562,7 +399,7 @@ function set($cid, $data, $expire = CACHE_PERMANENT) { ->execute(); } catch (Exception $e) { - // The database may not be available, so we'll ignore cache_set requests. + // The database may not be available, so we'll ignore these calls. } } @@ -617,12 +454,12 @@ function expire() { $cache_flush = variable_get('cache_flush_' . $this->bin, 0); if ($cache_flush == 0) { - // This is the first request to clear the cache, start a timer. + // This is the first request to clear the cache; start a timer. variable_set('cache_flush_' . $this->bin, REQUEST_TIME); } elseif (REQUEST_TIME > ($cache_flush + variable_get('cache_lifetime', 0))) { - // Clear the cache for everyone, cache_lifetime seconds have - // passed since the first request to clear the cache. + // Clear the cache for everyone; cache_lifetime seconds have passed + // since the first request to clear the cache. db_delete($this->bin) ->condition('expire', CACHE_PERMANENT, '<>') ->condition('expire', REQUEST_TIME, '<') @@ -631,7 +468,7 @@ function expire() { } } else { - // No minimum cache lifetime, flush all temporary cache entries now. + // No minimum cache lifetime; flush all temporary cache entries now. db_delete($this->bin) ->condition('expire', CACHE_PERMANENT, '<>') ->condition('expire', REQUEST_TIME, '<') @@ -649,7 +486,8 @@ function garbageCollection() { // often since this will remove temporary cache items indiscriminately. $cache_flush = variable_get('cache_flush_' . $this->bin, 0); if ($cache_flush && ($cache_flush + variable_get('cache_lifetime', 0) <= REQUEST_TIME)) { - // Reset the variable immediately to prevent a meltdown in heavy load situations. + // Reset the variable immediately to prevent a meltdown in heavy load + // situations. variable_set('cache_flush_' . $this->bin, 0); // Time to flush old cache data db_delete($this->bin) @@ -659,33 +497,6 @@ function garbageCollection() { } } - /** - * Implements DrupalCacheInterface::clear(). - */ - function clear($cid = NULL, $wildcard = FALSE) { - global $user; - - if (empty($cid)) { - $this->expire(); - } - else { - if ($wildcard) { - if ($cid == '*') { - $this->flush(); - } - else { - $this->deletePrefix($cid); - } - } - elseif (is_array($cid)) { - $this->deleteMultiple($cid); - } - else { - $this->delete($cid); - } - } - } - /** * Implements DrupalCacheInterface::isEmpty(). */ diff --git a/core/includes/common.inc b/core/includes/common.inc index 9da67ac903120c2b21e573d4708dad224def25fb..3d19d3e300278bc72a4707fd781519088ae7f99f 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -6090,7 +6090,7 @@ function drupal_render_collect_attached($elements, $return = FALSE) { * added to this string and is also part of the cache key in * drupal_render_cache_set() and drupal_render_cache_get(). * @param $expire - * The cache expire time, passed eventually to cache_set(). + * The cache expire time, passed eventually to cache()->set(). * @param $granularity * One or more granularity constants passed to drupal_render_cid_parts(). * diff --git a/core/includes/menu.inc b/core/includes/menu.inc index b687d3da42fbf3db46009a8a4ca6ddefb5905a8c..94123101e6d6837c6e785b00ab23d7677e7e1eda 100644 --- a/core/includes/menu.inc +++ b/core/includes/menu.inc @@ -2612,17 +2612,7 @@ function menu_link_load($mlid) { * Clears the cached cached data for a single named menu. */ function menu_cache_clear($menu_name = 'navigation') { - $cache_cleared = &drupal_static(__FUNCTION__, array()); - - if (empty($cache_cleared[$menu_name])) { - cache('menu')->deletePrefix('links:' . $menu_name . ':'); - $cache_cleared[$menu_name] = 1; - } - elseif ($cache_cleared[$menu_name] == 1) { - drupal_register_shutdown_function('cache_clear_all', 'links:' . $menu_name . ':', 'cache_menu', TRUE); - $cache_cleared[$menu_name] = 2; - } - + cache('menu')->deletePrefix('links:' . $menu_name . ':'); // Also clear the menu system static caches. menu_reset_static_cache(); } diff --git a/core/includes/module.inc b/core/includes/module.inc index 221ad605a4ea1d9a03926428652cde9da74e9134..63d18ce3adf46de553ea5ba1062c87ef6361d3d9 100644 --- a/core/includes/module.inc +++ b/core/includes/module.inc @@ -700,10 +700,10 @@ function module_implements_reset() { // request. Benchmarks show that the benefit of this caching outweighs the // additional database hit even when using the default database caching // backend and only a small number of modules are enabled. The cost of the - // cache_get() is more or less constant and reduced further when non-database - // caching backends are used, so there will be more significant gains when a - // large number of modules are installed or hooks invoked, since this can - // quickly lead to module_hook() being called several thousand times + // cache('bootstrap')->get() is more or less constant and reduced further when + // non-database caching backends are used, so there will be more significant + // gains when a large number of modules are installed or hooks invoked, since + // this can quickly lead to module_hook() being called several thousand times // per request. drupal_static_reset('module_implements'); cache('bootstrap')->set('module_implements', array()); diff --git a/core/includes/theme.inc b/core/includes/theme.inc index 5304d25c0c184cf180bf69ed8f1faf8c7173ddb7..94d69d228f25943f7ce13d78fc5dfd58ac4464a0 100644 --- a/core/includes/theme.inc +++ b/core/includes/theme.inc @@ -652,7 +652,7 @@ function _theme_build_registry($theme, $base_theme, $theme_engine) { // serve as the basic registry. Since the list of enabled modules is the same // regardless of the theme used, this is cached in its own entry to save // building it for every theme. - if ($cached = cache_get('theme_registry:build:modules')) { + if ($cached = cache()->get('theme_registry:build:modules')) { $cache = $cached->data; } else { @@ -661,7 +661,7 @@ function _theme_build_registry($theme, $base_theme, $theme_engine) { } // Only cache this registry if all modules are loaded. if (module_load_all(NULL)) { - cache_set('theme_registry:build:modules', $cache); + cache()->set('theme_registry:build:modules', $cache); } } diff --git a/core/modules/field/field.attach.inc b/core/modules/field/field.attach.inc index aa91402b2a01912e51317e53aa21dfcf34814825..929172551ece5516c4758fff8ef78c75af02ab5b 100644 --- a/core/modules/field/field.attach.inc +++ b/core/modules/field/field.attach.inc @@ -643,7 +643,7 @@ function field_attach_load($entity_type, $entities, $age = FIELD_LOAD_CURRENT, $ foreach ($entities as $id => $entity) { $cids[] = "field:$entity_type:$id"; } - $cache = cache_get_multiple($cids, 'cache_field'); + $cache = cache('field')->getMultiple($cids); // Put the cached field values back into the entities and remove them from // the list of entities to query. foreach ($entities as $id => $entity) { diff --git a/core/modules/simpletest/tests/cache.test b/core/modules/simpletest/tests/cache.test index 664247b8ab58b7576606c4c3813c1b2490f9cf1b..bca4e250b3790e79ae8937d32c15423ea97bcc69 100644 --- a/core/modules/simpletest/tests/cache.test +++ b/core/modules/simpletest/tests/cache.test @@ -1,7 +1,7 @@ drupalGet('user/register'); - $this->assertFalse(cache_get(''), t('No cache entry is written with an empty cid.')); + $this->assertFalse(cache()->get(''), t('No cache entry is written with an empty cid.')); } } diff --git a/core/modules/system/system.install b/core/modules/system/system.install index 01579643634057e26b3d9e0f270961400f8a32b0..20e1dc19105701881c2a9867a617cbb676f8053f 100644 --- a/core/modules/system/system.install +++ b/core/modules/system/system.install @@ -1471,7 +1471,7 @@ function system_schema() { 'default' => 0, ), 'cache' => array( - 'description' => "The time of this user's last post. This is used when the site has specified a minimum_cache_lifetime. See cache_get().", + 'description' => "The time of this user's last post. This is used when the site has specified a minimum_cache_lifetime. See DrupalCacheInterface::get().", 'type' => 'int', 'not null' => TRUE, 'default' => 0, diff --git a/core/modules/update/update.module b/core/modules/update/update.module index 5612b073d9d3ed2447ca3e2e3f36ec4b8f4c8094..57a2854fd1a219e14e6d1fc6176f0a1640f82e17 100644 --- a/core/modules/update/update.module +++ b/core/modules/update/update.module @@ -724,10 +724,10 @@ function update_verify_update_archive($project, $archive_file, $directory) { * plug-able cache system that assumes volatile caches. * * Update module still uses the {cache_update} table, but instead of using - * cache_set(), cache_get(), and cache_clear_all(), there are private helper - * functions that implement these same basic tasks but ensure that the cache - * is not prematurely cleared, and that the data is always stored in the - * database, even if memcache or another cache backend is in use. + * the cache API, there are private helper functions that implement these same + * basic tasks but ensure that the cache is not prematurely cleared, and that + * the data is always stored in the database, even if memcache or another cache + * backend is in use. */ /**