diff options
author | Bharat Mediratta <bharat@menalto.com> | 2009-05-14 03:56:29 +0000 |
---|---|---|
committer | Bharat Mediratta <bharat@menalto.com> | 2009-05-14 03:56:29 +0000 |
commit | 627e83adc13005b2431d49d98c0a2dc05a8e65a6 (patch) | |
tree | d606d36cdb2050c62bf0e3de8f69dac1fea8534d /core | |
parent | aaff4a78863c0103d5c6da86e324e81cc0c2a564 (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.
Diffstat (limited to 'core')
-rw-r--r-- | core/controllers/admin_advanced_settings.php | 16 | ||||
-rw-r--r-- | core/controllers/scaffold.php | 1 | ||||
-rw-r--r-- | core/helpers/module.php | 58 | ||||
-rw-r--r-- | core/views/admin_advanced_settings.html.php | 1 |
4 files changed, 51 insertions, 25 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> |