31
31
use OCP \Files \NotPermittedException ;
32
32
use OCP \Files \SimpleFS \ISimpleFile ;
33
33
use OCP \Http \Client \IClientService ;
34
+ use OCP \ICache ;
35
+ use OCP \ICacheFactory ;
34
36
use OCP \IConfig ;
35
37
use OCP \IL10N ;
36
38
use OCP \IServerContainer ;
@@ -77,6 +79,11 @@ class Manager implements IManager {
77
79
private ?array $ availableTaskTypes = null ;
78
80
79
81
private IAppData $ appData ;
82
+ private ?array $ preferences = null ;
83
+ private ?array $ providersById = null ;
84
+ private ICache $ cache ;
85
+ private ICache $ distributedCache ;
86
+
80
87
public function __construct (
81
88
private IConfig $ config ,
82
89
private Coordinator $ coordinator ,
@@ -91,8 +98,11 @@ public function __construct(
91
98
private IUserMountCache $ userMountCache ,
92
99
private IClientService $ clientService ,
93
100
private IAppManager $ appManager ,
101
+ ICacheFactory $ cacheFactory ,
94
102
) {
95
103
$ this ->appData = $ appDataFactory ->get ('core ' );
104
+ $ this ->cache = $ cacheFactory ->createLocal ('task_processing:: ' );
105
+ $ this ->distributedCache = $ cacheFactory ->createDistributed ('task_processing:: ' );
96
106
}
97
107
98
108
@@ -698,12 +708,23 @@ public function getProviders(): array {
698
708
699
709
public function getPreferredProvider (string $ taskTypeId ) {
700
710
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
+
702
719
$ 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 ]];
707
728
}
708
729
}
709
730
// By default, use the first available provider
@@ -719,6 +740,10 @@ public function getPreferredProvider(string $taskTypeId) {
719
740
}
720
741
721
742
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
+ }
722
747
if ($ this ->availableTaskTypes === null ) {
723
748
$ taskTypes = $ this ->_getTaskTypes ();
724
749
@@ -750,6 +775,7 @@ public function getAvailableTaskTypes(): array {
750
775
}
751
776
752
777
$ this ->availableTaskTypes = $ availableTaskTypes ;
778
+ $ this ->cache ->set ('available_task_types ' , $ this ->availableTaskTypes , 60 );
753
779
}
754
780
755
781
return $ this ->availableTaskTypes ;
0 commit comments