Merge pull request #13212 from owncloud/cache-appstore-response

Cache responses from the AppStore server
This commit is contained in:
Morris Jobke 2015-01-10 09:56:00 +01:00
commit ae3483299c
7 changed files with 76 additions and 45 deletions

View File

@ -147,7 +147,7 @@ class OC_OCSClient{
$app['changed']=strtotime($tmp[$i]->changed); $app['changed']=strtotime($tmp[$i]->changed);
$app['description']=(string)$tmp[$i]->description; $app['description']=(string)$tmp[$i]->description;
$app['score']=(string)$tmp[$i]->score; $app['score']=(string)$tmp[$i]->score;
$app['downloads'] = $tmp[$i]->downloads; $app['downloads'] = (int)$tmp[$i]->downloads;
$apps[]=$app; $apps[]=$app;
} }

View File

@ -10,5 +10,9 @@ if (!array_key_exists('appid', $_POST)) {
$appId = $_POST['appid']; $appId = $_POST['appid'];
$appId = OC_App::cleanAppId($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_App::disable($appId);
OC_JSON::success(); OC_JSON::success();

View File

@ -7,6 +7,9 @@ $groups = isset($_POST['groups']) ? $_POST['groups'] : null;
try { try {
OC_App::enable(OC_App::cleanAppId($_POST['appid']), $groups); 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(); OC_JSON::success();
} catch (Exception $e) { } catch (Exception $e) {
OC_Log::write('core', $e->getMessage(), OC_Log::ERROR); OC_Log::write('core', $e->getMessage(), OC_Log::ERROR);

View File

@ -12,6 +12,9 @@ $appId = OC_App::cleanAppId($appId);
$result = OC_App::installApp($appId); $result = OC_App::installApp($appId);
if($result !== false) { 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))); OC_JSON::success(array('data' => array('appid' => $appId)));
} else { } else {
$l = \OC::$server->getL10N('settings'); $l = \OC::$server->getL10N('settings');

View File

@ -12,6 +12,9 @@ $appId = OC_App::cleanAppId($appId);
$result = OC_App::removeApp($appId); $result = OC_App::removeApp($appId);
if($result !== false) { 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))); OC_JSON::success(array('data' => array('appid' => $appId)));
} else { } else {
$l = \OC::$server->getL10N('settings'); $l = \OC::$server->getL10N('settings');

View File

@ -55,7 +55,8 @@ class Application extends App {
$c->query('AppName'), $c->query('AppName'),
$c->query('Request'), $c->query('Request'),
$c->query('L10N'), $c->query('L10N'),
$c->query('Config') $c->query('Config'),
$c->query('ICacheFactory')
); );
}); });
$container->registerService('SecuritySettingsController', function(IContainer $c) { $container->registerService('SecuritySettingsController', function(IContainer $c) {
@ -120,6 +121,9 @@ class Application extends App {
$container->registerService('Config', function(IContainer $c) { $container->registerService('Config', function(IContainer $c) {
return $c->query('ServerContainer')->getConfig(); return $c->query('ServerContainer')->getConfig();
}); });
$container->registerService('ICacheFactory', function(IContainer $c) {
return $c->query('ServerContainer')->getMemCacheFactory();
});
$container->registerService('L10N', function(IContainer $c) { $container->registerService('L10N', function(IContainer $c) {
return $c->query('ServerContainer')->getL10N('settings'); return $c->query('ServerContainer')->getL10N('settings');
}); });

View File

@ -14,6 +14,7 @@ namespace OC\Settings\Controller;
use OC\App\DependencyAnalyzer; use OC\App\DependencyAnalyzer;
use OC\App\Platform; use OC\App\Platform;
use \OCP\AppFramework\Controller; use \OCP\AppFramework\Controller;
use OCP\ICacheFactory;
use OCP\IRequest; use OCP\IRequest;
use OCP\IL10N; use OCP\IL10N;
use OCP\IConfig; use OCP\IConfig;
@ -27,20 +28,25 @@ class AppSettingsController extends Controller {
private $l10n; private $l10n;
/** @var IConfig */ /** @var IConfig */
private $config; private $config;
/** @var \OCP\ICache */
private $cache;
/** /**
* @param string $appName * @param string $appName
* @param IRequest $request * @param IRequest $request
* @param IL10N $l10n * @param IL10N $l10n
* @param IConfig $config * @param IConfig $config
* @param ICacheFactory $cache
*/ */
public function __construct($appName, public function __construct($appName,
IRequest $request, IRequest $request,
IL10N $l10n, IL10N $l10n,
IConfig $config) { IConfig $config,
ICacheFactory $cache) {
parent::__construct($appName, $request); parent::__construct($appName, $request);
$this->l10n = $l10n; $this->l10n = $l10n;
$this->config = $config; $this->config = $config;
$this->cache = $cache->create($appName);
} }
/** /**
@ -49,13 +55,16 @@ class AppSettingsController extends Controller {
*/ */
public function listCategories() { public function listCategories() {
$categories = array( if(!is_null($this->cache->get('listCategories'))) {
array('id' => 0, 'displayName' => (string)$this->l10n->t('Enabled') ), return $this->cache->get('listCategories');
array('id' => 1, 'displayName' => (string)$this->l10n->t('Not enabled') ), }
); $categories = [
['id' => 0, 'displayName' => (string)$this->l10n->t('Enabled')],
['id' => 1, 'displayName' => (string)$this->l10n->t('Not enabled')],
];
if($this->config->getSystemValue('appstoreenabled', true)) { 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 // apps from external repo via OCS
$ocs = \OC_OCSClient::getCategories(); $ocs = \OC_OCSClient::getCategories();
foreach($ocs as $k => $v) { foreach($ocs as $k => $v) {
@ -67,6 +76,7 @@ class AppSettingsController extends Controller {
} }
$categories['status'] = 'success'; $categories['status'] = 'success';
$this->cache->set('listCategories', $categories, 3600);
return $categories; return $categories;
} }
@ -77,44 +87,46 @@ class AppSettingsController extends Controller {
* @return array * @return array
*/ */
public function listApps($category = 0) { public function listApps($category = 0) {
$apps = array(); if(!is_null($this->cache->get('listApps-'.$category))) {
$apps = $this->cache->get('listApps-'.$category);
switch($category) { } else {
// installed apps switch ($category) {
case 0: // installed apps
$apps = \OC_App::listAllApps(true); case 0:
$apps = array_filter($apps, function($app) { $apps = \OC_App::listAllApps(true);
return $app['active']; $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';
}); });
} else { break;
$apps = \OC_App::getAppstoreApps('approved', $category); // not-installed apps
} case 1:
if (!$apps) { $apps = \OC_App::listAllApps(true);
$apps = array(); $apps = array_filter($apps, function ($app) {
} return !$app['active'];
usort($apps, function ($a, $b) { });
$a = (int)$a['score']; break;
$b = (int)$b['score']; default:
if ($a === $b) { if ($category === 2) {
return 0; $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);
} }
return ($a > $b) ? -1 : 1; if (!$apps) {
}); $apps = array();
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 // fix groups to be an array
@ -142,6 +154,8 @@ class AppSettingsController extends Controller {
return $app; return $app;
}, $apps); }, $apps);
return array('apps' => $apps, 'status' => 'success'); $this->cache->set('listApps-'.$category, $apps, 300);
return ['apps' => $apps, 'status' => 'success'];
} }
} }