Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix(taskprocessing): cache provider settings in distributed cache as …
…well

Signed-off-by: Marcel Klehr <[email protected]>
  • Loading branch information
marcelklehr committed Jan 24, 2025
commit c489d7d8a527ac08f4e58b23946622e900b305bc
12 changes: 11 additions & 1 deletion lib/private/TaskProcessing/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class Manager implements IManager {
private IAppData $appData;
private ?array $preferences = null;
private ICache $cache;
private ICache $distributedCache;

public function __construct(
private IConfig $config,
Expand All @@ -100,6 +101,7 @@ public function __construct(
) {
$this->appData = $appDataFactory->get('core');
$this->cache = $cacheFactory->createLocal('task_processing::');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

distributed cache would be good here too, it falls back to local cache if unavailable

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we can't use distributed cache here because it uses json_encode to serialize the values and that breaks stuff

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

apcu will serialize faithfully, afaik

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can probably handle the json_encode ourselves and pass the validated result only to the cache but feel free to use local only, won't affect it much

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At least the result of getAvailableTaskTypes( should be string-array only? So should be totally fine?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small comment regarding local vs distributed cache.

Local cache should typically be better for performance because it has lower latency. The distributed cache leaves the machine, at least for clustered setups with a dedicated Redis node.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At least the result of getAvailableTaskTypes( should be string-array only?

It's not, sadly

$this->cache = $cacheFactory->createDistributed('task_processing::');
}


Expand Down Expand Up @@ -732,7 +734,14 @@ public function getProviders(): array {

public function getPreferredProvider(string $taskTypeId) {
try {
$this->preferences = $this->preferences ?? json_decode($this->config->getAppValue('core', 'ai.taskprocessing_provider_preferences', 'null'), associative: true, flags: JSON_THROW_ON_ERROR);
if ($this->preferences === null) {
$this->preferences = $this->distributedCache->get('ai.taskprocessing_provider_preferences');
if ($this->preferences === null) {
$this->preferences = json_decode($this->config->getAppValue('core', 'ai.taskprocessing_provider_preferences', 'null'), associative: true, flags: JSON_THROW_ON_ERROR);
$this->distributedCache->set('ai.taskprocessing_provider_preferences', $this->preferences, 60 * 3);
}
}

$providers = $this->getProviders();
if (isset($this->preferences[$taskTypeId])) {
$provider = current(array_values(array_filter($providers, fn ($provider) => $provider->getId() === $this->preferences[$taskTypeId])));
Expand All @@ -754,6 +763,7 @@ public function getPreferredProvider(string $taskTypeId) {

public function getAvailableTaskTypes(bool $showDisabled = false): array {
if ($this->availableTaskTypes === null) {
// We use local cache only because distributed cache uses JSOn stringify which would botch our ShapeDescriptor objects
$this->availableTaskTypes = $this->cache->get('available_task_types');
}
// Either we have no cache or showDisabled is turned on, which we don't want to cache, ever.
Expand Down