summaryrefslogtreecommitdiff
path: root/core/helpers
diff options
context:
space:
mode:
authorBharat Mediratta <bharat@menalto.com>2008-12-16 04:29:00 +0000
committerBharat Mediratta <bharat@menalto.com>2008-12-16 04:29:00 +0000
commitd9e02a5d0c1a81925df33a9b25501fc90db91451 (patch)
treea10bc2a56861cd1c1adb2241d8e877ba12fcd2ae /core/helpers
parentdc089173456c95dcec1e6184b8e6b5b2810ced52 (diff)
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!
Diffstat (limited to 'core/helpers')
-rw-r--r--core/helpers/access.php48
-rw-r--r--core/helpers/model_cache.php35
2 files changed, 51 insertions, 32 deletions
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 @@
+<?php defined("SYSPATH") or die("No direct script access.");
+/**
+ * Gallery - a web based photo album viewer and editor
+ * Copyright (C) 2000-2008 Bharat Mediratta
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+class model_cache_Core {
+ private static $cache;
+
+ function get($model_name, $id, $field_name="id") {
+ if (TEST_MODE || empty(self::$cache->$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;
+ }
+}