Skip to content

Commit

Permalink
refs #4053 cache requests to marketplace in filesystem
Browse files Browse the repository at this point in the history
  • Loading branch information
tsteur committed Sep 19, 2013
1 parent 57385d5 commit 69c7462
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 38 deletions.
25 changes: 0 additions & 25 deletions plugins/CorePluginsAdmin/Marketplace.php
Expand Up @@ -24,8 +24,6 @@ class Marketplace
* @var MarketplaceApiClient
*/
private $client;
private static $pluginUpdates = null;
private static $themeUpdates = null;

public function __construct()
{
Expand Down Expand Up @@ -73,29 +71,6 @@ private function hasPluginUpdate($plugin)
* @return array
*/
public function getPluginsHavingUpdate($themesOnly)
{
if ($themesOnly && !is_null(static::$themeUpdates)) {
return static::$themeUpdates;
} else if (!$themesOnly && !is_null(static::$pluginUpdates)) {
return static::$pluginUpdates;
}

$pluginsHavingUpdate = $this->fetchPluginsHavingUpdate($themesOnly);

if ($themesOnly) {
static::$themeUpdates = $pluginsHavingUpdate;
} else {
static::$pluginUpdates = $pluginsHavingUpdate;
}

return $pluginsHavingUpdate;
}

/**
* @param $themesOnly
* @return array
*/
public function fetchPluginsHavingUpdate($themesOnly)
{
$pluginManager = PluginsManager::getInstance();
$loadedPlugins = $pluginManager->getLoadedPlugins();
Expand Down
51 changes: 38 additions & 13 deletions plugins/CorePluginsAdmin/MarketplaceApiClient.php
Expand Up @@ -9,7 +9,9 @@
* @package CorePluginsAdmin
*/
namespace Piwik\Plugins\CorePluginsAdmin;
use Piwik\CacheFile;
use Piwik\Http;
use Piwik\Tracker\Cache;

/**
*
Expand All @@ -20,18 +22,27 @@ class MarketplaceApiClient
private $domain = 'http://plugins.piwik.org/';

/**
* @var array array(pluginName => stdClass pluginInfo)
* @var CacheFile
*/
private static $pluginCache = array();
private $cache = null;

private function fetch($action, $params)
public function __construct()
{
$endpoint = $this->domain . '/api/1.0/';
$query = http_build_query($params);
$this->cache = new CacheFile('marketplace', 7200);
}

$url = sprintf('%s%s?%s', $endpoint, $action, $query);
private function fetch($action, $params)
{
$query = http_build_query($params);
$result = $this->getCachedResult($action, $query);

if (false === $result) {
$endpoint = $this->domain . '/api/1.0/';
$url = sprintf('%s%s?%s', $endpoint, $action, $query);
$result = Http::sendHttpRequest($url, 5);
$this->cacheResult($action, $query, $result);
}

$result = Http::sendHttpRequest($url, 5);
$result = json_decode($result);

if (!empty($result->error)) {
Expand All @@ -42,16 +53,30 @@ private function fetch($action, $params)
return $result;
}

public function getPluginInfo($name)
private function getCachedResult($action, $query)
{
if (array_key_exists($name, static::$pluginCache)) {
return static::$pluginCache[$name];
}
$cacheKey = $this->getCacheKey($action, $query);

return $this->cache->get($cacheKey);
}

private function cacheResult($action, $query, $result)
{
$cacheKey = $this->getCacheKey($action, $query);

$this->cache->set($cacheKey, $result);
}

private function getCacheKey($action, $query)
{
return sprintf('api.1.0.%s.%s', str_replace('/', '.', $action), md5($query));
}

public function getPluginInfo($name)
{
$action = sprintf('plugins/%s/info', $name);
static::$pluginCache[$name] = $this->fetch($action, array());

return static::$pluginCache[$name];
return $this->fetch($action, array());
}

public function download($pluginOrThemeName, $target)
Expand Down

0 comments on commit 69c7462

Please sign in to comment.