Commit dbfcd7d1 authored by Dries's avatar Dries
Browse files

- Patch #137415 by slantview: simplified the cache API/usages, and made it a...

- Patch #137415 by slantview: simplified the cache API/usages, and made it a tad smarter.  Makes it easier to program for, and easier to replace.  Will need to be documented.
parent 9a142acc
......@@ -369,14 +369,14 @@ function drupal_get_filename($type, $name, $filename = NULL) {
function variable_init($conf = array()) {
// NOTE: caching the variables improves performance by 20% when serving cached pages.
if ($cached = cache_get('variables', 'cache')) {
$variables = unserialize($cached->data);
$variables = $cached->data;
}
else {
$result = db_query('SELECT * FROM {variable}');
while ($variable = db_fetch_object($result)) {
$variables[$variable->name] = unserialize($variable->value);
}
cache_set('variables', serialize($variables));
cache_set('variables', $variables);
}
foreach ($conf as $name => $value) {
......
......@@ -2,7 +2,8 @@
// $Id$
/**
* Return data from the persistent cache.
* Return 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 $key
* The cache ID of the data to retrieve.
......@@ -21,12 +22,15 @@ function cache_get($key, $table = 'cache') {
variable_set('cache_flush', 0);
}
$cache = db_fetch_object(db_query("SELECT data, created, headers, expire FROM {$table} WHERE cid = '%s'", $key));
$cache = db_fetch_object(db_query("SELECT data, created, headers, expire, serialized FROM {$table} WHERE cid = '%s'", $key));
if (isset($cache->data)) {
// 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)) {
$cache->data = db_decode_blob($cache->data);
if ($cache->serialized) {
$cache->data = unserialize($cache->data);
}
}
// If enforcing a minimum cache lifetime, validate that the data is
// currently valid for this user before we return it by making sure the
......@@ -40,6 +44,9 @@ function cache_get($key, $table = 'cache') {
}
else {
$cache->data = db_decode_blob($cache->data);
if ($cache->serialized) {
$cache->data = unserialize($cache->data);
}
}
}
return $cache;
......@@ -75,7 +82,8 @@ function cache_get($key, $table = 'cache') {
* @param $cid
* The cache ID of the data to store.
* @param $data
* The data to store in the cache. Complex data types must be serialized first.
* 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 $table
* The table $table to store the data in. Valid core values are 'cache_filter',
* 'cache_menu', 'cache_page', or 'cache'.
......@@ -91,10 +99,15 @@ function cache_get($key, $table = 'cache') {
* A string containing HTTP header information for cached pages.
*/
function cache_set($cid, $data, $table = 'cache', $expire = CACHE_PERMANENT, $headers = NULL) {
$serialized = 0;
if (is_object($data) || is_array($data)) {
$data = serialize($data);
$serialized = 1;
}
db_lock_table($table);
db_query("UPDATE {$table} SET data = %b, created = %d, expire = %d, headers = '%s' WHERE cid = '%s'", $data, time(), $expire, $headers, $cid);
db_query("UPDATE {$table} SET data = %b, created = %d, expire = %d, headers = '%s', serialized = %d WHERE cid = '%s'", $data, time(), $expire, $headers, $serialized, $cid);
if (!db_affected_rows()) {
@db_query("INSERT INTO {$table} (cid, data, created, expire, headers) VALUES ('%s', %b, %d, %d, '%s')", $cid, $data, time(), $expire, $headers);
@db_query("INSERT INTO {$table} (cid, data, created, expire, headers, serialized) VALUES ('%s', %b, %d, %d, '%s', %d)", $cid, $data, time(), $expire, $headers, $serialized);
}
db_unlock_tables();
}
......
......@@ -115,7 +115,7 @@ function _theme_set_registry($registry) {
function _theme_load_registry($theme, $theme_engine = NULL) {
$cache = cache_get("theme_registry:$theme", 'cache');
if (isset($cache->data)) {
$registry = unserialize($cache->data);
$registry = $cache->data;
}
else {
$registry = _theme_build_registry($theme, $theme_engine);
......@@ -128,7 +128,7 @@ function _theme_load_registry($theme, $theme_engine = NULL) {
* Write the theme_registry cache into the database.
*/
function _theme_save_registry($theme, $registry) {
cache_set("theme_registry:$theme", serialize($registry));
cache_set("theme_registry:$theme", $registry);
}
/**
......
......@@ -245,7 +245,7 @@ function locale($string) {
locale_refresh_cache();
$cache = cache_get('locale:'. $language->language, 'cache');
}
$locale_t = unserialize($cache->data);
$locale_t = $cache->data;
}
// We have the translation cached (if it is TRUE, then there is no
......@@ -305,7 +305,7 @@ function locale_refresh_cache() {
while ($data = db_fetch_object($result)) {
$t[$data->source] = (empty($data->translation) ? TRUE : $data->translation);
}
cache_set('locale:'. $language->language, serialize($t));
cache_set('locale:'. $language->language, $t);
}
}
......
......@@ -219,6 +219,7 @@ function system_install() {
expire int NOT NULL default '0',
created int NOT NULL default '0',
headers text,
serialized int(1) NOT NULL default '0',
PRIMARY KEY (cid),
INDEX expire (expire)
) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");
......@@ -228,6 +229,7 @@ function system_install() {
expire int NOT NULL default '0',
created int NOT NULL default '0',
headers text,
serialized int(1) NOT NULL default '0',
PRIMARY KEY (cid),
INDEX expire (expire)
) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");
......@@ -237,6 +239,7 @@ function system_install() {
expire int NOT NULL default '0',
created int NOT NULL default '0',
headers text,
serialized int(1) NOT NULL default '0',
PRIMARY KEY (cid),
INDEX expire (expire)
) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");
......@@ -692,6 +695,7 @@ function system_install() {
expire int NOT NULL default '0',
created int NOT NULL default '0',
headers text,
serialized int(1) NOT NULL default '0',
PRIMARY KEY (cid)
)");
db_query("CREATE TABLE {cache_filter} (
......@@ -700,6 +704,7 @@ function system_install() {
expire int NOT NULL default '0',
created int NOT NULL default '0',
headers text,
serialized int(1) NOT NULL default '0',
PRIMARY KEY (cid)
)");
db_query("CREATE TABLE {cache_page} (
......@@ -708,6 +713,7 @@ function system_install() {
expire int NOT NULL default '0',
created int NOT NULL default '0',
headers text,
serialized int(1) NOT NULL default '0',
PRIMARY KEY (cid)
)");
db_query("CREATE INDEX {cache}_expire_idx ON {cache} (expire)");
......@@ -3816,6 +3822,30 @@ function system_update_6011() {
return $ret;
}
/**
* Add serialized field to cache tables
*/
function system_update_6012() {
$ret = array();
switch ($GLOBALS['db_type']) {
case 'pgsql':
db_add_column($ret, 'cache', 'serialized', 'int(1)', array('default' => "'0'", 'not null' => TRUE));
db_add_column($ret, 'cache_filter', 'serialized', 'int(1)', array('default' => "'0'", 'not null' => TRUE));
db_add_column($ret, 'cache_page', 'serialized', 'int(1)', array('default' => "'0'", 'not null' => TRUE));
break;
case 'mysql':
case 'mysqli':
$ret[] = update_sql("ALTER TABLE {cache} ADD serialized int(1) NOT NULL default '0'");
$ret[] = update_sql("ALTER TABLE {cache_filter} ADD serialized int(1) NOT NULL default '0'");
$ret[] = update_sql("ALTER TABLE {cache_page} ADD serialized int(1) NOT NULL default '0'");
break;
}
return $ret;
}
/**
* @} End of "defgroup updates-5.x-to-6.x"
* The next series of updates should start at 7000.
......
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