Skip to content

Commit 7cbca1a

Browse files
authored
enh: use distributed cache only when available (nextcloud#347)
It's a refactoring, it doesn't really fix anything(?), but it makes working with caches in the style of a Nextcloud server repository. Signed-off-by: Alexander Piskun <[email protected]>
1 parent 7082ccb commit 7cbca1a

10 files changed

+73
-53
lines changed

lib/Service/ExAppEventsListenerService.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,17 @@
1616

1717
class ExAppEventsListenerService {
1818

19-
private ICache $cache;
19+
private ?ICache $cache = null;
2020

2121
public function __construct(
2222
private readonly LoggerInterface $logger,
2323
private readonly ExAppEventsListenerMapper $mapper,
2424
ICacheFactory $cacheFactory,
2525

2626
) {
27-
$this->cache = $cacheFactory->createDistributed(Application::APP_ID . '/ex_events_listener');
27+
if ($cacheFactory->isAvailable()) {
28+
$this->cache = $cacheFactory->createDistributed(Application::APP_ID . '/ex_events_listener');
29+
}
2830
}
2931

3032
public function registerEventsListener(string $appId, string $eventType, string $actionHandler, array $eventSubtypes = []): ?ExAppEventsListener {
@@ -79,10 +81,10 @@ public function getEventsListener(string $appId, string $eventType): ?ExAppEvent
7981
public function getEventsListeners(): array {
8082
try {
8183
$cacheKey = '/ex_events_listener';
82-
$records = $this->cache->get($cacheKey);
84+
$records = $this->cache?->get($cacheKey);
8385
if ($records === null) {
8486
$records = $this->mapper->findAllEnabled();
85-
$this->cache->set($cacheKey, $records);
87+
$this->cache?->set($cacheKey, $records);
8688
}
8789
return array_map(function ($record) {
8890
return new ExAppEventsListener($record);
@@ -103,6 +105,6 @@ public function unregisterExAppEventListeners(string $appId): int {
103105
}
104106

105107
public function resetCacheEnabled(): void {
106-
$this->cache->remove('/ex_events_listener');
108+
$this->cache?->remove('/ex_events_listener');
107109
}
108110
}

lib/Service/ExAppOccService.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,16 @@
2222

2323
class ExAppOccService {
2424

25-
private ICache $cache;
25+
private ?ICache $cache = null;
2626

2727
public function __construct(
2828
private readonly LoggerInterface $logger,
2929
private readonly ExAppOccCommandMapper $mapper,
3030
ICacheFactory $cacheFactory,
3131
) {
32-
$this->cache = $cacheFactory->createDistributed(Application::APP_ID . '/ex_occ_commands');
32+
if ($cacheFactory->isAvailable()) {
33+
$this->cache = $cacheFactory->createDistributed(Application::APP_ID . '/ex_occ_commands');
34+
}
3335
}
3436

3537
public function registerCommand(
@@ -93,10 +95,10 @@ public function getOccCommand(string $appId, string $name): ?ExAppOccCommand {
9395
public function getOccCommands(): array {
9496
try {
9597
$cacheKey = '/ex_occ_commands';
96-
$records = $this->cache->get($cacheKey);
98+
$records = $this->cache?->get($cacheKey);
9799
if ($records === null) {
98100
$records = $this->mapper->findAllEnabled();
99-
$this->cache->set($cacheKey, $records);
101+
$this->cache?->set($cacheKey, $records);
100102
}
101103
return array_map(function ($record) {
102104
return new ExAppOccCommand($record);
@@ -261,6 +263,6 @@ public function unregisterExAppOccCommands(string $appId): int {
261263
}
262264

263265
public function resetCacheEnabled(): void {
264-
$this->cache->remove('/ex_occ_commands');
266+
$this->cache?->remove('/ex_occ_commands');
265267
}
266268
}

lib/Service/ExAppService.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
use SimpleXMLElement;
3131

3232
class ExAppService {
33-
private ICache $cache;
33+
private ?ICache $cache = null;
3434

3535
public function __construct(
3636
private readonly LoggerInterface $logger,
@@ -54,7 +54,9 @@ public function __construct(
5454
private readonly ExAppEventsListenerService $eventsListenerService,
5555
private readonly ExAppOccService $occService,
5656
) {
57-
$this->cache = $cacheFactory->createDistributed(Application::APP_ID . '/service');
57+
if ($cacheFactory->isAvailable()) {
58+
$this->cache = $cacheFactory->createDistributed(Application::APP_ID . '/service');
59+
}
5860
}
5961

6062
public function getExApp(string $appId): ?ExApp {
@@ -83,7 +85,7 @@ public function registerExApp(array $appInfo): ?ExApp {
8385
try {
8486
$this->exAppMapper->insert($exApp);
8587
$exApp = $this->exAppMapper->findByAppId($appInfo['id']);
86-
$this->cache->remove('/ex_apps');
88+
$this->cache?->remove('/ex_apps');
8789
if (isset($appInfo['external-app']['routes'])) {
8890
$exApp->setRoutes($this->registerExAppRoutes($exApp, $appInfo['external-app']['routes'])->getRoutes() ?? []);
8991
}
@@ -121,7 +123,7 @@ public function unregisterExApp(string $appId): bool {
121123
if ($rmRoutes === null) {
122124
$this->logger->error(sprintf('Error while unregistering %s ExApp routes from the database.', $appId));
123125
}
124-
$this->cache->remove('/ex_apps');
126+
$this->cache?->remove('/ex_apps');
125127
return $r === 1 && $rmRoutes !== null;
126128
}
127129

@@ -208,14 +210,14 @@ public function updateExAppInfo(ExApp $exApp, array $appInfo): bool {
208210
public function updateExApp(ExApp $exApp, array $fields = ['version', 'name', 'port', 'status', 'enabled', 'last_check_time', 'api_scopes']): bool {
209211
try {
210212
$this->exAppMapper->updateExApp($exApp, $fields);
211-
$this->cache->remove('/ex_apps');
213+
$this->cache?->remove('/ex_apps');
212214
if (in_array('enabled', $fields) || in_array('version', $fields)) {
213215
$this->resetCaches();
214216
}
215217
return true;
216218
} catch (Exception $e) {
217219
$this->logger->error(sprintf('Failed to update "%s" ExApp info.', $exApp->getAppid()), ['exception' => $e]);
218-
$this->cache->remove('/ex_apps');
220+
$this->cache?->remove('/ex_apps');
219221
$this->resetCaches();
220222
}
221223
return false;
@@ -372,14 +374,14 @@ public function setStatusError(ExApp $exApp, string $error): void {
372374
public function getExApps(): array {
373375
try {
374376
$cacheKey = '/ex_apps';
375-
$records = $this->cache->get($cacheKey);
377+
$records = $this->cache?->get($cacheKey);
376378
if ($records !== null) {
377379
return array_map(function ($record) {
378380
return $record instanceof ExApp ? $record : new ExApp($record);
379381
}, $records);
380382
}
381383
$records = $this->exAppMapper->findAll();
382-
$this->cache->set($cacheKey, $records);
384+
$this->cache?->set($cacheKey, $records);
383385
return $records;
384386
} catch (Exception) {
385387
return [];

lib/Service/ProvidersAI/SpeechToTextService.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,16 @@
2323
use Psr\Log\LoggerInterface;
2424

2525
class SpeechToTextService {
26-
private ICache $cache;
26+
private ?ICache $cache = null;
2727

2828
public function __construct(
2929
ICacheFactory $cacheFactory,
3030
private readonly SpeechToTextProviderMapper $mapper,
3131
private readonly LoggerInterface $logger,
3232
) {
33-
$this->cache = $cacheFactory->createDistributed(Application::APP_ID . '/ex_speech_to_text_providers');
33+
if ($cacheFactory->isAvailable()) {
34+
$this->cache = $cacheFactory->createDistributed(Application::APP_ID . '/ex_speech_to_text_providers');
35+
}
3436
}
3537

3638
public function registerSpeechToTextProvider(string $appId, string $name, string $displayName, string $actionHandler): ?SpeechToTextProvider {
@@ -82,10 +84,10 @@ public function unregisterSpeechToTextProvider(string $appId, string $name): ?Sp
8284
public function getRegisteredSpeechToTextProviders(): array {
8385
try {
8486
$cacheKey = '/ex_speech_to_text_providers';
85-
$records = $this->cache->get($cacheKey);
87+
$records = $this->cache?->get($cacheKey);
8688
if ($records === null) {
8789
$records = $this->mapper->findAllEnabled();
88-
$this->cache->set($cacheKey, $records);
90+
$this->cache?->set($cacheKey, $records);
8991
}
9092
return array_map(function ($record) {
9193
return new SpeechToTextProvider($record);
@@ -119,7 +121,7 @@ public function unregisterExAppSpeechToTextProviders(string $appId): int {
119121
}
120122

121123
public function resetCacheEnabled(): void {
122-
$this->cache->remove('/ex_speech_to_text_providers');
124+
$this->cache?->remove('/ex_speech_to_text_providers');
123125
}
124126

125127
/**

lib/Service/ProvidersAI/TaskProcessingService.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,16 @@
2121
use Psr\Log\LoggerInterface;
2222

2323
class TaskProcessingService {
24-
private ICache $cache;
24+
private ?ICache $cache = null;
2525

2626
public function __construct(
2727
ICacheFactory $cacheFactory,
2828
private readonly TaskProcessingProviderMapper $mapper,
2929
private readonly LoggerInterface $logger,
3030
) {
31-
$this->cache = $cacheFactory->createDistributed(Application::APP_ID . '/ex_task_processing_providers');
31+
if ($cacheFactory->isAvailable()) {
32+
$this->cache = $cacheFactory->createDistributed(Application::APP_ID . '/ex_task_processing_providers');
33+
}
3234
}
3335

3436
/**
@@ -39,10 +41,10 @@ public function __construct(
3941
public function getRegisteredTaskProcessingProviders(): array {
4042
try {
4143
$cacheKey = '/ex_task_processing_providers';
42-
$records = $this->cache->get($cacheKey);
44+
$records = $this->cache?->get($cacheKey);
4345
if ($records === null) {
4446
$records = $this->mapper->findAllEnabled();
45-
$this->cache->set($cacheKey, $records);
47+
$this->cache?->set($cacheKey, $records);
4648
}
4749

4850
return array_map(static function ($record) {
@@ -199,7 +201,7 @@ public function getOptionalOutputShapeEnumValues(): array {
199201
}
200202

201203
public function resetCacheEnabled(): void {
202-
$this->cache->remove('/ex_task_processing_providers');
204+
$this->cache?->remove('/ex_task_processing_providers');
203205
}
204206

205207
public function unregisterExAppTaskProcessingProviders(string $appId): int {

lib/Service/ProvidersAI/TextProcessingService.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,16 @@ class TextProcessingService {
3030
'topics' => 'OCP\TextProcessing\TopicsTaskType',
3131
];
3232

33-
private ICache $cache;
33+
private ?ICache $cache = null;
3434

3535
public function __construct(
3636
ICacheFactory $cacheFactory,
3737
private readonly TextProcessingProviderMapper $mapper,
3838
private readonly LoggerInterface $logger,
3939
) {
40-
$this->cache = $cacheFactory->createDistributed(Application::APP_ID . '/ex_text_processing_providers');
40+
if ($cacheFactory->isAvailable()) {
41+
$this->cache = $cacheFactory->createDistributed(Application::APP_ID . '/ex_text_processing_providers');
42+
}
4143
}
4244

4345
/**
@@ -48,10 +50,10 @@ public function __construct(
4850
public function getRegisteredTextProcessingProviders(): array {
4951
try {
5052
$cacheKey = '/ex_text_processing_providers';
51-
$records = $this->cache->get($cacheKey);
53+
$records = $this->cache?->get($cacheKey);
5254
if ($records === null) {
5355
$records = $this->mapper->findAllEnabled();
54-
$this->cache->set($cacheKey, $records);
56+
$this->cache?->set($cacheKey, $records);
5557
}
5658

5759
return array_map(function ($record) {
@@ -242,7 +244,7 @@ private function isTaskTypeValid(string $getActionType): bool {
242244
}
243245

244246
public function resetCacheEnabled(): void {
245-
$this->cache->remove('/ex_text_processing_providers');
247+
$this->cache?->remove('/ex_text_processing_providers');
246248
}
247249

248250
public function unregisterExAppTextProcessingProviders(string $appId): int {

lib/Service/ProvidersAI/TranslationService.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,16 @@
2424
use Psr\Log\LoggerInterface;
2525

2626
class TranslationService {
27-
private ICache $cache;
27+
private ?ICache $cache = null;
2828

2929
public function __construct(
3030
ICacheFactory $cacheFactory,
3131
private readonly TranslationProviderMapper $mapper,
3232
private readonly LoggerInterface $logger,
3333
) {
34-
$this->cache = $cacheFactory->createDistributed(Application::APP_ID . '/ex_translation_providers');
34+
if ($cacheFactory->isAvailable()) {
35+
$this->cache = $cacheFactory->createDistributed(Application::APP_ID . '/ex_translation_providers');
36+
}
3537
}
3638

3739
public function registerTranslationProvider(
@@ -94,10 +96,10 @@ public function unregisterTranslationProvider(string $appId, string $name): ?Tra
9496
public function getRegisteredTranslationProviders(): array {
9597
try {
9698
$cacheKey = '/ex_translation_providers';
97-
$records = $this->cache->get($cacheKey);
99+
$records = $this->cache?->get($cacheKey);
98100
if ($records === null) {
99101
$records = $this->mapper->findAllEnabled();
100-
$this->cache->set($cacheKey, $records);
102+
$this->cache?->set($cacheKey, $records);
101103
}
102104
return array_map(function ($record) {
103105
return new TranslationProvider($record);
@@ -131,7 +133,7 @@ public function unregisterExAppTranslationProviders(string $appId): int {
131133
}
132134

133135
public function resetCacheEnabled(): void {
134-
$this->cache->remove('/ex_translation_providers');
136+
$this->cache?->remove('/ex_translation_providers');
135137
}
136138

137139
/**

lib/Service/UI/FilesActionsMenuService.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,16 @@
1515
use Psr\Log\LoggerInterface;
1616

1717
class FilesActionsMenuService {
18-
private ICache $cache;
18+
private ?ICache $cache = null;
1919

2020
public function __construct(
2121
ICacheFactory $cacheFactory,
2222
private readonly FilesActionsMenuMapper $mapper,
2323
private readonly LoggerInterface $logger,
2424
) {
25-
$this->cache = $cacheFactory->createDistributed(Application::APP_ID . '/ex_ui_files_actions');
25+
if ($cacheFactory->isAvailable()) {
26+
$this->cache = $cacheFactory->createDistributed(Application::APP_ID . '/ex_ui_files_actions');
27+
}
2628
}
2729

2830
/**
@@ -94,10 +96,10 @@ public function unregisterFileActionMenu(string $appId, string $name): ?FilesAct
9496
public function getRegisteredFileActions(): array {
9597
try {
9698
$cacheKey = '/ex_ui_files_actions';
97-
$records = $this->cache->get($cacheKey);
99+
$records = $this->cache?->get($cacheKey);
98100
if ($records === null) {
99101
$records = $this->mapper->findAllEnabled();
100-
$this->cache->set($cacheKey, $records);
102+
$this->cache?->set($cacheKey, $records);
101103
}
102104
return array_map(function ($record) {
103105
return new FilesActionsMenu($record);
@@ -131,6 +133,6 @@ public function unregisterExAppFileActions(string $appId): int {
131133
}
132134

133135
public function resetCacheEnabled(): void {
134-
$this->cache->remove('/ex_ui_files_actions');
136+
$this->cache?->remove('/ex_ui_files_actions');
135137
}
136138
}

lib/Service/UI/SettingsService.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,16 @@
1515
use Psr\Log\LoggerInterface;
1616

1717
class SettingsService {
18-
private ICache $cache;
18+
private ?ICache $cache = null;
1919

2020
public function __construct(
2121
ICacheFactory $cacheFactory,
2222
private readonly SettingsFormMapper $mapper,
2323
private readonly LoggerInterface $logger,
2424
) {
25-
$this->cache = $cacheFactory->createDistributed(Application::APP_ID . '/ex_settings_forms');
25+
if ($cacheFactory->isAvailable()) {
26+
$this->cache = $cacheFactory->createDistributed(Application::APP_ID . '/ex_settings_forms');
27+
}
2628
}
2729

2830
public function registerForm(
@@ -78,10 +80,10 @@ public function unregisterForm(string $appId, string $formId): ?SettingsForm {
7880
public function getRegisteredForms(): array {
7981
try {
8082
$cacheKey = '/ex_settings_forms';
81-
$records = $this->cache->get($cacheKey);
83+
$records = $this->cache?->get($cacheKey);
8284
if ($records === null) {
8385
$records = $this->mapper->findAllEnabled();
84-
$this->cache->set($cacheKey, $records);
86+
$this->cache?->set($cacheKey, $records);
8587
}
8688
return array_map(function ($record) {
8789
return new SettingsForm($record);
@@ -115,6 +117,6 @@ public function unregisterExAppForms(string $appId): int {
115117
}
116118

117119
public function resetCacheEnabled(): void {
118-
$this->cache->remove('/ex_settings_forms');
120+
$this->cache?->remove('/ex_settings_forms');
119121
}
120122
}

0 commit comments

Comments
 (0)