Skip to content

Commit

Permalink
#6328 Standardized cache code
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasraoni committed Feb 17, 2022
1 parent d8ebe11 commit 90f3703
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 47 deletions.
24 changes: 11 additions & 13 deletions classes/i18n/Locale.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,22 +213,20 @@ public function getMetadata(string $locale): ?LocaleMetadata
*/
public function getLocales(): array
{
return $this->locales ??= Cache::remember(
__METHOD__ . static::MAX_CACHE_LIFETIME . array_reduce(array_keys($this->paths), fn(string $hash, string $path): string => sha1($hash . $path), ''),
DateInterval::createFromDateString(static::MAX_CACHE_LIFETIME),
function () {
$locales = [];
foreach (array_keys($this->paths) as $folder) {
foreach (new DirectoryIterator($folder) as $cursor) {
if ($cursor->isDir() && $this->isLocaleValid($cursor->getBasename())) {
$locales[$cursor->getBasename()] ??= LocaleMetadata::create($cursor->getBasename());
}
$key = __METHOD__ . static::MAX_CACHE_LIFETIME . array_reduce(array_keys($this->paths), fn(string $hash, string $path): string => sha1($hash . $path), '');
$expiration = DateInterval::createFromDateString(static::MAX_CACHE_LIFETIME);
return $this->locales ??= Cache::remember($key, $expiration, function () {
$locales = [];
foreach (array_keys($this->paths) as $folder) {
foreach (new DirectoryIterator($folder) as $cursor) {
if ($cursor->isDir() && $this->isLocaleValid($cursor->getBasename())) {
$locales[$cursor->getBasename()] ??= LocaleMetadata::create($cursor->getBasename());
}
}
ksort($locales);
return $locales;
}
);
ksort($locales);
return $locales;
});
}

/**
Expand Down
14 changes: 3 additions & 11 deletions classes/i18n/translation/IsoCodesTranslationDriver.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ public function configureDirectory(string $isoNumber, string $directory): void
if (file_exists($path)) {
// Check if it's installed before caching the ISO codes (huge dataset), just to avoid a slow installation page
$loader = fn () => Translator::createFromTranslationsArray(LocaleFile::loadArray($path, Application::isInstalled()));
$this->translator = Application::isInstalled()
? Cache::remember($this->_getCacheKey($path), DateInterval::createFromDateString(static::MAX_CACHE_LIFETIME), $loader)
: $loader();
$key = __METHOD__ . static::MAX_CACHE_LIFETIME . $path . filemtime($path);
$expiration = DateInterval::createFromDateString(static::MAX_CACHE_LIFETIME);
$this->translator = Application::isInstalled() ? Cache::remember($key, $expiration, $loader) : $loader();
break;
}
}
Expand All @@ -89,12 +89,4 @@ public function translate(string $isoNumber, string $message): string
{
return ($this->translator ? $this->translator->getSingular($message) : $message) ?: $message;
}

/**
* Retrieves the cache key
*/
private static function _getCacheKey(string $path): string
{
return __METHOD__ . static::MAX_CACHE_LIFETIME . '.' . sha1($path . filemtime($path));
}
}
15 changes: 3 additions & 12 deletions classes/i18n/translation/LocaleBundle.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,18 +119,9 @@ public function getTranslator(): Translator
array_walk($this->paths, fn (int $_, string $path) => $translator->addTranslations(LocaleFile::loadArray($path, $isSupported)));
return $translator;
};
return $this->translator ??= $isSupported
? Cache::remember($this->_getCacheKey(), DateInterval::createFromDateString(static::MAX_CACHE_LIFETIME), $loader)
: $loader();
}

/**
* Retrieves a cache key based on the path and modification date of all locale files
*/
private function _getCacheKey(): string
{
$key = array_reduce(array_keys($this->paths), fn(string $hash, string $path): string => sha1($hash . $path . filemtime($path)), '');
return __METHOD__ . static::MAX_CACHE_LIFETIME . ".${key}";
$key = __METHOD__ . static::MAX_CACHE_LIFETIME . array_reduce(array_keys($this->paths), fn(string $hash, string $path): string => sha1($hash . $path . filemtime($path)), '');
$expiration = DateInterval::createFromDateString(static::MAX_CACHE_LIFETIME);
return $this->translator ??= $isSupported ? Cache::remember($key, $expiration, $loader) : $loader();
}

/**
Expand Down
13 changes: 2 additions & 11 deletions classes/i18n/translation/LocaleFile.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,7 @@ public static function loadTranslations(string $path): Translations
public static function loadArray(string $path, bool $useCache = false): array
{
$loader = fn() => (new ArrayGenerator())->generateArray(static::loadTranslations($path));
return $useCache
? Cache::remember(static::_getCacheKey($path), DateInterval::createFromDateString(static::MAX_CACHE_LIFETIME), $loader)
: $loader();
}

/**
* Retrieves the cache key
*/
private static function _getCacheKey(string $path): string
{
return __METHOD__ . static::MAX_CACHE_LIFETIME . '.' . sha1($path . filemtime($path));
$key = __METHOD__ . static::MAX_CACHE_LIFETIME . $path . filemtime($path);
return $useCache ? Cache::remember($key, DateInterval::createFromDateString(static::MAX_CACHE_LIFETIME), $loader) : $loader();
}
}

0 comments on commit 90f3703

Please sign in to comment.