]> source.dussan.org Git - nextcloud-server.git/commitdiff
Cache responses from the AppStore server
authorLukas Reschke <lukas@owncloud.com>
Fri, 9 Jan 2015 17:31:39 +0000 (18:31 +0100)
committerLukas Reschke <lukas@owncloud.com>
Fri, 9 Jan 2015 18:49:59 +0000 (19:49 +0100)
Otherwise every time the AppStore was opened a lot of connections to the AppStore server were made which resulted in a terrible performance.

This changeset will cache the response for a sensible time so that only the first request will be somewhat slow.

Performance changes:
- Loading a category took previously more than 3 seconds on my machine. Now for every follow-up request it takes less than 200ms, resulting in a performance gain of 1950%
- Loading the category list took previously about 750ms - now it takes 154ms, a total performance gain of 395%

lib/private/ocsclient.php
settings/ajax/disableapp.php
settings/ajax/enableapp.php
settings/ajax/installapp.php
settings/ajax/uninstallapp.php
settings/application.php
settings/controller/appsettingscontroller.php

index 351027d80189e0b335105262ace97fa124b99725..7ce6723128bb7b01fd447d1f8705b0b5936a7a23 100644 (file)
@@ -147,7 +147,7 @@ class OC_OCSClient{
                        $app['changed']=strtotime($tmp[$i]->changed);
                        $app['description']=(string)$tmp[$i]->description;
                        $app['score']=(string)$tmp[$i]->score;
-                       $app['downloads'] = $tmp[$i]->downloads;
+                       $app['downloads'] = (int)$tmp[$i]->downloads;
 
                        $apps[]=$app;
                }
index c1e5bc8eac76680cce533ab50d40207fd8964d54..1a133ea9af7bd325f69d38198c53f75971b9402e 100644 (file)
@@ -10,5 +10,9 @@ if (!array_key_exists('appid', $_POST)) {
 $appId = $_POST['appid'];
 $appId = OC_App::cleanAppId($appId);
 
+// FIXME: Clear the cache - move that into some sane helper method
+\OC::$server->getMemCacheFactory()->create('settings')->remove('listApps-0');
+\OC::$server->getMemCacheFactory()->create('settings')->remove('listApps-1');
+
 OC_App::disable($appId);
 OC_JSON::success();
index 81ca1e0338d40d69288cb3d557be32f9e7bfd229..88abff487db450dfcf774b7423a244cf3bb7e604 100644 (file)
@@ -7,6 +7,9 @@ $groups = isset($_POST['groups']) ? $_POST['groups'] : null;
 
 try {
        OC_App::enable(OC_App::cleanAppId($_POST['appid']), $groups);
+       // FIXME: Clear the cache - move that into some sane helper method
+       \OC::$server->getMemCacheFactory()->create('settings')->remove('listApps-0');
+       \OC::$server->getMemCacheFactory()->create('settings')->remove('listApps-1');
        OC_JSON::success();
 } catch (Exception $e) {
        OC_Log::write('core', $e->getMessage(), OC_Log::ERROR);
index 80bc1819724b0290dd9a6cc33b493c951f91018e..f25e68214a7f5f4b1f8b5a4da0930059b6fe94ce 100644 (file)
@@ -12,6 +12,9 @@ $appId = OC_App::cleanAppId($appId);
 
 $result = OC_App::installApp($appId);
 if($result !== false) {
+       // FIXME: Clear the cache - move that into some sane helper method
+       \OC::$server->getMemCacheFactory()->create('settings')->remove('listApps-0');
+       \OC::$server->getMemCacheFactory()->create('settings')->remove('listApps-1');
        OC_JSON::success(array('data' => array('appid' => $appId)));
 } else {
        $l = \OC::$server->getL10N('settings');
index cae7c33f292f7d7926ceda9c270862f2036610d6..e50fc31a449faf8987e0ae674c6d7bdfb1ce2103 100644 (file)
@@ -12,6 +12,9 @@ $appId = OC_App::cleanAppId($appId);
 
 $result = OC_App::removeApp($appId);
 if($result !== false) {
+       // FIXME: Clear the cache - move that into some sane helper method
+       \OC::$server->getMemCacheFactory()->create('settings')->remove('listApps-0');
+       \OC::$server->getMemCacheFactory()->create('settings')->remove('listApps-1');
        OC_JSON::success(array('data' => array('appid' => $appId)));
 } else {
        $l = \OC::$server->getL10N('settings');
index 070da8024b22fe84216079b275d10ef94370b10f..f7ba72f3bfc6181a4f42fdbb652314f695522c9b 100644 (file)
@@ -55,7 +55,8 @@ class Application extends App {
                                $c->query('AppName'),
                                $c->query('Request'),
                                $c->query('L10N'),
-                               $c->query('Config')
+                               $c->query('Config'),
+                               $c->query('ICacheFactory')
                        );
                });
                $container->registerService('SecuritySettingsController', function(IContainer $c) {
@@ -120,6 +121,9 @@ class Application extends App {
                $container->registerService('Config', function(IContainer $c) {
                        return $c->query('ServerContainer')->getConfig();
                });
+               $container->registerService('ICacheFactory', function(IContainer $c) {
+                       return $c->query('ServerContainer')->getMemCacheFactory();
+               });
                $container->registerService('L10N', function(IContainer $c) {
                        return $c->query('ServerContainer')->getL10N('settings');
                });
index 3688859ef5602b637f954fe60532838b677ceaa4..55677d64870b5f2110979bf510da20bd53cfdd6c 100644 (file)
@@ -14,6 +14,7 @@ namespace OC\Settings\Controller;
 use OC\App\DependencyAnalyzer;
 use OC\App\Platform;
 use \OCP\AppFramework\Controller;
+use OCP\ICacheFactory;
 use OCP\IRequest;
 use OCP\IL10N;
 use OCP\IConfig;
@@ -27,20 +28,25 @@ class AppSettingsController extends Controller {
        private $l10n;
        /** @var IConfig */
        private $config;
+       /** @var \OCP\ICache */
+       private $cache;
 
        /**
         * @param string $appName
         * @param IRequest $request
         * @param IL10N $l10n
         * @param IConfig $config
+        * @param ICacheFactory $cache
         */
        public function __construct($appName,
                                                                IRequest $request,
                                                                IL10N $l10n,
-                                                               IConfig $config) {
+                                                               IConfig $config,
+                                                               ICacheFactory $cache) {
                parent::__construct($appName, $request);
                $this->l10n = $l10n;
                $this->config = $config;
+               $this->cache = $cache->create($appName);
        }
 
        /**
@@ -49,13 +55,16 @@ class AppSettingsController extends Controller {
         */
        public function listCategories() {
 
-               $categories = array(
-                       array('id' => 0, 'displayName' => (string)$this->l10n->t('Enabled') ),
-                       array('id' => 1, 'displayName' => (string)$this->l10n->t('Not enabled') ),
-               );
+               if(!is_null($this->cache->get('listCategories'))) {
+                       return $this->cache->get('listCategories');
+               }
+               $categories = [
+                       ['id' => 0, 'displayName' => (string)$this->l10n->t('Enabled')],
+                       ['id' => 1, 'displayName' => (string)$this->l10n->t('Not enabled')],
+               ];
 
                if($this->config->getSystemValue('appstoreenabled', true)) {
-                       $categories[] = array('id' => 2, 'displayName' => (string)$this->l10n->t('Recommended') );
+                       $categories[] = ['id' => 2, 'displayName' => (string)$this->l10n->t('Recommended')];
                        // apps from external repo via OCS
                        $ocs = \OC_OCSClient::getCategories();
                        foreach($ocs as $k => $v) {
@@ -67,6 +76,7 @@ class AppSettingsController extends Controller {
                }
 
                $categories['status'] = 'success';
+               $this->cache->set('listCategories', $categories, 3600);
 
                return $categories;
        }
@@ -77,44 +87,46 @@ class AppSettingsController extends Controller {
         * @return array
         */
        public function listApps($category = 0) {
-               $apps = array();
-
-               switch($category) {
-                       // installed apps
-                       case 0:
-                               $apps = \OC_App::listAllApps(true);
-                               $apps = array_filter($apps, function($app) {
-                                       return $app['active'];
-                               });
-                               break;
-                       // not-installed apps
-                       case 1:
-                               $apps = \OC_App::listAllApps(true);
-                               $apps = array_filter($apps, function($app) {
-                                       return !$app['active'];
-                               });
-                               break;
-                       default:
-                               if ($category === 2) {
-                                       $apps = \OC_App::getAppstoreApps('approved');
-                                       $apps = array_filter($apps, function($app) {
-                                               return isset($app['internalclass']) && $app['internalclass'] === 'recommendedapp';
+               if(!is_null($this->cache->get('listApps-'.$category))) {
+                       $apps = $this->cache->get('listApps-'.$category);
+               } else {
+                       switch ($category) {
+                               // installed apps
+                               case 0:
+                                       $apps = \OC_App::listAllApps(true);
+                                       $apps = array_filter($apps, function ($app) {
+                                               return $app['active'];
+                                       });
+                                       break;
+                               // not-installed apps
+                               case 1:
+                                       $apps = \OC_App::listAllApps(true);
+                                       $apps = array_filter($apps, function ($app) {
+                                               return !$app['active'];
                                        });
-                               } else {
-                                       $apps = \OC_App::getAppstoreApps('approved', $category);
-                               }
-                               if (!$apps) {
-                                       $apps = array();
-                               }
-                               usort($apps, function ($a, $b) {
-                                       $a = (int)$a['score'];
-                                       $b = (int)$b['score'];
-                                       if ($a === $b) {
-                                               return 0;
+                                       break;
+                               default:
+                                       if ($category === 2) {
+                                               $apps = \OC_App::getAppstoreApps('approved');
+                                               $apps = array_filter($apps, function ($app) {
+                                                       return isset($app['internalclass']) && $app['internalclass'] === 'recommendedapp';
+                                               });
+                                       } else {
+                                               $apps = \OC_App::getAppstoreApps('approved', $category);
+                                       }
+                                       if (!$apps) {
+                                               $apps = array();
                                        }
-                                       return ($a > $b) ? -1 : 1;
-                               });
-                               break;
+                                       usort($apps, function ($a, $b) {
+                                               $a = (int)$a['score'];
+                                               $b = (int)$b['score'];
+                                               if ($a === $b) {
+                                                       return 0;
+                                               }
+                                               return ($a > $b) ? -1 : 1;
+                                       });
+                                       break;
+                       }
                }
 
                // fix groups to be an array
@@ -142,6 +154,8 @@ class AppSettingsController extends Controller {
                        return $app;
                }, $apps);
 
-               return array('apps' => $apps, 'status' => 'success');
+               $this->cache->set('listApps-'.$category, $apps, 300);
+
+               return ['apps' => $apps, 'status' => 'success'];
        }
 }