diff options
author | Dries Buytaert <dries@buytaert.net> | 2007-08-19 08:08:45 +0000 |
---|---|---|
committer | Dries Buytaert <dries@buytaert.net> | 2007-08-19 08:08:45 +0000 |
commit | 74292cd0625032180b2d178ae1c57e51462f121e (patch) | |
tree | 79ffaafcf1cd4c7b4a2dd2d33512228a0efb597c /modules | |
parent | e9c36f969752488ca33da745b15805c489932a30 (diff) | |
download | brdo-74292cd0625032180b2d178ae1c57e51462f121e.tar.gz brdo-74292cd0625032180b2d178ae1c57e51462f121e.tar.bz2 |
- Patch #80951 by killes, yched et al: block caching.
Diffstat (limited to 'modules')
-rw-r--r-- | modules/block/block.admin.inc | 2 | ||||
-rw-r--r-- | modules/block/block.module | 121 | ||||
-rw-r--r-- | modules/block/block.schema | 5 | ||||
-rw-r--r-- | modules/book/book.module | 3 | ||||
-rw-r--r-- | modules/locale/locale.module | 2 | ||||
-rw-r--r-- | modules/menu/menu.module | 5 | ||||
-rw-r--r-- | modules/node/node.module | 6 | ||||
-rw-r--r-- | modules/profile/profile.module | 2 | ||||
-rw-r--r-- | modules/search/search.module | 2 | ||||
-rw-r--r-- | modules/statistics/statistics.module | 2 | ||||
-rw-r--r-- | modules/system/system.install | 46 | ||||
-rw-r--r-- | modules/system/system.module | 23 | ||||
-rw-r--r-- | modules/user/user.module | 11 |
13 files changed, 216 insertions, 14 deletions
diff --git a/modules/block/block.admin.inc b/modules/block/block.admin.inc index 39b7df0fe..e660b2688 100644 --- a/modules/block/block.admin.inc +++ b/modules/block/block.admin.inc @@ -262,7 +262,7 @@ function block_add_block_form_submit($form, &$form_state) { foreach (list_themes() as $key => $theme) { if ($theme->status) { - db_query("INSERT INTO {blocks} (visibility, pages, custom, title, module, theme, status, weight, delta) VALUES(%d, '%s', %d, '%s', '%s', '%s', %d, %d, %d)", $form_state['values']['visibility'], trim($form_state['values']['pages']), $form_state['values']['custom'], $form_state['values']['title'], $form_state['values']['module'], $theme->name, 0, 0, $delta); + db_query("INSERT INTO {blocks} (visibility, pages, custom, title, module, theme, status, weight, delta, cache) VALUES(%d, '%s', %d, '%s', '%s', '%s', %d, %d, %d)", $form_state['values']['visibility'], trim($form_state['values']['pages']), $form_state['values']['custom'], $form_state['values']['title'], $form_state['values']['module'], $theme->name, 0, 0, $delta, BLOCK_NO_CACHE); } } diff --git a/modules/block/block.module b/modules/block/block.module index d1e73c2e9..ec87d3ca3 100644 --- a/modules/block/block.module +++ b/modules/block/block.module @@ -13,6 +13,55 @@ define('BLOCK_REGION_NONE', -1); /** + * Constants defining cache granularity for blocks. + * + * Modules specify the caching patterns for their blocks using binary + * combinations of these constants in their hook_block(op 'list'): + * $block[delta]['cache'] = BLOCK_CACHE_PER_ROLE | BLOCK_CACHE_PER_PAGE; + * BLOCK_CACHE_PER_ROLE is used as a default when no caching pattern is + * specified. + * + * The block cache is cleared in cache_clear_all(), and uses the same clearing + * policy than page cache (node, comment, user, taxonomy added or updated...). + * Blocks requiring more fine-grained clearing might consider disabling the + * built-in block cache (BLOCK_NO_CACHE) and roll their own. + * + * Note that user 1 is excluded from block caching. + */ + +/** + * The block should not get cached. This setting should be used: + * - for simple blocks (notably those that do not perform any db query), + * where querying the db cache would be more expensive than directly generating + * the content. + * - for blocks that change too frequently. + */ +define('BLOCK_NO_CACHE', -1); + +/** +* The block can change depending on the roles the user viewing the page belongs to. +* This is the default setting, used when the block does not specify anything. +*/ +define('BLOCK_CACHE_PER_ROLE', 0x0001); + +/** +* The block can change depending on the user viewing the page. +* This setting can be resource-consuming for sites with large number of users, +* and thus should only be used when BLOCK_CACHE_PER_ROLE is not sufficient. +*/ +define('BLOCK_CACHE_PER_USER', 0x0002); + +/** +* The block can change depending on the page being viewed. +*/ +define('BLOCK_CACHE_PER_PAGE', 0x0004); + +/** + * The block is the same for every user on every page where it is visible. + */ +define('BLOCK_CACHE_GLOBAL', 0x0008); + +/** * Implementation of hook_help(). */ function block_help($path, $arg) { @@ -187,6 +236,8 @@ function block_block($op = 'list', $delta = 0, $edit = array()) { $result = db_query('SELECT bid, info FROM {boxes} ORDER BY info'); while ($block = db_fetch_object($result)) { $blocks[$block->bid]['info'] = $block->info; + // Not worth caching. + $blocks[$block->bid]['cache'] = BLOCK_NO_CACHE; } return $blocks; @@ -235,6 +286,8 @@ function _block_rehash() { foreach ($module_blocks as $delta => $block) { $block['module'] = $module; $block['delta'] = $delta; + // If no cache pattern is specified, we use PER_ROLE as a default. + $block['cache'] = isset($block['cache']) ? $block['cache'] : BLOCK_CACHE_PER_ROLE; // If previously written to database, load values. if (!empty($old_blocks[$module][$delta])) { $block['status'] = $old_blocks[$module][$delta]->status; @@ -271,7 +324,7 @@ function _block_rehash() { 'visibility' => NULL, 'throttle' => NULL, ); - db_query("INSERT INTO {blocks} (module, delta, theme, status, weight, region, visibility, pages, custom, throttle, title) VALUES ('%s', '%s', '%s', %d, %d, '%s', %d, '%s', %d, %d, '%s')", $block['module'], $block['delta'], $theme_key, $block['status'], $block['weight'], $block['region'], $block['visibility'], $block['pages'], $block['custom'], $block['throttle'], $block['title']); + db_query("INSERT INTO {blocks} (module, delta, theme, status, weight, region, visibility, pages, custom, throttle, title, cache) VALUES ('%s', '%s', '%s', %d, %d, '%s', %d, '%s', %d, %d, '%s', %d)", $block['module'], $block['delta'], $theme_key, $block['status'], $block['weight'], $block['region'], $block['visibility'], $block['pages'], $block['custom'], $block['throttle'], $block['title'], $block['cache']); } db_unlock_tables(); @@ -429,7 +482,19 @@ function block_list($region) { // Check the current throttle status and see if block should be displayed // based on server load. if (!($block->throttle && (module_invoke('throttle', 'status') > 0))) { - $array = module_invoke($block->module, 'block', 'view', $block->delta); + // Try fetching the block from cache. Block caching is not compatible with + // node_access modules. We also preserve the submission of forms in blocks, + // by fetching from cache only if the request method is 'GET'. + if (!count(module_implements('node_grants')) && $_SERVER['REQUEST_METHOD'] == 'GET' && ($cid = _block_get_cache_id($block)) && ($cache = cache_get($cid, 'cache_block'))) { + $array = $cache->data; + } + else { + $array = module_invoke($block->module, 'block', 'view', $block->delta); + if (isset($cid)) { + cache_set($cid, $array, 'cache_block', CACHE_TEMPORARY); + } + } + if (isset($array) && is_array($array)) { foreach ($array as $k => $v) { $block->$k = $v; @@ -453,3 +518,55 @@ function block_list($region) { } return $blocks[$region]; } + +/** + * Assemble the cache_id to use for a given block. + * + * The cache_id string reflects the viewing context for the current block + * instance, obtained by concatenating the relevant context information + * (user, page, ...) according to the block's cache settings (BLOCK_CACHE_* + * constants). Two block instances can use the same cached content when + * they share the same cache_id. + * + * Theme and language contexts are automatically differenciated. + * + * @param $block + * @return + * The string used as cache_id for the block. + */ +function _block_get_cache_id($block) { + global $theme, $base_root, $user; + + // User 1 being out of the regular 'roles define permissions' schema, + // it brings too many chances of having unwanted output get in the cache + // and later be served to other users. We therefore exclude user 1 from + // block caching. + if (variable_get('block_cache', 0) && $block->cache != BLOCK_NO_CACHE && $user->uid != 1) { + $cid_parts = array(); + + // Start with common sub-patterns: block identification, theme, language. + $cid_parts[] = $block->module; + $cid_parts[] = $block->delta; + $cid_parts[] = $theme; + if (module_exists('locale')) { + global $language; + $cid_parts[] = $language->language; + } + + // 'PER_ROLE' and 'PER_USER' are mutually exclusive. 'PER_USER' can be a + // resource drag for sites with many users, so when a module is being + // equivocal, we favor the less expensive 'PER_ROLE' pattern. + if ($block->cache & BLOCK_CACHE_PER_ROLE) { + $cid_parts[] = 'r.'. implode(',', array_keys($user->roles)); + } + elseif ($block->cache & BLOCK_CACHE_PER_USER) { + $cid_parts[] = "u.$user->uid"; + } + + if ($block->cache & BLOCK_CACHE_PER_PAGE) { + $cid_parts[] = $base_root . request_uri(); + } + + return implode(':', $cid_parts); + } +}
\ No newline at end of file diff --git a/modules/block/block.schema b/modules/block/block.schema index 2aca16075..e025867a9 100644 --- a/modules/block/block.schema +++ b/modules/block/block.schema @@ -15,7 +15,8 @@ function block_schema() { 'throttle' => array('type' => 'int', 'not null' => TRUE, 'default' => 0, 'size' => 'tiny'), 'visibility' => array('type' => 'int', 'not null' => TRUE, 'default' => 0, 'size' => 'tiny'), 'pages' => array('type' => 'text', 'not null' => TRUE), - 'title' => array('type' => 'varchar', 'length' => 64, 'not null' => TRUE, 'default' => '') + 'title' => array('type' => 'varchar', 'length' => 64, 'not null' => TRUE, 'default' => ''), + 'cache' => array('type' => 'int', 'not null' => TRUE, 'default' => 0, 'size' => 'tiny'), ), 'primary key' => array('bid'), ); @@ -44,6 +45,8 @@ function block_schema() { 'primary key' => array('bid'), ); + $schema['cache_block'] = drupal_get_schema_unprocessed('system', 'cache'); + return $schema; } diff --git a/modules/book/book.module b/modules/book/book.module index 1e4eb7ef0..245651d09 100644 --- a/modules/book/book.module +++ b/modules/book/book.module @@ -164,6 +164,7 @@ function book_block($op = 'list', $delta = 0, $edit = array()) { switch ($op) { case 'list': $block[0]['info'] = t('Book navigation'); + $block[0]['cache'] = BLOCK_CACHE_PER_PAGE | BLOCK_CACHE_PER_ROLE; return $block; case 'view': if (arg(0) == 'node' && is_numeric(arg(1))) { @@ -440,7 +441,7 @@ function _book_add_form_elements(&$form, $node) { ); $options = array(); $nid = isset($node->nid) ? $node->nid : 'new'; - + if (isset($node->nid) && ($nid == $node->book['original_bid']) && $node->book['has_children'] && (MENU_MAX_DEPTH - 1 - menu_link_children_relative_depth($node->book) == 0)) { // This is the top level node in a maximum depth book and thus cannot be moved. $options[$node->nid] = $node->title; diff --git a/modules/locale/locale.module b/modules/locale/locale.module index 2fc47a299..417f99f19 100644 --- a/modules/locale/locale.module +++ b/modules/locale/locale.module @@ -516,6 +516,8 @@ function _locale_batch_import($filepath, &$context) { function locale_block($op = 'list', $delta = 0) { if ($op == 'list') { $block[0]['info'] = t('Language switcher'); + // Not worth caching. + $block[0]['cache'] = BLOCK_NO_CACHE; return $block; } diff --git a/modules/menu/menu.module b/modules/menu/menu.module index be44c1330..f9ad1a31c 100644 --- a/modules/menu/menu.module +++ b/modules/menu/menu.module @@ -404,7 +404,7 @@ function menu_parent_options($menus, $item) { // If the item has children, there is an added limit to the depth of valid parents. $limit = MENU_MAX_DEPTH - 1 - (($item['mlid'] && $item['has_children']) ? menu_link_children_relative_depth($item) : 0); - + foreach ($menus as $menu_name => $title) { $tree = menu_tree_all_data($menu_name, NULL); $options[$menu_name .':0'] = '<'. $title .'>'; @@ -666,6 +666,9 @@ function menu_block($op = 'list', $delta = 0) { foreach ($custom_menus as $name => $title) { // Default "Navigation" block is handled by user.module. $blocks[$name]['info'] = check_plain($title); + // Menu blocks can't be cached because each menu item can have + // a custom access callback. menu.inc manages its own caching. + $blocks[$name]['cache'] = BLOCK_NO_CACHE; } return $blocks; } diff --git a/modules/node/node.module b/modules/node/node.module index b7d2bcb76..51d52b12e 100644 --- a/modules/node/node.module +++ b/modules/node/node.module @@ -770,7 +770,7 @@ function node_save(&$node) { // Update the node access table for this node. node_access_acquire_grants($node); - // Clear the cache so an anonymous poster can see the node being added or updated. + // Clear the page and block caches. cache_clear_all(); } @@ -1915,6 +1915,8 @@ function node_admin_search() { function node_block($op = 'list', $delta = 0) { if ($op == 'list') { $blocks[0]['info'] = t('Syndicate'); + // Not worth caching. + $blocks[0]['cache'] = BLOCK_NO_CACHE; return $blocks; } else if ($op == 'view') { @@ -2502,7 +2504,7 @@ function node_delete($nid) { node_invoke($node, 'delete'); node_invoke_nodeapi($node, 'delete'); - // Clear the cache so an anonymous poster can see the node being deleted. + // Clear the page and block caches. cache_clear_all(); // Remove this node from the search index if needed. diff --git a/modules/profile/profile.module b/modules/profile/profile.module index 0468878ab..78d59ee70 100644 --- a/modules/profile/profile.module +++ b/modules/profile/profile.module @@ -125,7 +125,7 @@ function profile_block($op = 'list', $delta = 0, $edit = array()) { if ($op == 'list') { $blocks[0]['info'] = t('Author information'); - + $blocks[0]['cache'] = BLOCK_CACHE_PER_PAGE | BLOCK_CACHE_PER_ROLE; return $blocks; } else if ($op == 'configure' && $delta == 0) { diff --git a/modules/search/search.module b/modules/search/search.module index e9faa5149..cff9314e3 100644 --- a/modules/search/search.module +++ b/modules/search/search.module @@ -144,6 +144,8 @@ function search_perm() { function search_block($op = 'list', $delta = 0) { if ($op == 'list') { $blocks[0]['info'] = t('Search form'); + // Not worth caching. + $blocks[0]['cache'] = BLOCK_NO_CACHE; return $blocks; } else if ($op == 'view' && user_access('search content')) { diff --git a/modules/statistics/statistics.module b/modules/statistics/statistics.module index e84901285..8f33fdacf 100644 --- a/modules/statistics/statistics.module +++ b/modules/statistics/statistics.module @@ -502,6 +502,8 @@ function statistics_block($op = 'list', $delta = 0, $edit = array()) { case 'list': if (variable_get('statistics_count_content_views', 0)) { $blocks[0]['info'] = t('Popular content'); + // Too dynamic to cache. + $blocks[0]['cache'] = BLOCK_NO_CACHE; return $blocks; } break; diff --git a/modules/system/system.install b/modules/system/system.install index 56205fdc9..ffa68b16a 100644 --- a/modules/system/system.install +++ b/modules/system/system.install @@ -3481,6 +3481,52 @@ function system_update_6026() { } /** + * Add block cache. + */ +function system_update_6027() { + $ret = array(); + + // Create the blocks.cache column. + db_add_field($ret, 'blocks', 'cache', array('type' => 'int', 'not null' => TRUE, 'default' => 0, 'size' => 'tiny')); + + // Create the cache_block table. + $schema['cache_block'] = array( + 'fields' => array( + 'cid' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''), + 'data' => array('type' => 'blob', 'not null' => FALSE, 'size' => 'big'), + 'expire' => array('type' => 'int', 'not null' => TRUE, 'default' => 0), + 'created' => array('type' => 'int', 'not null' => TRUE, 'default' => 0), + 'headers' => array('type' => 'text', 'not null' => FALSE), + 'serialized' => array('type' => 'int', 'size' => 'small', 'not null' => TRUE, 'default' => 0) + ), + 'indexes' => array('expire' => array('expire')), + 'primary key' => array('cid'), + ); + db_create_table($ret, 'cache_block', $schema['cache_block']); + + // Fill in the values for the new 'cache' column, + // by refreshing the {blocks} table. + global $theme, $custom_theme; + $old_theme = $theme; + $themes = list_themes(); + + $result = db_query("SELECT DISTINCT theme FROM {blocks}"); + while ($row = db_fetch_array($result)) { + if (array_key_exists($row['theme'], $themes)) { + // Set up global values so that _blocks_rehash() + // operates on the expected theme. + $theme = NULL; + $custom_theme = $row['theme']; + _block_rehash(); + } + } + + $theme = $old_theme; + + return $ret; +} + +/** * @} End of "defgroup updates-5.x-to-6.x" * The next series of updates should start at 7000. */ diff --git a/modules/system/system.module b/modules/system/system.module index 9fc049644..e0152b5f1 100644 --- a/modules/system/system.module +++ b/modules/system/system.module @@ -683,7 +683,7 @@ function system_performance_settings() { $form['page_cache'] = array( '#type' => 'fieldset', '#title' => t('Page cache'), - '#description' => t('Enabling the cache will offer a significant performance boost. Drupal can store and send compressed cached pages requested by <em>anonymous</em> users. By caching a web page, Drupal does not have to construct the page each time someone wants to view it.'), + '#description' => t('Enabling the page cache will offer a significant performance boost. Drupal can store and send compressed cached pages requested by <em>anonymous</em> users. By caching a web page, Drupal does not have to construct the page each time someone wants to view it.'), ); $form['page_cache']['cache'] = array( @@ -701,7 +701,22 @@ function system_performance_settings() { '#title' => t('Minimum cache lifetime'), '#default_value' => variable_get('cache_lifetime', 0), '#options' => $period, - '#description' => t('On high-traffic sites it can become necessary to enforce a minimum cache lifetime. The minimum cache lifetime is the minimum amount of time that will go by before the cache is emptied and recreated. A larger minimum cache lifetime offers better performance, but users will not see new content for a longer period of time.') + '#description' => t('On high-traffic sites it can become necessary to enforce a minimum cache lifetime. The minimum cache lifetime is the minimum amount of time that will go by before the cache is emptied and recreated. A larger minimum cache lifetime offers better performance, but users will not see new content for a longer period of time. This setting also affects block caching.') + ); + + $form['block_cache'] = array( + '#type' => 'fieldset', + '#title' => t('Block cache'), + '#description' => t('Enabling the block cache can offer a performance increase for all users by preventing blocks from being reconstructed on every page load. If page cache is also enabled, this performance increase will mainly affect authenticated users.'), + ); + + $form['block_cache']['block_cache'] = array( + '#type' => 'radios', + '#title' => t('Block cache'), + '#default_value' => variable_get('block_cache', CACHE_DISABLED), + '#options' => array(CACHE_DISABLED => t('Disabled'), CACHE_NORMAL => t('Enabled (recommended)')), + '#disabled' => count(module_implements('node_grants')), + '#description' => t('Note that block caching is inactive when modules defining content access restrictions are enabled.'), ); $form['bandwidth_optimizations'] = array( @@ -1275,8 +1290,8 @@ function system_initialize_theme_blocks($theme) { if (!array_key_exists($block['region'], $regions)) { $block['region'] = system_default_region($theme); } - db_query("INSERT INTO {blocks} (module, delta, theme, status, weight, region, visibility, pages, custom, throttle) VALUES ('%s', '%s', '%s', %d, %d, '%s', %d, '%s', %d, %d)", - $block['module'], $block['delta'], $theme, $block['status'], $block['weight'], $block['region'], $block['visibility'], $block['pages'], $block['custom'], $block['throttle']); + db_query("INSERT INTO {blocks} (module, delta, theme, status, weight, region, visibility, pages, custom, throttle, cache) VALUES ('%s', '%s', '%s', %d, %d, '%s', %d, '%s', %d, %d, %d)", + $block['module'], $block['delta'], $theme, $block['status'], $block['weight'], $block['region'], $block['visibility'], $block['pages'], $block['custom'], $block['throttle'], $block['cache']); } } } diff --git a/modules/user/user.module b/modules/user/user.module index fdce324ab..9f2686722 100644 --- a/modules/user/user.module +++ b/modules/user/user.module @@ -627,10 +627,19 @@ function user_block($op = 'list', $delta = 0, $edit = array()) { if ($op == 'list') { $blocks[0]['info'] = t('User login'); + // Not worth caching. + $blocks[0]['cache'] = BLOCK_NO_CACHE; + $blocks[1]['info'] = t('Navigation'); + // Menu blocks can't be cached because each menu item can have + // a custom access callback. menu.inc manages its own caching. + $blocks[1]['cache'] = BLOCK_NO_CACHE; + $blocks[2]['info'] = t('Who\'s new'); - $blocks[3]['info'] = t('Who\'s online'); + // Too dynamic to cache. + $blocks[3]['info'] = t('Who\'s online'); + $blocks[3]['cache'] = BLOCK_NO_CACHE; return $blocks; } else if ($op == 'configure' && $delta == 2) { |