fix(config): keep the category cache small and save settings at once
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.
This commit is contained in:
@@ -20,12 +20,14 @@
|
||||
namespace OCA\SideMenu\Controller;
|
||||
|
||||
use OCA\SideMenu\AppInfo\Application;
|
||||
use OCA\SideMenu\Service\CategoryRepository;
|
||||
use OCA\SideMenu\Service\Color;
|
||||
use OCA\SideMenu\Service\ConfigProxy;
|
||||
use OCA\SideMenu\Service\LangRepository;
|
||||
use OCP\AppFramework\Controller;
|
||||
use OCP\AppFramework\Http\Attribute\FrontpageRoute;
|
||||
use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
|
||||
use OCP\AppFramework\Http\Attribute\PasswordConfirmationRequired;
|
||||
use OCP\AppFramework\Http\DataDownloadResponse;
|
||||
use OCP\AppFramework\Http\JSONResponse;
|
||||
use OCP\AppFramework\Http\RedirectResponse;
|
||||
@@ -35,6 +37,12 @@ use OCP\IURLGenerator;
|
||||
|
||||
class AdminSettingController extends Controller
|
||||
{
|
||||
/**
|
||||
* Keys that are managed by the app or by Nextcloud itself ("enabled")
|
||||
* and must not be written from the settings form.
|
||||
*/
|
||||
protected const UNSAVED_KEYS = ['cache', 'cache-categories', 'enabled'];
|
||||
|
||||
public function __construct(
|
||||
$appName,
|
||||
IRequest $request,
|
||||
@@ -43,6 +51,7 @@ class AdminSettingController extends Controller
|
||||
protected IURLGenerator $urlGenerator,
|
||||
protected Color $color,
|
||||
protected LangRepository $langRepository,
|
||||
protected CategoryRepository $categoryRepository,
|
||||
) {
|
||||
parent::__construct($appName, $request);
|
||||
}
|
||||
@@ -51,7 +60,7 @@ class AdminSettingController extends Controller
|
||||
#[FrontpageRoute(verb: 'GET', url: '/admin/cache/remove')]
|
||||
public function removeCache(): RedirectResponse
|
||||
{
|
||||
$this->config->setAppValue(Application::APP_ID, 'cache-categories', '[]');
|
||||
$this->categoryRepository->clearCache();
|
||||
|
||||
return new RedirectResponse($this->urlGenerator->linkToRoute('settings.AdminSettings.index', [
|
||||
'section' => Application::APP_ID,
|
||||
@@ -131,7 +140,77 @@ class AdminSettingController extends Controller
|
||||
'top-menu-mouse-over-hidden-label',
|
||||
];
|
||||
|
||||
$defaults = [
|
||||
$defaults = $this->getDefaults();
|
||||
|
||||
$config = [];
|
||||
|
||||
foreach ($keys as $key) {
|
||||
if (!isset($defaults[$key])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (in_array($key, $booleans)) {
|
||||
$config[$key] = $this->configProxy->getAppValueBool($key, $defaults[$key]);
|
||||
} elseif (in_array($key, $arrays)) {
|
||||
$config[$key] = $this->configProxy->getAppValueArray($key, $defaults[$key]);
|
||||
} elseif (in_array($key, $integers)) {
|
||||
$config[$key] = $this->configProxy->getAppValueInt($key, $defaults[$key]);
|
||||
} else {
|
||||
$config[$key] = $this->configProxy->getAppValue($key, $defaults[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($defaults as $key => $default) {
|
||||
if (array_key_exists($key, $config)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (in_array($key, $booleans)) {
|
||||
$config[$key] = $default === '1';
|
||||
} elseif (in_array($key, $arrays)) {
|
||||
$config[$key] = json_decode($default);
|
||||
} elseif (in_array($key, $integers)) {
|
||||
$config[$key] = (int) $default;
|
||||
} else {
|
||||
$config[$key] = $default;
|
||||
}
|
||||
}
|
||||
|
||||
$config['langs'] = $this->langRepository->getUsedLangs();
|
||||
|
||||
return new JSONResponse($config);
|
||||
}
|
||||
|
||||
#[PasswordConfirmationRequired]
|
||||
#[FrontpageRoute(verb: 'POST', url: '/admin/config')]
|
||||
public function saveConfiguration(array $config): JSONResponse
|
||||
{
|
||||
$allowed = array_diff(array_keys($this->getDefaults()), self::UNSAVED_KEYS);
|
||||
|
||||
foreach ($config as $key => $value) {
|
||||
if (!in_array($key, $allowed, true)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (is_array($value)) {
|
||||
$value = json_encode($value);
|
||||
} elseif (is_bool($value)) {
|
||||
$value = $value ? '1' : '0';
|
||||
}
|
||||
|
||||
$this->config->setAppValue(Application::APP_ID, $key, (string) $value);
|
||||
}
|
||||
|
||||
// New stylesheet URL, and category names that match the new settings
|
||||
$this->config->setAppValue(Application::APP_ID, 'cache', (string) time());
|
||||
$this->categoryRepository->clearCache();
|
||||
|
||||
return new JSONResponse(['status' => 'ok']);
|
||||
}
|
||||
|
||||
protected function getDefaults(): array
|
||||
{
|
||||
return [
|
||||
'opener-only' => '0',
|
||||
'opener-hover' => '0',
|
||||
'display-logo' => '1',
|
||||
@@ -188,43 +267,5 @@ class AdminSettingController extends Controller
|
||||
|
||||
'categories-order-type' => 'default',
|
||||
];
|
||||
|
||||
$config = [];
|
||||
|
||||
foreach ($keys as $key) {
|
||||
if (!isset($defaults[$key])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (in_array($key, $booleans)) {
|
||||
$config[$key] = $this->configProxy->getAppValueBool($key, $defaults[$key]);
|
||||
} elseif (in_array($key, $arrays)) {
|
||||
$config[$key] = $this->configProxy->getAppValueArray($key, $defaults[$key]);
|
||||
} elseif (in_array($key, $integers)) {
|
||||
$config[$key] = $this->configProxy->getAppValueInt($key, $defaults[$key]);
|
||||
} else {
|
||||
$config[$key] = $this->configProxy->getAppValue($key, $defaults[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($defaults as $key => $default) {
|
||||
if (array_key_exists($key, $config)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (in_array($key, $booleans)) {
|
||||
$config[$key] = $default === '1';
|
||||
} elseif (in_array($key, $arrays)) {
|
||||
$config[$key] = json_decode($default);
|
||||
} elseif (in_array($key, $integers)) {
|
||||
$config[$key] = (int) $default;
|
||||
} else {
|
||||
$config[$key] = $default;
|
||||
}
|
||||
}
|
||||
|
||||
$config['langs'] = $this->langRepository->getUsedLangs();
|
||||
|
||||
return new JSONResponse($config);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user