From d9e02a5d0c1a81925df33a9b25501fc90db91451 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Tue, 16 Dec 2008 04:29:00 +0000 Subject: Various optimizations: o Add model_cache::get() which caches models avoiding duplicate lookups o Stop using ORM relationships for Item_Model::owner so that we can use caching o For Item_Model::xxx_edit fields, don't make them editable for guests o Other minor stuff. These optimizations reduce the number of queries for a 9-photos page from ~200 to ~45. Still way too many! --- core/helpers/access.php | 48 +++++++++++++++----------------------------- core/helpers/model_cache.php | 35 ++++++++++++++++++++++++++++++++ core/libraries/ORM_MPTT.php | 3 +-- core/models/item.php | 7 ++++--- 4 files changed, 56 insertions(+), 37 deletions(-) create mode 100644 core/helpers/model_cache.php (limited to 'core') diff --git a/core/helpers/access.php b/core/helpers/access.php index f40a5c7c..eda91d65 100644 --- a/core/helpers/access.php +++ b/core/helpers/access.php @@ -79,15 +79,8 @@ class access_Core { * @return boolean */ public static function group_can($group, $perm_name, $item) { - if ($perm_name == "view") { - $resource = $item; - } else { - $resource = ORM::factory("access_cache")->where("item_id", $item->id)->find(); - if (!$resource) { - throw new Exception("@todo MISSING_ACCESS for $item->id"); - } - } - + $resource = $perm_name == "view" ? + $item : model_cache::get("access_cache", $item->id, "item_id"); return $resource->__get("{$perm_name}_{$group->id}") === self::ALLOW; } @@ -103,15 +96,8 @@ class access_Core { return false; } - if ($perm_name == "view") { - $resource = $item; - } else { - $resource = ORM::factory("access_cache")->where("item_id", $item->id)->find(); - if (!$resource) { - throw new Exception("@todo MISSING_ACCESS for $item->id"); - } - } - + $resource = $perm_name == "view" ? + $item : model_cache::get("access_cache", $item->id, "item_id"); foreach (user::active()->groups as $group) { if ($resource->__get("{$perm_name}_{$group->id}") === self::ALLOW) { return true; @@ -135,11 +121,7 @@ class access_Core { if ($album->type != "album") { throw new Exception("@todo INVALID_ALBUM_TYPE not an album"); } - $access = ORM::factory("access_intent")->where("item_id", $album->id)->find(); - if (!$access->loaded) { - throw new Exception("@todo MISSING_ACCESS for $album->id"); - } - + $access = model_cache::get("access_intent", $album->id, "item_id"); $access->__set("{$perm_name}_{$group->id}", $value); $access->save(); @@ -268,17 +250,19 @@ class access_Core { $access_intent->save(); // Create a new access cache entry and copy the parents values. - $parent_access_cache = - ORM::factory("access_cache")->where("item_id", $item->parent()->id)->find(); $access_cache = ORM::factory("access_cache"); $access_cache->item_id = $item->id; - foreach (self::_get_all_groups() as $group) { - foreach (ORM::factory("permission")->find_all() as $perm) { - $field = "{$perm->name}_{$group->id}"; - if ($perm->name == "view") { - $item->$field = $item->parent()->$field; - } else { - $access_cache->$field = $parent_access_cache->$field; + if ($item->id != 1) { + $parent_access_cache = + ORM::factory("access_cache")->where("item_id", $item->parent()->id)->find(); + foreach (self::_get_all_groups() as $group) { + foreach (ORM::factory("permission")->find_all() as $perm) { + $field = "{$perm->name}_{$group->id}"; + if ($perm->name == "view") { + $item->$field = $item->parent()->$field; + } else { + $access_cache->$field = $parent_access_cache->$field; + } } } } diff --git a/core/helpers/model_cache.php b/core/helpers/model_cache.php new file mode 100644 index 00000000..7a01393b --- /dev/null +++ b/core/helpers/model_cache.php @@ -0,0 +1,35 @@ +$model_name->$field_name->$id)) { + $model = ORM::factory($model_name)->where($field_name, $id)->find(); + if (!$model->loaded) { + throw new Exception("@todo MISSING_MODEL $model_name:$id"); + } + self::$cache->$model_name->$field_name->$id = $model; + } + + + return self::$cache->$model_name->$field_name->$id; + } +} diff --git a/core/libraries/ORM_MPTT.php b/core/libraries/ORM_MPTT.php index 411afe44..37789156 100644 --- a/core/libraries/ORM_MPTT.php +++ b/core/libraries/ORM_MPTT.php @@ -114,8 +114,7 @@ class ORM_MPTT_Core extends ORM { */ function parent() { if (!isset($this->parent)) { - $this->parent = - ORM::factory($this->model_name)->where("id", $this->parent_id)->find(); + $this->parent = model_cache::get($this->model_name, $this->parent_id); } return $this->parent; } diff --git a/core/models/item.php b/core/models/item.php index 25b1d7f3..47fae0e7 100644 --- a/core/models/item.php +++ b/core/models/item.php @@ -19,7 +19,6 @@ */ class Item_Model extends ORM_MPTT { protected $children = 'items'; - protected $has_one = array("owner" => "user"); var $rules = array(); @@ -224,7 +223,9 @@ class Item_Model extends ORM_MPTT { public function __get($column) { if (substr($column, -5) == "_edit") { $real_column = substr($column, 0, strlen($column) - 5); - if (access::can("edit", $this)) { + $editable = $this->type == "album" ? + access::can("edit", $this) : access::can("edit", $this->parent()); + if ($editable) { return "id}-{$real_column}\">" . "{$this->$real_column}"; } else { @@ -234,7 +235,7 @@ class Item_Model extends ORM_MPTT { // This relationship depends on an outside module, which may not be present so handle // failures gracefully. try { - return parent::__get($column); + return model_cache::get("user", $this->owner_id); } catch (Exception $e) { return null; } -- cgit v1.2.3