summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBharat Mediratta <bharat@menalto.com>2009-05-14 03:56:29 +0000
committerBharat Mediratta <bharat@menalto.com>2009-05-14 03:56:29 +0000
commit627e83adc13005b2431d49d98c0a2dc05a8e65a6 (patch)
treed606d36cdb2050c62bf0e3de8f69dac1fea8534d
parentaaff4a78863c0103d5c6da86e324e81cc0c2a564 (diff)
Cache variables in core._cache so that we can retrieve them all in a
single query. In most cases, we were fetching 4-5 variables per page load, so this is 2-3x faster.
-rw-r--r--core/controllers/admin_advanced_settings.php16
-rw-r--r--core/controllers/scaffold.php1
-rw-r--r--core/helpers/module.php58
-rw-r--r--core/views/admin_advanced_settings.html.php1
-rw-r--r--installer/install.sql4
5 files changed, 53 insertions, 27 deletions
diff --git a/core/controllers/admin_advanced_settings.php b/core/controllers/admin_advanced_settings.php
index 1797b162..e8e608df 100644
--- a/core/controllers/admin_advanced_settings.php
+++ b/core/controllers/admin_advanced_settings.php
@@ -21,7 +21,9 @@ class Admin_Advanced_Settings_Controller extends Admin_Controller {
public function index() {
$view = new Admin_View("admin.html");
$view->content = new View("admin_advanced_settings.html");
- $view->content->vars = ORM::factory("var")->orderby("module_name", "name")->find_all();
+ $view->content->vars = ORM::factory("var")
+ ->orderby("module_name", "name")
+ ->find_all();
print $view;
}
@@ -41,17 +43,7 @@ class Admin_Advanced_Settings_Controller extends Admin_Controller {
public function save($module_name, $var_name) {
access::verify_csrf();
- $var = ORM::factory("var")
- ->where("module_name", $module_name)
- ->where("name", $var_name)
- ->find();
- if (!$var->loaded) {
- kohana::show_404();
- }
-
- $var->value = Input::instance()->post("value");
- $var->save();
-
+ module::set_var($module_name, $var_name, Input::instance()->post("value"));
message::success(
t("Saved value for %var (%module_name)",
array("var" => $var->name, "module_name" => $var->module_name)));
diff --git a/core/controllers/scaffold.php b/core/controllers/scaffold.php
index 14a5fa57..9d42cbb8 100644
--- a/core/controllers/scaffold.php
+++ b/core/controllers/scaffold.php
@@ -317,6 +317,7 @@ class Scaffold_Controller extends Template_Controller {
$db = Database::instance();
$db->query("TRUNCATE {sessions}");
$db->query("TRUNCATE {logs}");
+ $db->query("DELETE FROM {vars} WHERE `module_name` = 'core' AND `name` = '_cache'");
$db->update("users", array("password" => ""), array("id" => 1));
$db->update("users", array("password" => ""), array("id" => 2));
diff --git a/core/helpers/module.php b/core/helpers/module.php
index a4470798..2b1921de 100644
--- a/core/helpers/module.php
+++ b/core/helpers/module.php
@@ -26,7 +26,7 @@
class module_Core {
public static $module_names = array();
public static $modules = array();
- public static $var_cache = array();
+ public static $var_cache = null;
static function get_version($module_name) {
return ORM::factory("module")->where("name", $module_name)->find()->version;
@@ -199,17 +199,43 @@ class module_Core {
* @return the value
*/
static function get_var($module_name, $name, $default_value=null) {
- if (isset(self::$var_cache[$module_name][$name])) {
- return self::$var_cache[$module_name][$name];
+ // We cache all vars in core._cache so that we can load all vars at once for
+ // performance.
+ if (empty(self::$var_cache)) {
+ $row = Database::instance()
+ ->select("value")
+ ->from("vars")
+ ->where(array("module_name" => "core", "name" => "_cache"))
+ ->get()
+ ->current();
+ if ($row) {
+ self::$var_cache = unserialize($row->value);
+ } else {
+ // core._cache doesn't exist. Create it now.
+ foreach (Database::instance()
+ ->select("module_name", "name", "value")
+ ->from("vars")
+ ->orderby("module_name", "name")
+ ->get() as $row) {
+ if ($row->module_name == "core" && $row->name == "_cache") {
+ // This could happen if there's a race condition
+ continue;
+ }
+ self::$var_cache->{$row->module_name}->{$row->name} = $row->value;
+ }
+ $cache = ORM::factory("var");
+ $cache->module_name = "core";
+ $cache->name = "_cache";
+ $cache->value = serialize(self::$var_cache);
+ $cache->save();
+ }
}
- $var = ORM::factory("var")
- ->where("module_name", $module_name)
- ->where("name", $name)
- ->find();
-
- self::$var_cache[$module_name][$name] = $var->loaded ? $var->value : $default_value;
- return self::$var_cache[$module_name][$name];
+ if (isset(self::$var_cache->$module_name->$name)) {
+ return self::$var_cache->$module_name->$name;
+ } else {
+ return $default_value;
+ }
}
/**
@@ -230,7 +256,9 @@ class module_Core {
}
$var->value = $value;
$var->save();
- self::$var_cache[$module_name][$name] = $value;
+
+ Database::instance()->delete("vars", array("module_name" => "core", "name" => "_cache"));
+ self::$var_cache = null;
}
/**
@@ -244,7 +272,9 @@ class module_Core {
"UPDATE {vars} SET `value` = `value` + $increment " .
"WHERE `module_name` = '$module_name' " .
"AND `name` = '$name'");
- unset(self::$var_cache[$module_name][$name]);
+
+ Database::instance()->delete("vars", array("module_name" => "core", "name" => "_cache"));
+ self::$var_cache = null;
}
/**
@@ -260,6 +290,8 @@ class module_Core {
if ($var->loaded) {
$var->delete();
}
- unset(self::$var_cache[$module_name][$name]);
+
+ Database::instance()->delete("vars", array("module_name" => "core", "name" => "_cache"));
+ self::$var_cache = null;
}
}
diff --git a/core/views/admin_advanced_settings.html.php b/core/views/admin_advanced_settings.html.php
index 1995c30c..1f3825bd 100644
--- a/core/views/admin_advanced_settings.html.php
+++ b/core/views/admin_advanced_settings.html.php
@@ -17,6 +17,7 @@
<th> <?= t("Value") ?></th>
</tr>
<? foreach ($vars as $var): ?>
+ <? if ($var->module_name == "core" && $var->name == "_cache") continue ?>
<tr class="setting">
<td> <?= $var->module_name ?> </td>
<td> <?= $var->name ?> </td>
diff --git a/installer/install.sql b/installer/install.sql
index 2891df0f..8f559f02 100644
--- a/installer/install.sql
+++ b/installer/install.sql
@@ -154,7 +154,7 @@ CREATE TABLE {items} (
KEY `random` (`rand_key`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
SET character_set_client = @saved_cs_client;
-INSERT INTO {items} VALUES (NULL,NULL,UNIX_TIMESTAMP(),'',NULL,1,1,1,NULL,NULL,NULL,0,NULL,NULL,1,2,NULL,NULL,1,'Gallery','album',UNIX_TIMESTAMP(),0,NULL,NULL,NULL,'weight','ASC',1,1,1);
+INSERT INTO {items} VALUES (NULL,NULL,UNIX_TIMESTAMP(),'',NULL,1,1,1,NULL,NULL,NULL,0,NULL,NULL,1,2,NULL,NULL,1,'Gallery','album',1242272949,0,NULL,NULL,'','weight','ASC',1,1,1);
DROP TABLE IF EXISTS {items_tags};
SET @saved_cs_client = @@character_set_client;
SET character_set_client = utf8;
@@ -333,4 +333,4 @@ CREATE TABLE {vars} (
UNIQUE KEY `module_name` (`module_name`,`name`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8;
SET character_set_client = @saved_cs_client;
-INSERT INTO {vars} VALUES (1,'core','active_site_theme','default'),(2,'core','active_admin_theme','admin_default'),(3,'core','page_size','9'),(4,'core','thumb_size','200'),(5,'core','resize_size','640'),(6,'core','default_locale','en_US'),(7,'core','blocks_dashboard_sidebar','a:4:{i:1229952929;a:2:{i:0;s:4:\"core\";i:1;s:11:\"block_adder\";}i:837192806;a:2:{i:0;s:4:\"core\";i:1;s:5:\"stats\";}i:1511984720;a:2:{i:0;s:4:\"core\";i:1;s:13:\"platform_info\";}i:784311393;a:2:{i:0;s:4:\"core\";i:1;s:12:\"project_news\";}}'),(8,'core','blocks_dashboard_center','a:4:{i:1049359538;a:2:{i:0;s:4:\"core\";i:1;s:7:\"welcome\";}i:84529933;a:2:{i:0;s:4:\"core\";i:1;s:12:\"photo_stream\";}i:1690694961;a:2:{i:0;s:4:\"core\";i:1;s:11:\"log_entries\";}i:728059697;a:2:{i:0;s:7:\"comment\";i:1;s:15:\"recent_comments\";}}'),(9,'core','version','3.0 pre-beta svn'),(10,'core','choose_default_tookit','1'),(11,'comment','spam_caught','0');
+INSERT INTO {vars} VALUES (1,'core','active_site_theme','default'),(2,'core','active_admin_theme','admin_default'),(3,'core','page_size','9'),(4,'core','thumb_size','200'),(5,'core','resize_size','640'),(6,'core','default_locale','en_US'),(7,'core','blocks_dashboard_sidebar','a:4:{i:1385617552;a:2:{i:0;s:4:\"core\";i:1;s:11:\"block_adder\";}i:3304927;a:2:{i:0;s:4:\"core\";i:1;s:5:\"stats\";}i:476152634;a:2:{i:0;s:4:\"core\";i:1;s:13:\"platform_info\";}i:1783274837;a:2:{i:0;s:4:\"core\";i:1;s:12:\"project_news\";}}'),(8,'core','blocks_dashboard_center','a:4:{i:779406394;a:2:{i:0;s:4:\"core\";i:1;s:7:\"welcome\";}i:255875268;a:2:{i:0;s:4:\"core\";i:1;s:12:\"photo_stream\";}i:2067894253;a:2:{i:0;s:4:\"core\";i:1;s:11:\"log_entries\";}i:507552858;a:2:{i:0;s:7:\"comment\";i:1;s:15:\"recent_comments\";}}'),(9,'core','version','3.0 pre-beta svn'),(10,'core','choose_default_tookit','1'),(11,'comment','spam_caught','0');