3131use OCP \Files \NotPermittedException ;
3232use OCP \Files \SimpleFS \ISimpleFile ;
3333use OCP \Http \Client \IClientService ;
34+ use OCP \ICache ;
35+ use OCP \ICacheFactory ;
3436use OCP \IConfig ;
3537use OCP \IL10N ;
3638use 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 ;
0 commit comments