Compare commits

...

5 Commits

Author SHA1 Message Date
dillard 2c3e6a245b fix(config): clear the old category cache on upgrade
The old cache is only replaced when the categories are requested, so
on instances that use a menu without categories the 170 KB app config
value stayed and was still loaded on every request. A post-migration
repair step clears it when the app is upgraded.
2026-09-30 13:32:40 +02:00
dillard 80f6dcf73a fix(config): refresh a category cache stored with another type
If cache-categories was stored with a type other than mixed or array,
for example set by hand with occ, the typed read throws. Treat that as
a missing cache so it is refreshed instead of breaking the menu and the
settings page.
2026-09-30 13:30:01 +02:00
dillard 73dea84d89 fix(config): keep the category cache in the memory cache when available
A cache belongs in Nextcloud's memory cache rather than in the app
config. Use the distributed cache (e.g. Redis) with a one-week TTL when
one is available; Nextcloud falls back to the local cache (APCu) when
only that is configured. Without any memory cache the lazy app config
value stays as the fallback. A leftover app config value is removed
when the categories are stored in the memory cache, and saving the
settings clears both.
2026-09-30 13:26:45 +02:00
dillard 22e87e12ad 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.
2026-09-30 13:17:37 +02:00
dillard ccefefa6f7 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.
2026-09-30 12:01:50 +02:00
9 changed files with 292 additions and 89 deletions
+6
View File
@@ -1,5 +1,11 @@
## [Unreleased]
### Fixed
- fix(config): store the app store category cache lazily with only the names in the languages in use, refresh it weekly and when the settings are saved, and load it only when categories are used
- fix(admin): save all settings in one request so changes are not lost after the page reloads
- fix(config): remove the old category cache from the app config on upgrade
## 6.2.0
### Added
+2
View File
@@ -49,6 +49,8 @@ Users can disable the menu using the page of personal settings.
Use the shortcut `Ctrl`+`o` to open and to hide the side menu. Use `tab` to navigate.
The names of the app store categories are cached for a week, only in the languages that are in use on the instance (and English). If a user switches to a new language and sees English category names, save the settings on the administration page to refresh the cache right away.
### Use first top menu app as default app
You can easily let Custom Menu redirect to the first app in the top menu by changing the following parameter in your `config/config.php`:
+5
View File
@@ -55,6 +55,11 @@ In case of downtime, you can download **Custom Menu** from [here](https://kim.de
<php min-version="8.1" max-version="8.5" />
<nextcloud min-version="34" max-version="35"/>
</dependencies>
<repair-steps>
<post-migration>
<step>OCA\SideMenu\Migration\ClearCategoryCache</step>
</post-migration>
</repair-steps>
<settings>
<admin>OCA\SideMenu\Settings\Admin</admin>
<admin-section>OCA\SideMenu\Settings\AdminSection</admin-section>
+9 -1
View File
@@ -12,12 +12,16 @@ use OCA\SideMenu\Service\AppRepository;
use OCA\SideMenu\Service\CategoryRepository;
use OCA\SideMenu\Service\Color;
use OCA\SideMenu\Service\ConfigProxy;
use OCA\SideMenu\Service\LangRepository;
use OCA\Theming\ThemingDefaults;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IAppConfig;
use OCP\ICacheFactory;
use OCP\IConfig;
use OCP\INavigationManager;
use OCP\IRequest;
@@ -56,7 +60,11 @@ class Application extends App implements IBootstrap
$c->get(ConfigProxy::class),
$c->get(IConfig::class),
$c->get(IFactory::class),
$c->get(IUserSession::class)
$c->get(IUserSession::class),
$c->get(IAppConfig::class),
$c->get(ITimeFactory::class),
$c->get(LangRepository::class),
$c->get(ICacheFactory::class)
);
});
+81 -40
View File
@@ -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);
}
}
+46
View File
@@ -0,0 +1,46 @@
<?php
/**
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace OCA\SideMenu\Migration;
use OCA\SideMenu\Service\CategoryRepository;
use OCP\Migration\IOutput;
use OCP\Migration\IRepairStep;
/**
* Earlier versions stored the category cache as a regular app config
* value, which Nextcloud loads on every request. Clear it on upgrade so
* it does not linger on instances that never request the categories.
*/
class ClearCategoryCache implements IRepairStep
{
public function __construct(protected CategoryRepository $categoryRepository)
{
}
public function getName(): string
{
return 'Clear the category cache';
}
public function run(IOutput $output): void
{
$this->categoryRepository->clearCache();
}
}
+2 -1
View File
@@ -40,7 +40,8 @@ class AppRepository
{
$navigation = $this->navigationManager->getAll();
$appCategoriesCustom = $this->config->getAppValueArray('apps-categories-custom', '[]');
$categories = $this->categoryRepository->getOrderedCategories();
// Only needed to check custom categories, skip loading them otherwise
$categories = empty(array_filter($appCategoriesCustom)) ? [] : $this->categoryRepository->getOrderedCategories();
$apps = $this->ocApp->listAllApps();
$visibleApps = [];
+122 -7
View File
@@ -4,6 +4,11 @@ namespace OCA\SideMenu\Service;
use OC\App\AppStore\Fetcher\CategoryFetcher;
use OCA\SideMenu\AppInfo\Application;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Exceptions\AppConfigTypeConflictException;
use OCP\IAppConfig;
use OCP\ICache;
use OCP\ICacheFactory;
use OCP\IConfig;
use OCP\IUserSession;
use OCP\L10N\IFactory;
@@ -15,12 +20,23 @@ use OCP\L10N\IFactory;
*/
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,
protected LangRepository $langRepository,
protected ICacheFactory $cacheFactory,
) {
}
@@ -32,15 +48,9 @@ class CategoryRepository
$currentLanguage = substr($this->l10nFactory->findLanguage(), 0, 2);
$type = $this->config->getAppValue('categories-order-type', 'default');
$order = $this->config->getAppValueArray('categories-order', '[]');
$categoriesLabels = $this->config->getAppValueArray('cache-categories', '[]');
$categoriesLabels = $this->getCategories();
$customCategories = $this->config->getAppValueArray('categories-custom', '[]');
if (empty($categoriesLabels)) {
$categoriesLabels = $this->categoryFetcher->get();
$this->iConfig->setAppValue(Application::APP_ID, 'cache-categories', json_encode($categoriesLabels));
}
foreach ($categoriesLabels as $k => $category) {
$categoriesLabels[$category['id']] = $category['translations'][$currentLanguage]['name'] ??
$category['translations']['en']['name'];
@@ -85,4 +95,109 @@ class CategoryRepository
return $categoriesLabels;
}
public function clearCache(): void
{
$this->getDistributedCache()?->remove(self::CACHE_KEY);
$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. They are
* cached in the distributed cache (e.g. Redis) when one is configured,
* otherwise in a lazy app config value, so they are not loaded on
* every request with the other settings.
*/
protected function getCategories(): array
{
$cache = $this->getDistributedCache();
if ($cache !== null) {
$categories = $cache->get(self::CACHE_KEY);
if (is_array($categories)) {
return $categories;
}
} else {
try {
$stored = $this->appConfig->getValueArray(Application::APP_ID, self::CACHE_KEY, [], lazy: true);
} catch (AppConfigTypeConflictException) {
// Stored with another type, e.g. set by hand; refreshed below
$stored = [];
}
if (isset($stored['expires'], $stored['categories']) && $stored['expires'] > $this->timeFactory->getTime()) {
return $stored['categories'];
}
}
$categories = $this->fetchCategories();
// Don't cache an empty list, e.g. when the app store is unreachable
if (empty($categories)) {
return $categories;
}
$this->clearCache();
if ($cache !== null) {
$cache->set(self::CACHE_KEY, $categories, self::CACHE_TTL);
} else {
$this->appConfig->setValueArray(Application::APP_ID, self::CACHE_KEY, [
'expires' => $this->timeFactory->getTime() + self::CACHE_TTL,
'categories' => $categories,
], lazy: true);
}
return $categories;
}
protected function fetchCategories(): array
{
$langs = $this->getCachedLangs();
$categories = [];
foreach ($this->categoryFetcher->get() as $category) {
$names = [];
foreach ($category['translations'] ?? [] as $lang => $translation) {
if (in_array($lang, $langs, true)) {
$names[$lang] = ['name' => $translation['name'] ?? ''];
}
}
$categories[] = [
'id' => $category['id'],
'translations' => $names,
];
}
return $categories;
}
protected function getDistributedCache(): ?ICache
{
return $this->cacheFactory->isAvailable()
? $this->cacheFactory->createDistributed(Application::APP_ID)
: null;
}
}
+18 -39
View File
@@ -20,6 +20,9 @@
</template>
<script setup>
import axios from '@nextcloud/axios'
import { translate as t } from '@nextcloud/l10n'
import { generateUrl } from '@nextcloud/router'
import { NcButton, NcLoadingIcon } from '@nextcloud/vue'
import { ref } from 'vue'
import { waitPasswordConfirmation } from '../../lib/setting.js'
@@ -46,52 +49,28 @@ const filterConfig = (value) => {
}
const save = async () => {
const data = filterConfig(config)
const size = Object.keys(data).length
let counter = 0
loading.value = true
error.value = null
const update = () => {
++counter
if (counter === size) {
try {
await waitPasswordConfirmation()
} catch {
loading.value = false
return
}
if (!error.value) {
try {
// One request for all settings: separate requests per setting could
// leave a half-saved configuration in Nextcloud's config cache when
// the page reloads.
await axios.post(generateUrl('/apps/side_menu/admin/config'), {
config: filterConfig(config),
})
location.reload()
}
}
}
waitPasswordConfirmation()
.then(() => {
for (let key in data) {
let value = data[key]
if (Array.isArray(value) || typeof value === 'object') {
value = JSON.stringify(value)
} else if (typeof value === 'boolean') {
value = value ? '1' : '0'
}
OCP.AppConfig.setValue('side_menu', key, value.toString(), {
success() {
update()
},
error() {
error.value = `Error while saving ${key}`
update()
},
})
}
})
.catch(() => {
counter = 0
} catch {
error.value = t('side_menu', 'Error while saving the settings')
loading.value = false
error.value = null
})
}
}
</script>