fix(config): only cache category names in the languages in use
The cache held the category names in all 96 app store languages (about 49 KB), while an instance typically uses one or two. Keep only English, the default language and the languages users have set, as the two-letter codes the lookup uses; on a Dutch instance this is about 1.3 KB. A user who switches to a language that is not cached yet sees English names until the cache is refreshed after a week, or right away when an admin saves the settings. The README mentions this.
This commit is contained in:
@@ -32,6 +32,7 @@ class CategoryRepository
|
||||
protected IUserSession $userSession,
|
||||
protected IAppConfig $appConfig,
|
||||
protected ITimeFactory $timeFactory,
|
||||
protected LangRepository $langRepository,
|
||||
) {
|
||||
}
|
||||
|
||||
@@ -96,6 +97,25 @@ class CategoryRepository
|
||||
$this->appConfig->deleteKey(Application::APP_ID, self::CACHE_KEY);
|
||||
}
|
||||
|
||||
/**
|
||||
* The languages to keep in the cache: English as fallback, the
|
||||
* default language and the languages of the users, as two-letter
|
||||
* codes like getOrderedCategories() looks them up. A user who picks
|
||||
* a new language sees English names until the cache is refreshed,
|
||||
* which saving the settings does right away.
|
||||
*/
|
||||
protected function getCachedLangs(): array
|
||||
{
|
||||
$langs = $this->langRepository->getUsedLangs();
|
||||
$langs[] = (string) $this->iConfig->getSystemValue('default_language', 'en');
|
||||
$langs[] = (string) $this->iConfig->getSystemValue('force_language', 'en');
|
||||
|
||||
return array_values(array_unique(array_filter(array_map(
|
||||
static fn (string $lang) => strtolower(substr($lang, 0, 2)),
|
||||
array_merge(['en'], $langs)
|
||||
))));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the app store categories with only their names, from a
|
||||
* cache that is stored lazily: it holds the names in every language,
|
||||
@@ -109,13 +129,16 @@ class CategoryRepository
|
||||
return $cache['categories'];
|
||||
}
|
||||
|
||||
$langs = $this->getCachedLangs();
|
||||
$categories = [];
|
||||
|
||||
foreach ($this->categoryFetcher->get() as $category) {
|
||||
$names = [];
|
||||
|
||||
foreach ($category['translations'] ?? [] as $lang => $translation) {
|
||||
$names[$lang] = ['name' => $translation['name'] ?? ''];
|
||||
if (in_array($lang, $langs, true)) {
|
||||
$names[$lang] = ['name' => $translation['name'] ?? ''];
|
||||
}
|
||||
}
|
||||
|
||||
$categories[] = [
|
||||
|
||||
Reference in New Issue
Block a user