summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDries Buytaert <dries@buytaert.net>2007-04-25 21:34:32 +0000
committerDries Buytaert <dries@buytaert.net>2007-04-25 21:34:32 +0000
commitdbfcd7d13716a5ad10ad454ca2e22339008c1fd2 (patch)
tree5b0f966b6357e8ed830adf8fe21bc06d46536b05
parent9a142acc8dcbf690e146d0c146128d8cf8443b47 (diff)
downloadbrdo-dbfcd7d13716a5ad10ad454ca2e22339008c1fd2.tar.gz
brdo-dbfcd7d13716a5ad10ad454ca2e22339008c1fd2.tar.bz2
- 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.
-rw-r--r--includes/bootstrap.inc4
-rw-r--r--includes/cache.inc23
-rw-r--r--includes/theme.inc4
-rw-r--r--modules/locale/locale.module4
-rw-r--r--modules/system/system.install30
5 files changed, 54 insertions, 11 deletions
diff --git a/includes/bootstrap.inc b/includes/bootstrap.inc
index c4215148f..4d3e8f8e7 100644
--- a/includes/bootstrap.inc
+++ b/includes/bootstrap.inc
@@ -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) {
diff --git a/includes/cache.inc b/includes/cache.inc
index e7774a10b..4bca5fba8 100644
--- a/includes/cache.inc
+++ b/includes/cache.inc
@@ -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();
}
diff --git a/includes/theme.inc b/includes/theme.inc
index f35a64e0e..230bb20da 100644
--- a/includes/theme.inc
+++ b/includes/theme.inc
@@ -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);
}
/**
diff --git a/modules/locale/locale.module b/modules/locale/locale.module
index 5bd955824..a419af94a 100644
--- a/modules/locale/locale.module
+++ b/modules/locale/locale.module
@@ -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);
}
}
diff --git a/modules/system/system.install b/modules/system/system.install
index b5f93887d..f409de7fa 100644
--- a/modules/system/system.install
+++ b/modules/system/system.install
@@ -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)");
@@ -3817,6 +3823,30 @@ function system_update_6011() {
}
/**
+ * 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.
*/