Commit 9e32c2e9 authored by Dries's avatar Dries

Patch based on work of Kjartan:

- Changed cache API.

- Fixed caching bug in comment.module.  Odd this hasn't been reported yet.

- Fixed caching bug in forum.module.

- Fixed caching bug in system.module.

- Fixed caching bug in block.module.

- Simplified caching support in forum.module thanks to improved cache API.
parent 64f8781f
...@@ -473,12 +473,22 @@ function cache_set($cid, $data, $expire = 0) { ...@@ -473,12 +473,22 @@ function cache_set($cid, $data, $expire = 0) {
} }
} }
function cache_del($cid) { function cache_clear_all($cid = NULL) {
db_query("DELETE FROM cache WHERE cid = '%s'", $cid); if (empty($cid)) {
db_query("DELETE FROM cache");
}
else {
db_query("DELETE FROM cache WHERE cid = '%s'", $cid);
}
} }
function cache_clear() { function cache_clear_old($cid = NULL) {
db_query("DELETE FROM cache WHERE expire < ". time() ." AND expire > 0"); if (empty($cid)) {
db_query("DELETE FROM cache WHERE expire < ". time() ." AND expire > 0");
}
else {
db_query("DELETE FROM cache WHERE cid = '%s' AND expire < %s AND expire > 0", $cid, time());
}
} }
function page_set_cache() { function page_set_cache() {
...@@ -486,7 +496,7 @@ function page_set_cache() { ...@@ -486,7 +496,7 @@ function page_set_cache() {
if (!$user->uid && $REQUEST_METHOD == "GET") { if (!$user->uid && $REQUEST_METHOD == "GET") {
if ($data = ob_get_contents()) { if ($data = ob_get_contents()) {
cache_set(request_uri(), $data, (time() + variable_get("cache_clear", 30))); cache_set(request_uri(), $data, (time() + variable_get("cache_clear", 120)));
} }
} }
} }
...@@ -496,7 +506,7 @@ function page_get_cache() { ...@@ -496,7 +506,7 @@ function page_get_cache() {
if (!$user->uid && $REQUEST_METHOD == "GET") { if (!$user->uid && $REQUEST_METHOD == "GET") {
if ($cache = cache_get(request_uri())) { if ($cache = cache_get(request_uri())) {
cache_clear(); cache_clear_old();
} }
else { else {
ob_start(); ob_start();
......
...@@ -77,6 +77,12 @@ function block_admin_save($edit) { ...@@ -77,6 +77,12 @@ function block_admin_save($edit) {
db_query("UPDATE blocks SET region = '%s', status = '%d', custom = '%d', path = '%s', weight = '%d' WHERE module = '%s' AND delta = '%d'", $block["region"], $block["status"], $block["custom"], $block["path"], $block["weight"], $module, $delta); db_query("UPDATE blocks SET region = '%s', status = '%d', custom = '%d', path = '%s', weight = '%d' WHERE module = '%s' AND delta = '%d'", $block["region"], $block["status"], $block["custom"], $block["path"], $block["weight"], $module, $delta);
} }
} }
/*
** Clear the cache so the changes take effect for anonymous users.
*/
cache_clear_all();
} }
/** /**
......
...@@ -77,6 +77,12 @@ function block_admin_save($edit) { ...@@ -77,6 +77,12 @@ function block_admin_save($edit) {
db_query("UPDATE blocks SET region = '%s', status = '%d', custom = '%d', path = '%s', weight = '%d' WHERE module = '%s' AND delta = '%d'", $block["region"], $block["status"], $block["custom"], $block["path"], $block["weight"], $module, $delta); db_query("UPDATE blocks SET region = '%s', status = '%d', custom = '%d', path = '%s', weight = '%d' WHERE module = '%s' AND delta = '%d'", $block["region"], $block["status"], $block["custom"], $block["path"], $block["weight"], $module, $delta);
} }
} }
/*
** Clear the cache so the changes take effect for anonymous users.
*/
cache_clear_all();
} }
/** /**
......
...@@ -276,17 +276,11 @@ function comment_post($edit) { ...@@ -276,17 +276,11 @@ function comment_post($edit) {
} }
/* /*
** Clear the cache: ** Clear the cache so an anonymous user can see his comment being
** added.
*/ */
cache_clear(); cache_clear_all();
/*
** TODO: we'd prefer to invalidate the page or pages with the newly
** inserted comment.
**
** db_query("DELETE FROM cache WHERE cid LIKE '%s'", "%id=".$edit["nid"]."%");
*/
} }
} }
else { else {
......
...@@ -276,17 +276,11 @@ function comment_post($edit) { ...@@ -276,17 +276,11 @@ function comment_post($edit) {
} }
/* /*
** Clear the cache: ** Clear the cache so an anonymous user can see his comment being
** added.
*/ */
cache_clear(); cache_clear_all();
/*
** TODO: we'd prefer to invalidate the page or pages with the newly
** inserted comment.
**
** db_query("DELETE FROM cache WHERE cid LIKE '%s'", "%id=".$edit["nid"]."%");
*/
} }
} }
else { else {
......
...@@ -43,7 +43,6 @@ function forum_conf_options() { ...@@ -43,7 +43,6 @@ function forum_conf_options() {
$forder = array(1 => "Date - newest first", 2 => "Date - oldest first", 3 => "Posts - most active first", 4=> "Posts - least active first"); $forder = array(1 => "Date - newest first", 2 => "Date - oldest first", 3 => "Posts - most active first", 4=> "Posts - least active first");
$output .= form_select("Default order", "forum_order", variable_get("forum_order", 1), $forder, "The default display order for topics."); $output .= form_select("Default order", "forum_order", variable_get("forum_order", 1), $forder, "The default display order for topics.");
$output .= form_textfield("Number of topics in block", "forum_block_num", variable_get("forum_block_num", "5"), 5, 5, "The number of topics in the <b>Forum topics</b>-block. To enable the block, click ". la("here", array("mod" => "block")) ."."); $output .= form_textfield("Number of topics in block", "forum_block_num", variable_get("forum_block_num", "5"), 5, 5, "The number of topics in the <b>Forum topics</b>-block. To enable the block, click ". la("here", array("mod" => "block")) .".");
$output .= form_select("Internal caching", "forum_cache", variable_get("forum_cache", 0), array("disabled", "enabled"), "Cache internal datastructures for both anonymous and autheticated users. When enabled, the forum data won't be 100% up to date with regards regard to the number of posts, the number of replies and the last topic being posted. Enable when you have busy forum.");
return $output; return $output;
} }
...@@ -91,9 +90,7 @@ function forum_block($op = "list", $delta = 0) { ...@@ -91,9 +90,7 @@ function forum_block($op = "list", $delta = 0) {
} }
else { else {
if (user_access("access content")) { if (user_access("access content")) {
if (variable_get("forum_cache", 0)) { $content = cache_get("forum:block");
$content = cache_get("forum:block");
}
if (!$content) { if (!$content) {
unset($items); unset($items);
...@@ -107,7 +104,7 @@ function forum_block($op = "list", $delta = 0) { ...@@ -107,7 +104,7 @@ function forum_block($op = "list", $delta = 0) {
$content .= "<div id=\"forum_more\" align=\"right\">". lm(t("more"), array("mod" => "forum")) ."</div>"; $content .= "<div id=\"forum_more\" align=\"right\">". lm(t("more"), array("mod" => "forum")) ."</div>";
} }
cache_set("forum:block", $content, time() + 60 * 3); cache_set("forum:block", $content, time() + variable_get("cache_clear", 120));
} }
$blocks["subject"] = t("Forum topics"); $blocks["subject"] = t("Forum topics");
...@@ -256,10 +253,6 @@ function forum_insert($node) { ...@@ -256,10 +253,6 @@ function forum_insert($node) {
// we created a shadow, a link to a moved topic in a new forum // we created a shadow, a link to a moved topic in a new forum
db_query("INSERT INTO forum (nid, icon, shadow) VALUES ('%d', '%s', '%d')", $node->nid, $node->icon, $node->shadow); db_query("INSERT INTO forum (nid, icon, shadow) VALUES ('%d', '%s', '%d')", $node->nid, $node->icon, $node->shadow);
} }
if (variable_get("forum_cache", 0)) {
cache_clear();
}
} }
function forum_update($node) { function forum_update($node) {
...@@ -326,9 +319,7 @@ function forum_get_forums($tid = 0) { ...@@ -326,9 +319,7 @@ function forum_get_forums($tid = 0) {
$tid = 0; $tid = 0;
} }
if (variable_get("forum_cache", 0)) { $forums = unserialize(cache_get("forum:$tid"));
$forums = unserialize(cache_get("forum:$tid"));
}
if (!$forums) { if (!$forums) {
$forums = array(); $forums = array();
...@@ -347,9 +338,7 @@ function forum_get_forums($tid = 0) { ...@@ -347,9 +338,7 @@ function forum_get_forums($tid = 0) {
$n++; $n++;
} }
if (variable_get("forum_cache", 0)) { cache_set("forum:$tid", serialize($forums), time() + variable_get("cache_clear", 120));
cache_set("forum:$tid", serialize($forums), time()+60*10);
}
} }
if ($user->uid && $forums) { if ($user->uid && $forums) {
......
...@@ -43,7 +43,6 @@ function forum_conf_options() { ...@@ -43,7 +43,6 @@ function forum_conf_options() {
$forder = array(1 => "Date - newest first", 2 => "Date - oldest first", 3 => "Posts - most active first", 4=> "Posts - least active first"); $forder = array(1 => "Date - newest first", 2 => "Date - oldest first", 3 => "Posts - most active first", 4=> "Posts - least active first");
$output .= form_select("Default order", "forum_order", variable_get("forum_order", 1), $forder, "The default display order for topics."); $output .= form_select("Default order", "forum_order", variable_get("forum_order", 1), $forder, "The default display order for topics.");
$output .= form_textfield("Number of topics in block", "forum_block_num", variable_get("forum_block_num", "5"), 5, 5, "The number of topics in the <b>Forum topics</b>-block. To enable the block, click ". la("here", array("mod" => "block")) ."."); $output .= form_textfield("Number of topics in block", "forum_block_num", variable_get("forum_block_num", "5"), 5, 5, "The number of topics in the <b>Forum topics</b>-block. To enable the block, click ". la("here", array("mod" => "block")) .".");
$output .= form_select("Internal caching", "forum_cache", variable_get("forum_cache", 0), array("disabled", "enabled"), "Cache internal datastructures for both anonymous and autheticated users. When enabled, the forum data won't be 100% up to date with regards regard to the number of posts, the number of replies and the last topic being posted. Enable when you have busy forum.");
return $output; return $output;
} }
...@@ -91,9 +90,7 @@ function forum_block($op = "list", $delta = 0) { ...@@ -91,9 +90,7 @@ function forum_block($op = "list", $delta = 0) {
} }
else { else {
if (user_access("access content")) { if (user_access("access content")) {
if (variable_get("forum_cache", 0)) { $content = cache_get("forum:block");
$content = cache_get("forum:block");
}
if (!$content) { if (!$content) {
unset($items); unset($items);
...@@ -107,7 +104,7 @@ function forum_block($op = "list", $delta = 0) { ...@@ -107,7 +104,7 @@ function forum_block($op = "list", $delta = 0) {
$content .= "<div id=\"forum_more\" align=\"right\">". lm(t("more"), array("mod" => "forum")) ."</div>"; $content .= "<div id=\"forum_more\" align=\"right\">". lm(t("more"), array("mod" => "forum")) ."</div>";
} }
cache_set("forum:block", $content, time() + 60 * 3); cache_set("forum:block", $content, time() + variable_get("cache_clear", 120));
} }
$blocks["subject"] = t("Forum topics"); $blocks["subject"] = t("Forum topics");
...@@ -256,10 +253,6 @@ function forum_insert($node) { ...@@ -256,10 +253,6 @@ function forum_insert($node) {
// we created a shadow, a link to a moved topic in a new forum // we created a shadow, a link to a moved topic in a new forum
db_query("INSERT INTO forum (nid, icon, shadow) VALUES ('%d', '%s', '%d')", $node->nid, $node->icon, $node->shadow); db_query("INSERT INTO forum (nid, icon, shadow) VALUES ('%d', '%s', '%d')", $node->nid, $node->icon, $node->shadow);
} }
if (variable_get("forum_cache", 0)) {
cache_clear();
}
} }
function forum_update($node) { function forum_update($node) {
...@@ -326,9 +319,7 @@ function forum_get_forums($tid = 0) { ...@@ -326,9 +319,7 @@ function forum_get_forums($tid = 0) {
$tid = 0; $tid = 0;
} }
if (variable_get("forum_cache", 0)) { $forums = unserialize(cache_get("forum:$tid"));
$forums = unserialize(cache_get("forum:$tid"));
}
if (!$forums) { if (!$forums) {
$forums = array(); $forums = array();
...@@ -347,9 +338,7 @@ function forum_get_forums($tid = 0) { ...@@ -347,9 +338,7 @@ function forum_get_forums($tid = 0) {
$n++; $n++;
} }
if (variable_get("forum_cache", 0)) { cache_set("forum:$tid", serialize($forums), time() + variable_get("cache_clear", 120));
cache_set("forum:$tid", serialize($forums), time()+60*10);
}
} }
if ($user->uid && $forums) { if ($user->uid && $forums) {
......
...@@ -208,6 +208,13 @@ function node_save($node, $filter) { ...@@ -208,6 +208,13 @@ function node_save($node, $filter) {
} }
/*
** Clear the cache so an anonymous poster can see the node being added
** or updated.
*/
cache_clear_all();
/* /*
** Return the node ID: ** Return the node ID:
*/ */
......
...@@ -208,6 +208,13 @@ function node_save($node, $filter) { ...@@ -208,6 +208,13 @@ function node_save($node, $filter) {
} }
/*
** Clear the cache so an anonymous poster can see the node being added
** or updated.
*/
cache_clear_all();
/* /*
** Return the node ID: ** Return the node ID:
*/ */
......
...@@ -15,7 +15,7 @@ function system_system($field){ ...@@ -15,7 +15,7 @@ function system_system($field){
function system_help_cache() { function system_help_cache() {
$output .= "<p>Drupal has a caching mechanism that stores dynamically generated pages in a database. By caching a page, Drupal does not have to generate the page each time it is requested. Only pages requested by anonymous users are being cached. When a cached page is accessed, Drupal will retrieve that page with minimal overhead using one SQL query only, thus reducing both the server load and the response time.</p>"; $output .= "<p>Drupal has a caching mechanism that stores dynamically generated pages in a database. By caching a page, Drupal does not have to generate the page each time it is requested. Only pages requested by anonymous users are being cached. When a cached page is accessed, Drupal will retrieve that page with minimal overhead using one SQL query only, thus reducing both the server load and the response time.</p>";
$output .= "<p>Drupal's caching mechanism can be enabled and disabled by the site administrators from the 'settings' page. He can also define how long cached pages should be kept.</p>"; $output .= "<p>Drupal's caching mechanism can be enabled and disabled by the site administrators from the 'settings' page. Site administrators can also define how long cached pages should be kept.</p>";
return $output; return $output;
} }
...@@ -59,7 +59,7 @@ function system_view_options() { ...@@ -59,7 +59,7 @@ function system_view_options() {
$output .= "<h3>" . t("Cache settings") . "</h3>\n"; $output .= "<h3>" . t("Cache settings") . "</h3>\n";
$period = array(10 => format_interval(10), 20 => format_interval(20), 30 => format_interval(30), 40 => format_interval(40), 50 => format_interval(50), 50 => format_interval(50), 60 => format_interval(60), 90 => format_interval(90), 120 => format_interval(120), 150 => format_interval(150), 180 => format_interval(180), 210 => format_interval(210), 240 => format_interval(240), 270 => format_interval(270), 300 => format_interval(300), 360 => format_interval(360), 420 => format_interval(420), 480 => format_interval(480), 540 => format_interval(540), 600 => format_interval(600), 1800 => format_interval(1800), 3600 => format_interval(3600), 7200 => format_interval(7200)); $period = array(10 => format_interval(10), 20 => format_interval(20), 30 => format_interval(30), 40 => format_interval(40), 50 => format_interval(50), 50 => format_interval(50), 60 => format_interval(60), 90 => format_interval(90), 120 => format_interval(120), 150 => format_interval(150), 180 => format_interval(180), 210 => format_interval(210), 240 => format_interval(240), 270 => format_interval(270), 300 => format_interval(300), 360 => format_interval(360), 420 => format_interval(420), 480 => format_interval(480), 540 => format_interval(540), 600 => format_interval(600), 1800 => format_interval(1800), 3600 => format_interval(3600), 7200 => format_interval(7200));
$output .= form_select(t("Cache support"), "cache", variable_get("cache", 0), array(t("Disabled"), t("Enabled")), t("Enable or disable the caching of pages.")); $output .= form_select(t("Cache support"), "cache", variable_get("cache", 0), array(t("Disabled"), t("Enabled")), t("Enable or disable the caching of pages."));
$output .= form_select(t("Discard cached pages older than"), "cache_clear", variable_get("cache_clear", 30), $period, t("The time cached pages should be kept. Older pages are automatically refreshed.")); $output .= form_select(t("Discard cached pages older than"), "cache_clear", variable_get("cache_clear", 120), $period, t("The time cached pages should be kept. Older pages are automatically refreshed."));
$output .= "<hr />\n"; $output .= "<hr />\n";
// submission settings: // submission settings:
...@@ -105,12 +105,24 @@ function system_view_filters() { ...@@ -105,12 +105,24 @@ function system_view_filters() {
} }
function system_save($edit = array()) { function system_save($edit = array()) {
foreach ($edit as $name => $value) variable_set($name, $value); foreach ($edit as $name => $value) {
variable_set($name, $value);
}
/*
** Clear the cache so the changes take effect for anonymous users.
*/
cache_clear_all();
return t("The configuration options have been saved."); return t("The configuration options have been saved.");
} }
function system_default($edit = array()) { function system_default($edit = array()) {
foreach ($edit as $name => $value) variable_del($name); foreach ($edit as $name => $value) {
variable_del($name);
}
return t("The configuration options have been reset to their default values."); return t("The configuration options have been reset to their default values.");
} }
...@@ -197,7 +209,7 @@ function system_listing($type, $directory, $required = array()) { ...@@ -197,7 +209,7 @@ function system_listing($type, $directory, $required = array()) {
} }
} }
// Clean up database. // Update the contents of the system table:
db_query("DELETE FROM system WHERE filename = '%s' AND type = '%s'", $filename, $type); db_query("DELETE FROM system WHERE filename = '%s' AND type = '%s'", $filename, $type);
db_query("INSERT INTO system (name, description, type, filename, status) VALUES ('%s', '%s', '%s', '%s', %d)", $info->name, $info->description, $type, $filename, $file->status); db_query("INSERT INTO system (name, description, type, filename, status) VALUES ('%s', '%s', '%s', '%s', %d)", $info->name, $info->description, $type, $filename, $file->status);
......
...@@ -15,7 +15,7 @@ function system_system($field){ ...@@ -15,7 +15,7 @@ function system_system($field){
function system_help_cache() { function system_help_cache() {
$output .= "<p>Drupal has a caching mechanism that stores dynamically generated pages in a database. By caching a page, Drupal does not have to generate the page each time it is requested. Only pages requested by anonymous users are being cached. When a cached page is accessed, Drupal will retrieve that page with minimal overhead using one SQL query only, thus reducing both the server load and the response time.</p>"; $output .= "<p>Drupal has a caching mechanism that stores dynamically generated pages in a database. By caching a page, Drupal does not have to generate the page each time it is requested. Only pages requested by anonymous users are being cached. When a cached page is accessed, Drupal will retrieve that page with minimal overhead using one SQL query only, thus reducing both the server load and the response time.</p>";
$output .= "<p>Drupal's caching mechanism can be enabled and disabled by the site administrators from the 'settings' page. He can also define how long cached pages should be kept.</p>"; $output .= "<p>Drupal's caching mechanism can be enabled and disabled by the site administrators from the 'settings' page. Site administrators can also define how long cached pages should be kept.</p>";
return $output; return $output;
} }
...@@ -59,7 +59,7 @@ function system_view_options() { ...@@ -59,7 +59,7 @@ function system_view_options() {
$output .= "<h3>" . t("Cache settings") . "</h3>\n"; $output .= "<h3>" . t("Cache settings") . "</h3>\n";
$period = array(10 => format_interval(10), 20 => format_interval(20), 30 => format_interval(30), 40 => format_interval(40), 50 => format_interval(50), 50 => format_interval(50), 60 => format_interval(60), 90 => format_interval(90), 120 => format_interval(120), 150 => format_interval(150), 180 => format_interval(180), 210 => format_interval(210), 240 => format_interval(240), 270 => format_interval(270), 300 => format_interval(300), 360 => format_interval(360), 420 => format_interval(420), 480 => format_interval(480), 540 => format_interval(540), 600 => format_interval(600), 1800 => format_interval(1800), 3600 => format_interval(3600), 7200 => format_interval(7200)); $period = array(10 => format_interval(10), 20 => format_interval(20), 30 => format_interval(30), 40 => format_interval(40), 50 => format_interval(50), 50 => format_interval(50), 60 => format_interval(60), 90 => format_interval(90), 120 => format_interval(120), 150 => format_interval(150), 180 => format_interval(180), 210 => format_interval(210), 240 => format_interval(240), 270 => format_interval(270), 300 => format_interval(300), 360 => format_interval(360), 420 => format_interval(420), 480 => format_interval(480), 540 => format_interval(540), 600 => format_interval(600), 1800 => format_interval(1800), 3600 => format_interval(3600), 7200 => format_interval(7200));
$output .= form_select(t("Cache support"), "cache", variable_get("cache", 0), array(t("Disabled"), t("Enabled")), t("Enable or disable the caching of pages.")); $output .= form_select(t("Cache support"), "cache", variable_get("cache", 0), array(t("Disabled"), t("Enabled")), t("Enable or disable the caching of pages."));
$output .= form_select(t("Discard cached pages older than"), "cache_clear", variable_get("cache_clear", 30), $period, t("The time cached pages should be kept. Older pages are automatically refreshed.")); $output .= form_select(t("Discard cached pages older than"), "cache_clear", variable_get("cache_clear", 120), $period, t("The time cached pages should be kept. Older pages are automatically refreshed."));
$output .= "<hr />\n"; $output .= "<hr />\n";
// submission settings: // submission settings:
...@@ -105,12 +105,24 @@ function system_view_filters() { ...@@ -105,12 +105,24 @@ function system_view_filters() {
} }
function system_save($edit = array()) { function system_save($edit = array()) {
foreach ($edit as $name => $value) variable_set($name, $value); foreach ($edit as $name => $value) {
variable_set($name, $value);
}
/*
** Clear the cache so the changes take effect for anonymous users.
*/
cache_clear_all();
return t("The configuration options have been saved."); return t("The configuration options have been saved.");
} }
function system_default($edit = array()) { function system_default($edit = array()) {
foreach ($edit as $name => $value) variable_del($name); foreach ($edit as $name => $value) {
variable_del($name);
}
return t("The configuration options have been reset to their default values."); return t("The configuration options have been reset to their default values.");
} }
...@@ -197,7 +209,7 @@ function system_listing($type, $directory, $required = array()) { ...@@ -197,7 +209,7 @@ function system_listing($type, $directory, $required = array()) {
} }
} }
// Clean up database. // Update the contents of the system table:
db_query("DELETE FROM system WHERE filename = '%s' AND type = '%s'", $filename, $type); db_query("DELETE FROM system WHERE filename = '%s' AND type = '%s'", $filename, $type);
db_query("INSERT INTO system (name, description, type, filename, status) VALUES ('%s', '%s', '%s', '%s', %d)", $info->name, $info->description, $type, $filename, $file->status); db_query("INSERT INTO system (name, description, type, filename, status) VALUES ('%s', '%s', '%s', '%s', %d)", $info->name, $info->description, $type, $filename, $file->status);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment