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
@@ -582,10 +592,10 @@ private function _getTaskTypeSettings(): array {
582
592
foreach ($ taskTypes as $ taskType ) {
583
593
$ taskTypeSettings [$ taskType ->getId ()] = false ;
584
594
};
585
-
595
+
586
596
return $ taskTypeSettings ;
587
597
}
588
-
598
+
589
599
}
590
600
591
601
/**
@@ -725,12 +735,23 @@ public function getProviders(): array {
725
735
726
736
public function getPreferredProvider (string $ taskTypeId ) {
727
737
try {
728
- $ preferences = json_decode ($ this ->config ->getAppValue ('core ' , 'ai.taskprocessing_provider_preferences ' , 'null ' ), associative: true , flags: JSON_THROW_ON_ERROR );
738
+ if ($ this ->preferences === null ) {
739
+ $ this ->preferences = $ this ->distributedCache ->get ('ai.taskprocessing_provider_preferences ' );
740
+ if ($ this ->preferences === null ) {
741
+ $ this ->preferences = json_decode ($ this ->config ->getAppValue ('core ' , 'ai.taskprocessing_provider_preferences ' , 'null ' ), associative: true , flags: JSON_THROW_ON_ERROR );
742
+ $ this ->distributedCache ->set ('ai.taskprocessing_provider_preferences ' , $ this ->preferences , 60 * 3 );
743
+ }
744
+ }
745
+
729
746
$ providers = $ this ->getProviders ();
730
- if (isset ($ preferences [$ taskTypeId ])) {
731
- $ provider = current (array_values (array_filter ($ providers , fn ($ provider ) => $ provider ->getId () === $ preferences [$ taskTypeId ])));
732
- if ($ provider !== false ) {
733
- return $ provider ;
747
+ if (isset ($ this ->preferences [$ taskTypeId ])) {
748
+ $ providersById = $ this ->providersById ?? array_reduce ($ providers , static function (array $ carry , IProvider $ provider ) {
749
+ $ carry [$ provider ->getId ()] = $ provider ;
750
+ return $ carry ;
751
+ }, []);
752
+ $ this ->providersById = $ providersById ;
753
+ if (isset ($ providersById [$ this ->preferences [$ taskTypeId ]])) {
754
+ return $ providersById [$ this ->preferences [$ taskTypeId ]];
734
755
}
735
756
}
736
757
// By default, use the first available provider
@@ -746,6 +767,10 @@ public function getPreferredProvider(string $taskTypeId) {
746
767
}
747
768
748
769
public function getAvailableTaskTypes (bool $ showDisabled = false ): array {
770
+ if ($ this ->availableTaskTypes === null ) {
771
+ // We use local cache only because distributed cache uses JSOn stringify which would botch our ShapeDescriptor objects
772
+ $ this ->availableTaskTypes = $ this ->cache ->get ('available_task_types ' );
773
+ }
749
774
// Either we have no cache or showDisabled is turned on, which we don't want to cache, ever.
750
775
if ($ this ->availableTaskTypes === null || $ showDisabled ) {
751
776
$ taskTypes = $ this ->_getTaskTypes ();
@@ -787,6 +812,7 @@ public function getAvailableTaskTypes(bool $showDisabled = false): array {
787
812
}
788
813
789
814
$ this ->availableTaskTypes = $ availableTaskTypes ;
815
+ $ this ->cache ->set ('available_task_types ' , $ this ->availableTaskTypes , 60 );
790
816
}
791
817
792
818
0 commit comments