ccefefa6f7
The app store category cache (cache-categories) held the name and description of every category in every language, about 170 KB, stored as a regular app config value. Nextcloud loads all regular values of all apps on every request, so this one value was read on every page load. It was only refreshed with the "remove cache" button. - Keep only the category ids and names, store the cache as a lazy value so it is only loaded when categories are needed, and refresh it after a week. - Only load the categories where they are used: the menus with categories, and the app list when custom categories are set. - Save all settings in one request to a new admin endpoint, which accepts only known settings, then clears the category cache and changes the stylesheet cache key. The settings page sent one request per setting and reloaded right after; with Nextcloud's short-lived local config cache, the reloaded page could show a half-saved configuration, so a change sometimes had to be made twice.
139 lines
4.1 KiB
PHP
139 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace OCA\SideMenu\Service;
|
|
|
|
use OC\App\AppStore\Fetcher\CategoryFetcher;
|
|
use OCA\SideMenu\AppInfo\Application;
|
|
use OCP\AppFramework\Utility\ITimeFactory;
|
|
use OCP\IAppConfig;
|
|
use OCP\IConfig;
|
|
use OCP\IUserSession;
|
|
use OCP\L10N\IFactory;
|
|
|
|
/**
|
|
* class CategoryRepository.
|
|
*
|
|
* @author Simon Vieille <simon@deblan.fr>
|
|
*/
|
|
class CategoryRepository
|
|
{
|
|
public const CACHE_KEY = 'cache-categories';
|
|
|
|
/**
|
|
* The app store categories rarely change, refresh them once a week.
|
|
*/
|
|
protected const CACHE_TTL = 604800;
|
|
|
|
public function __construct(
|
|
protected CategoryFetcher $categoryFetcher,
|
|
protected ConfigProxy $config,
|
|
protected IConfig $iConfig,
|
|
protected IFactory $l10nFactory,
|
|
protected IUserSession $userSession,
|
|
protected IAppConfig $appConfig,
|
|
protected ITimeFactory $timeFactory,
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* Retrieves categories.
|
|
*/
|
|
public function getOrderedCategories(): array
|
|
{
|
|
$currentLanguage = substr($this->l10nFactory->findLanguage(), 0, 2);
|
|
$type = $this->config->getAppValue('categories-order-type', 'default');
|
|
$order = $this->config->getAppValueArray('categories-order', '[]');
|
|
$categoriesLabels = $this->getCategories();
|
|
$customCategories = $this->config->getAppValueArray('categories-custom', '[]');
|
|
|
|
foreach ($categoriesLabels as $k => $category) {
|
|
$categoriesLabels[$category['id']] = $category['translations'][$currentLanguage]['name'] ??
|
|
$category['translations']['en']['name'];
|
|
|
|
unset($categoriesLabels[$k]);
|
|
}
|
|
|
|
$categoriesLabels['external_links'] = $this->l10nFactory->get('external')->t('External sites');
|
|
$categoriesLabels['other'] = '';
|
|
|
|
$user = $this->userSession->getUser();
|
|
|
|
if ($user) {
|
|
$lang = $this->iConfig->getUserValue($user->getUid(), 'core', 'lang');
|
|
} else {
|
|
$lang = 'en';
|
|
}
|
|
|
|
foreach ($customCategories as $category) {
|
|
$categoriesLabels[$category['id']] = $category[$lang] ?? $category['en'];
|
|
}
|
|
|
|
asort($categoriesLabels);
|
|
|
|
if ('custom' === $type) {
|
|
$ordered = [];
|
|
|
|
foreach ($order as $id) {
|
|
if (isset($categoriesLabels[$id])) {
|
|
$ordered[$id] = $categoriesLabels[$id];
|
|
}
|
|
}
|
|
|
|
foreach ($categoriesLabels as $id => $value) {
|
|
if (!isset($ordered[$id])) {
|
|
$ordered[$id] = $value;
|
|
}
|
|
}
|
|
|
|
$categoriesLabels = $ordered;
|
|
}
|
|
|
|
return $categoriesLabels;
|
|
}
|
|
|
|
public function clearCache(): void
|
|
{
|
|
$this->appConfig->deleteKey(Application::APP_ID, self::CACHE_KEY);
|
|
}
|
|
|
|
/**
|
|
* Returns the app store categories with only their names, from a
|
|
* cache that is stored lazily: it holds the names in every language,
|
|
* so it must not be loaded on every request with the other settings.
|
|
*/
|
|
protected function getCategories(): array
|
|
{
|
|
$cache = $this->appConfig->getValueArray(Application::APP_ID, self::CACHE_KEY, [], lazy: true);
|
|
|
|
if (isset($cache['expires'], $cache['categories']) && $cache['expires'] > $this->timeFactory->getTime()) {
|
|
return $cache['categories'];
|
|
}
|
|
|
|
$categories = [];
|
|
|
|
foreach ($this->categoryFetcher->get() as $category) {
|
|
$names = [];
|
|
|
|
foreach ($category['translations'] ?? [] as $lang => $translation) {
|
|
$names[$lang] = ['name' => $translation['name'] ?? ''];
|
|
}
|
|
|
|
$categories[] = [
|
|
'id' => $category['id'],
|
|
'translations' => $names,
|
|
];
|
|
}
|
|
|
|
// Don't cache an empty list, e.g. when the app store is unreachable
|
|
if (!empty($categories)) {
|
|
$this->clearCache();
|
|
$this->appConfig->setValueArray(Application::APP_ID, self::CACHE_KEY, [
|
|
'expires' => $this->timeFactory->getTime() + self::CACHE_TTL,
|
|
'categories' => $categories,
|
|
], lazy: true);
|
|
}
|
|
|
|
return $categories;
|
|
}
|
|
}
|