Skip to content

Commit fb7f163

Browse files
authored
Merge pull request #50418 from nextcloud/backport/50331/stable30
[stable30] fix(taskprocessing): More caching
2 parents 5efa224 + 4e10081 commit fb7f163

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

lib/private/TaskProcessing/Manager.php

+31-5
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
use OCP\Files\NotPermittedException;
3232
use OCP\Files\SimpleFS\ISimpleFile;
3333
use OCP\Http\Client\IClientService;
34+
use OCP\ICache;
35+
use OCP\ICacheFactory;
3436
use OCP\IConfig;
3537
use OCP\IL10N;
3638
use OCP\IServerContainer;
@@ -77,6 +79,11 @@ class Manager implements IManager {
7779
private ?array $availableTaskTypes = null;
7880

7981
private IAppData $appData;
82+
private ?array $preferences = null;
83+
private ?array $providersById = null;
84+
private ICache $cache;
85+
private ICache $distributedCache;
86+
8087
public function __construct(
8188
private IConfig $config,
8289
private Coordinator $coordinator,
@@ -91,8 +98,11 @@ public function __construct(
9198
private IUserMountCache $userMountCache,
9299
private IClientService $clientService,
93100
private IAppManager $appManager,
101+
ICacheFactory $cacheFactory,
94102
) {
95103
$this->appData = $appDataFactory->get('core');
104+
$this->cache = $cacheFactory->createLocal('task_processing::');
105+
$this->distributedCache = $cacheFactory->createDistributed('task_processing::');
96106
}
97107

98108

@@ -698,12 +708,23 @@ public function getProviders(): array {
698708

699709
public function getPreferredProvider(string $taskTypeId) {
700710
try {
701-
$preferences = json_decode($this->config->getAppValue('core', 'ai.taskprocessing_provider_preferences', 'null'), associative: true, flags: JSON_THROW_ON_ERROR);
711+
if ($this->preferences === null) {
712+
$this->preferences = $this->distributedCache->get('ai.taskprocessing_provider_preferences');
713+
if ($this->preferences === null) {
714+
$this->preferences = json_decode($this->config->getAppValue('core', 'ai.taskprocessing_provider_preferences', 'null'), associative: true, flags: JSON_THROW_ON_ERROR);
715+
$this->distributedCache->set('ai.taskprocessing_provider_preferences', $this->preferences, 60 * 3);
716+
}
717+
}
718+
702719
$providers = $this->getProviders();
703-
if (isset($preferences[$taskTypeId])) {
704-
$provider = current(array_values(array_filter($providers, fn ($provider) => $provider->getId() === $preferences[$taskTypeId])));
705-
if ($provider !== false) {
706-
return $provider;
720+
if (isset($this->preferences[$taskTypeId])) {
721+
$providersById = $this->providersById ?? array_reduce($providers, static function (array $carry, IProvider $provider) {
722+
$carry[$provider->getId()] = $provider;
723+
return $carry;
724+
}, []);
725+
$this->providersById = $providersById;
726+
if (isset($providersById[$this->preferences[$taskTypeId]])) {
727+
return $providersById[$this->preferences[$taskTypeId]];
707728
}
708729
}
709730
// By default, use the first available provider
@@ -719,6 +740,10 @@ public function getPreferredProvider(string $taskTypeId) {
719740
}
720741

721742
public function getAvailableTaskTypes(): array {
743+
if ($this->availableTaskTypes === null) {
744+
// We use local cache only because distributed cache uses JSOn stringify which would botch our ShapeDescriptor objects
745+
$this->availableTaskTypes = $this->cache->get('available_task_types');
746+
}
722747
if ($this->availableTaskTypes === null) {
723748
$taskTypes = $this->_getTaskTypes();
724749

@@ -750,6 +775,7 @@ public function getAvailableTaskTypes(): array {
750775
}
751776

752777
$this->availableTaskTypes = $availableTaskTypes;
778+
$this->cache->set('available_task_types', $this->availableTaskTypes, 60);
753779
}
754780

755781
return $this->availableTaskTypes;

tests/lib/TaskProcessing/TaskProcessingTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use OCP\Files\Config\IUserMountCache;
2222
use OCP\Files\IRootFolder;
2323
use OCP\Http\Client\IClientService;
24+
use OCP\ICacheFactory;
2425
use OCP\IConfig;
2526
use OCP\IDBConnection;
2627
use OCP\IServerContainer;
@@ -475,6 +476,7 @@ protected function setUp(): void {
475476
$this->userMountCache,
476477
\OC::$server->get(IClientService::class),
477478
\OC::$server->get(IAppManager::class),
479+
\OC::$server->get(ICacheFactory::class),
478480
);
479481
}
480482

0 commit comments

Comments
 (0)