summaryrefslogtreecommitdiff
path: root/core/models
diff options
context:
space:
mode:
authorBharat Mediratta <bharat@menalto.com>2009-05-14 02:14:46 +0000
committerBharat Mediratta <bharat@menalto.com>2009-05-14 02:14:46 +0000
commitaaff4a78863c0103d5c6da86e324e81cc0c2a564 (patch)
tree6b954f38d81b6e55bef265e09cca6b389db5e529 /core/models
parente543b055fbcfeca7f25f6c31696d420b035574df (diff)
Introduce a relative_path_cache column in the items table. This lets
us avoid doing lots of MPTT lookups to find the parent path when we're trying to generate thumbnails, etc. Invalidate the cache at all the right times. This greatly reduces our query count on album page views. This fixes ticket #40.
Diffstat (limited to 'core/models')
-rw-r--r--core/models/item.php22
1 files changed, 13 insertions, 9 deletions
diff --git a/core/models/item.php b/core/models/item.php
index 940eeab7..5b989b0c 100644
--- a/core/models/item.php
+++ b/core/models/item.php
@@ -19,7 +19,6 @@
*/
class Item_Model extends ORM_MPTT {
protected $children = 'items';
- private $relative_path = null;
private $view_restrictions = null;
protected $sorting = array();
@@ -137,12 +136,16 @@ class Item_Model extends ORM_MPTT {
$original_thumb_path = $this->thumb_path();
parent::move_to($target, true);
- $this->relative_path = null;
+ $this->relative_path_cache = null;
rename($original_path, $this->file_path());
if ($this->is_album()) {
@rename(dirname($original_resize_path), dirname($this->resize_path()));
@rename(dirname($original_thumb_path), dirname($this->thumb_path()));
+ Database::instance()
+ ->update("items",
+ array("relative_path_cache" => null),
+ array("left >" => $this->left, "right <" => $this->right));
} else {
@rename($original_resize_path, $this->resize_path());
@rename($original_thumb_path, $this->thumb_path());
@@ -250,21 +253,22 @@ class Item_Model extends ORM_MPTT {
* @return string
*/
public function relative_path() {
- if (empty($this->relative_path)) {
+ if (!isset($this->relative_path_cache)) {
$paths = array();
foreach (Database::instance()
->select("name")
->from("items")
- ->where("`left` <= {$this->left}")
- ->where("`right` >= {$this->right}")
- ->where("id <> 1")
+ ->where("left <=", $this->left)
+ ->where("right >=", $this->right)
+ ->where("id <>", 1)
->orderby("left", "ASC")
->get() as $row) {
$paths[] = $row->name;
}
- $this->relative_path = implode($paths, "/");
+ $this->relative_path_cache = implode($paths, "/");
+ $this->save();
}
- return $this->relative_path;
+ return $this->relative_path_cache;
}
/**
@@ -290,7 +294,7 @@ class Item_Model extends ORM_MPTT {
public function __set($column, $value) {
if ($column == "name") {
// Clear the relative path as it is no longer valid.
- $this->relative_path = null;
+ $this->relative_path_cache = null;
}
parent::__set($column, $value);
}