Skip to content

Commit

Permalink
Added caching for UrlService::getUrlByPageKey
Browse files Browse the repository at this point in the history
  • Loading branch information
krazzer committed Feb 22, 2023
1 parent 62ad397 commit e6021c5
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/Config/CacheConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class CacheConfig
const PAGE_LANGUAGE_FOR_KEY = 'pageLanguageForKey';
const OTHER_LANG_MAP = 'otherLangMap';
const URL = 'url';
const URL_FOR_KEY = 'urlForKey';
const MENU = 'menu';
const MENU_PAGES = 'menuPages';
const EXISTING_PAGE_CACHE = 'existingPageCache';
Expand Down
1 change: 1 addition & 0 deletions src/Services/CacheService.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public function clearPageCache()
$this->clear(CacheConfig::MENU_PAGES);
$this->clear(CacheConfig::PAGE_LANGUAGE_FOR_URL);
$this->clear(CacheConfig::PAGE_LANGUAGE_FOR_KEY);
$this->clear(CacheConfig::URL_FOR_KEY);

$this->existingPageCacheService->clear();
}
Expand Down
14 changes: 11 additions & 3 deletions src/Services/Pages/UrlService.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,19 @@ public function getUrlByPageId(int $pageId, string $languageCode = null): string
*/
public function getUrlByPageKey(string $pageKey, string $languageCode = null): string
{
if ( ! $page = $this->pageService->getByKey($pageKey)) {
return '';
$cacheKey = CacheConfig::URL_FOR_KEY . CacheConfig::SEPARATOR . $pageKey;

if($languageCode){
$cacheKey .= CacheConfig::SEPARATOR . $languageCode;
}

return $this->getUrlByPageId($page->getId(), $languageCode);
return $this->cacheService->cache($cacheKey, function () use ($pageKey, $languageCode) {
if ( ! $page = $this->pageService->getByKey($pageKey)) {
return '';
}

return $this->getUrlByPageId($page->getId(), $languageCode);
});
}

/**
Expand Down

0 comments on commit e6021c5

Please sign in to comment.