Integrated Marco's generic/improved cache into Drupal. Requires an
SQL update. See below for more details. - Merged the file "cache.inc" into "common.inc". - In addition, I renamed the field 'url' in the cache table to a more generic 'cid' (cache identifier). It's no longer for URLs only. - Made the "cache_set()" function ASNI compliant such that it will play nice with other databases such as Postgres. - Added some extra input checking. - Updated the old caching code in the functions "page_header()" and "page_footer()" to use the new, generic cache API. - Updated "update.php" to make the required SQL changes.4.0.x
parent
f9abcda22f
commit
58152c45d1
|
@ -374,33 +374,51 @@ function format_size($size) {
|
|||
return "$size $suffix";
|
||||
}
|
||||
|
||||
function cache_clear($interval = 0) {
|
||||
db_query("DELETE FROM cache WHERE ". time() ." - timestamp > $interval");
|
||||
function cache_get($key) {
|
||||
$cache = db_fetch_object(db_query("SELECT data FROM cache WHERE cid = '". check_query($key) ."'"));
|
||||
return $cache->data ? $cache->data : 0;
|
||||
}
|
||||
|
||||
function cache_get() {
|
||||
function cache_set($cid, $data, $expire = 0) {
|
||||
if (db_fetch_object(db_query("SELECT cid FROM cache WHERE cid = '". check_query($cid) ."'"))) {
|
||||
db_query("UPDATE cache SET data = '". check_query($data) ."' WHERE cic = '". check_query($cid) ."'");
|
||||
}
|
||||
else {
|
||||
db_query("INSERT INTO cache (cid, data, expire) VALUES('". check_query($cid) ."', '". check_query($data) ."', '". check_query($expire) ."')");
|
||||
}
|
||||
}
|
||||
|
||||
function cache_del($cid) {
|
||||
db_query("DELETE FROM cache WHERE cid = '". check_query($cid) ."'");
|
||||
}
|
||||
|
||||
function cache_clear() {
|
||||
db_query("DELETE FROM cache WHERE expire < ". time() ." AND expire > 0");
|
||||
}
|
||||
|
||||
function page_set_cache() {
|
||||
global $user, $REQUEST_URI, $REQUEST_METHOD;
|
||||
|
||||
if (!$user->uid && $REQUEST_METHOD == "GET") {
|
||||
if ($cache = db_fetch_object(db_query("SELECT * FROM cache WHERE url = '". check_input($REQUEST_URI) ."'"))) {
|
||||
cache_clear(variable_get("cache_clear", 30));
|
||||
if ($data = ob_get_contents()) {
|
||||
cache_set($REQUEST_URI, $data, (time() + variable_get("cache_clear", 30)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function page_get_cache() {
|
||||
global $user, $REQUEST_URI, $REQUEST_METHOD;
|
||||
|
||||
if (!$user->uid && $REQUEST_METHOD == "GET") {
|
||||
if ($cache = cache_get($REQUEST_URI)) {
|
||||
cache_clear();
|
||||
}
|
||||
else {
|
||||
ob_start();
|
||||
}
|
||||
}
|
||||
|
||||
return $cache->data ? $cache->data : 0;
|
||||
}
|
||||
|
||||
function cache_set() {
|
||||
global $user, $REQUEST_URI, $REQUEST_METHOD;
|
||||
|
||||
if (!$user->uid && $REQUEST_METHOD == "GET") {
|
||||
if ($data = ob_get_contents()) {
|
||||
db_query("INSERT INTO cache (url, data, timestamp) VALUES('". addslashes($REQUEST_URI) ."', '". addslashes($data) ."', '". time() ."')");
|
||||
}
|
||||
}
|
||||
return $cache ? $cache : 0;
|
||||
}
|
||||
|
||||
function format_interval($timestamp) {
|
||||
|
@ -595,7 +613,7 @@ function page_header() {
|
|||
}
|
||||
|
||||
if (variable_get("cache", 0)) {
|
||||
if ($data = cache_get()) {
|
||||
if ($data = page_get_cache()) {
|
||||
print $data;
|
||||
exit();
|
||||
}
|
||||
|
@ -612,7 +630,7 @@ function page_footer() {
|
|||
}
|
||||
|
||||
if (variable_get("cache", 0)) {
|
||||
cache_set();
|
||||
page_set_cache();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
11
update.php
11
update.php
|
@ -42,6 +42,7 @@ $mysql_updates = array(
|
|||
"2001-12-24" => "update_15",
|
||||
"2001-12-30" => "update_16",
|
||||
"2001-12-31" => "update_17",
|
||||
"2002-01-05" => "update_18",
|
||||
);
|
||||
|
||||
// Update functions
|
||||
|
@ -306,7 +307,15 @@ function update_17() {
|
|||
);");
|
||||
}
|
||||
|
||||
// System functions
|
||||
function update_18() {
|
||||
update_sql("ALTER TABLE cache CHANGE timestamp expire int(11) DEFAULT '0' NOT NULL;");
|
||||
update_sql("ALTER TABLE cache CHANGE url cid varchar(255) DEFAULT '' NOT NULL;");
|
||||
}
|
||||
|
||||
/*
|
||||
** System functions
|
||||
*/
|
||||
|
||||
function update_sql($sql) {
|
||||
global $edit;
|
||||
print nl2br(check_output($sql)) ." ";
|
||||
|
|
Loading…
Reference in New Issue