From 6fb39dea77cab9024fc1f94d08c4cb19fbdb030f Mon Sep 17 00:00:00 2001 From: Julian Maurice Date: Thu, 16 Feb 2017 12:44:02 +0100 Subject: [PATCH] Cache collections children This can greatly reduce the page load time when dealing with a large set of collections. Example on collections/edit with 2000+ collections in database: Without this patch: ~950ms With this patch: ~350ms --- models/Table/CollectionTree.php | 38 +++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/models/Table/CollectionTree.php b/models/Table/CollectionTree.php index f52624f..e7ec0db 100644 --- a/models/Table/CollectionTree.php +++ b/models/Table/CollectionTree.php @@ -21,6 +21,14 @@ class Table_CollectionTree extends Omeka_Db_Table */ protected $_collections; + /** + * Cache of children of a collection. + * + * It's an associative array where keys are collection ids and values are + * arrays of children ids. + */ + protected $_collectionsChildren; + /** * Cache of variables needed for some use. * @@ -271,12 +279,14 @@ public function getCollection($collectionId) public function getChildCollections($collectionId) { $childCollections = array(); - $collections = $this->_getCollections(); - foreach ($collections as $collection) { - if ($collectionId == $collection['parent_collection_id']) { - $childCollections[$collection['id']] = $collection; + $collectionsChildren = $this->_getCollectionsChildren(); + + if (isset($collectionsChildren[$collectionId])) { + foreach ($collectionsChildren[$collectionId] as $childId) { + $childCollections[$childId] = $this->getCollection($childId); } } + return $childCollections; } @@ -365,6 +375,26 @@ protected function _getCollections() return $this->_collections; } + /** + * Cache collections children data in an associative array. + */ + protected function _getCollectionsChildren() + { + if (is_null($this->_collectionsChildren)) { + $collections = $this->_getCollections(); + + $this->_collectionsChildren = array(); + foreach ($collections as $id => $collection) { + if ($collection['parent_collection_id']) { + $parent_collection_id = $collection['parent_collection_id']; + $this->_collectionsChildren[$parent_collection_id][] = $id; + } + } + } + + return $this->_collectionsChildren; + } + /** * Reset the cache property. */