|
|
|
@@ -5,7 +5,10 @@ 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;
|
|
|
|
@@ -33,6 +36,7 @@ class CategoryRepository
|
|
|
|
|
protected IAppConfig $appConfig,
|
|
|
|
|
protected ITimeFactory $timeFactory,
|
|
|
|
|
protected LangRepository $langRepository,
|
|
|
|
|
protected ICacheFactory $cacheFactory,
|
|
|
|
|
) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -94,6 +98,7 @@ class CategoryRepository
|
|
|
|
|
|
|
|
|
|
public function clearCache(): void
|
|
|
|
|
{
|
|
|
|
|
$this->getDistributedCache()?->remove(self::CACHE_KEY);
|
|
|
|
|
$this->appConfig->deleteKey(Application::APP_ID, self::CACHE_KEY);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -117,18 +122,57 @@ class CategoryRepository
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
* 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->appConfig->getValueArray(Application::APP_ID, self::CACHE_KEY, [], lazy: true);
|
|
|
|
|
$cache = $this->getDistributedCache();
|
|
|
|
|
|
|
|
|
|
if (isset($cache['expires'], $cache['categories']) && $cache['expires'] > $this->timeFactory->getTime()) {
|
|
|
|
|
return $cache['categories'];
|
|
|
|
|
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 = [];
|
|
|
|
|
|
|
|
|
@@ -147,15 +191,13 @@ class CategoryRepository
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function getDistributedCache(): ?ICache
|
|
|
|
|
{
|
|
|
|
|
return $this->cacheFactory->isAvailable()
|
|
|
|
|
? $this->cacheFactory->createDistributed(Application::APP_ID)
|
|
|
|
|
: null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|