From 267d16fe0bf60b85a44390ff99b57bca67c2d616 Mon Sep 17 00:00:00 2001 From: emkael Date: Sat, 7 May 2016 20:33:10 +0200 Subject: * caching Calendar records by custom URL --- app/php/facades/CalendarFacade.php | 39 +++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) (limited to 'app/php/facades') diff --git a/app/php/facades/CalendarFacade.php b/app/php/facades/CalendarFacade.php index 232acf3..b15eac3 100644 --- a/app/php/facades/CalendarFacade.php +++ b/app/php/facades/CalendarFacade.php @@ -135,7 +135,11 @@ class CalendarFacade extends Facade { } public function getAll() { - return Calendar::finder()->withCategory()->findAll('ORDER BY name ASC'); + $records = Calendar::finder()->withCategory()->findAll('ORDER BY name ASC'); + foreach ($records as $record) { + $this->_fillUrlCache($record); + } + return $records; } public function getCategories() { @@ -143,15 +147,36 @@ class CalendarFacade extends Facade { } public function get($uid) { - return Calendar::finder()->withCategory()->findAllByPks($uid); + $records = Calendar::finder()->withCategory()->findAllByPks($uid); + foreach ($records as $record) { + $this->_fillUrlCache($record); + } + return $records; + } + + private $_urlCache = []; + private function _fillUrlCache($record) { + if ($record && $record->CustomUrl + && !isset($this->_urlCache[$record->CustomUrl])) { + $dto = new CalendarDTO(); + if ($record) { + $dto->loadRecord($record); + } else { + $dto = NULL; + } + return $this->_urlCache[$record->CustomUrl] = $dto; + } } public function resolveUrl($url) { - $dto = new CalendarDTO(); - $record = Calendar::finder()->findByCustomUrl($url); - if ($record) { - $dto->loadRecord($record); - return $dto; + if ($url) { + if (isset($this->_urlCache[$url])) { + return $this->_urlCache[$url]; + } + $record = Calendar::finder()->findByCustomUrl($url); + if ($record) { + return $this->_fillUrlCache($record); + } } return NULL; } -- cgit v1.2.3