break out curl caching into separate class

This commit is contained in:
Alex Grintsvayg 2016-10-12 11:31:52 -04:00
parent db5eb7e88a
commit ff49e3e1fd
4 changed files with 64 additions and 48 deletions

View file

@ -2,7 +2,7 @@
class Asana class Asana
{ {
protected static $curlOptions = ['json_response' => true, 'cache' => true]; protected static $curlOptions = ['json_response' => true, 'cache' => true, 'timeout' => 10];
public static function listRoadmapTasks($cache = true) public static function listRoadmapTasks($cache = true)
{ {
@ -16,7 +16,6 @@ class Asana
158602294500249 => ['Documentation', null], 158602294500249 => ['Documentation', null],
]; ];
$groupCategories = ['ongoing', 'upcoming'];
$tasks = []; $tasks = [];
$tags = [ $tags = [
@ -25,7 +24,7 @@ class Asana
192699565737948 => 'Future' 192699565737948 => 'Future'
]; ];
foreach($tags as $tagId => $tagLabel) foreach ($tags as $tagId => $tagLabel)
{ {
$taggedTasks = static::get('/tags/' . $tagId . '/tasks', ['completed_since' => 'now'], $cache); $taggedTasks = static::get('/tags/' . $tagId . '/tasks', ['completed_since' => 'now'], $cache);
foreach ($taggedTasks as $task) foreach ($taggedTasks as $task)
@ -48,7 +47,7 @@ class Asana
} }
} }
foreach($tasks as &$groupTasks) foreach ($tasks as &$groupTasks)
{ {
usort($groupTasks, function ($tA, $tB) usort($groupTasks, function ($tA, $tB)
{ {
@ -76,8 +75,8 @@ class Asana
'cache' => $cache 'cache' => $cache
] + static::$curlOptions; ] + static::$curlOptions;
$responseData = Curl::get('https://app.asana.com/api/1.0' . $endpoint, $data, $options); $responseData = CurlWithCache::get('https://app.asana.com/api/1.0' . $endpoint, $data, $options);
return isset($responseData['data']) ? $responseData['data'] : []; return $responseData['data'] ?? [];
} }
} }

View file

@ -33,7 +33,7 @@ class Github
public static function get($endpoint, $cache = true) public static function get($endpoint, $cache = true)
{ {
return Curl::get('https://api.github.com' . $endpoint, [], ['user_agent' => 'LBRY', 'json_response' => true, 'cache' => $cache]); return CurlWithCache::get('https://api.github.com' . $endpoint, [], ['user_agent' => 'LBRY', 'json_response' => true, 'cache' => $cache]);
} }
public static function listRoadmapChangesets($cache = true) public static function listRoadmapChangesets($cache = true)

View file

@ -2,10 +2,12 @@
class Curl class Curl
{ {
const GET = 'GET', const
GET = 'GET',
POST = 'POST', POST = 'POST',
PUT = 'PUT', PUT = 'PUT',
DEFAULT_CACHE = 600000; DELETE = 'DELETE';
public static function get($url, $params = [], $options = []) public static function get($url, $params = [], $options = [])
{ {
@ -25,13 +27,18 @@ class Curl
return $body; return $body;
} }
public static function delete($url, $params = [], $options = [])
{
list ($status, $headers, $body) = static::doCurl(static::DELETE, $url, $params, $options);
return $body;
}
public static function doCurl($method, $url, $params = [], $options = []) public static function doCurl($method, $url, $params = [], $options = [])
{ {
$defaults = [ $defaults = [
'cache' => false,
'headers' => [], 'headers' => [],
'verify' => true, 'verify' => true,
'timeout' => 10, //5 was timing out on /roadmap for Asana API 'timeout' => 5,
'follow_redirects' => true, 'follow_redirects' => true,
'user_agent' => null, 'user_agent' => null,
'proxy' => null, 'proxy' => null,
@ -39,7 +46,7 @@ class Curl
'cookie' => null, 'cookie' => null,
'json_data' => false, 'json_data' => false,
'json_response' => false, 'json_response' => false,
'retry' => false 'retry' => false,
]; ];
$invalid = array_diff_key($options, $defaults); $invalid = array_diff_key($options, $defaults);
@ -55,21 +62,6 @@ class Curl
$options = array_merge($defaults, $options); $options = array_merge($defaults, $options);
if (!Apc::isEnabled())
{
$options['cache'] = false;
}
if ($options['cache'])
{
$cacheKey = md5('z' . $url . $method . serialize($options) . serialize($params));
$cachedData = apc_fetch($cacheKey);
if ($cachedData)
{
return $cachedData;
}
}
if ($options['headers'] && $options['headers'] !== array_values($options['headers'])) // associative array if ($options['headers'] && $options['headers'] !== array_values($options['headers'])) // associative array
{ {
throw new DomainException('Headers must not be an associative array. Its a simple array with values of the form "Header: value"'); throw new DomainException('Headers must not be an associative array. Its a simple array with values of the form "Header: value"');
@ -180,14 +172,7 @@ class Curl
curl_close($ch); curl_close($ch);
$response = [$statusCode, $headers, $responseContent]; return [$statusCode, $headers, $responseContent];
if ($options['cache'])
{
apc_store($cacheKey, $response, is_numeric($options['cache']) ? $options['cache'] : static::DEFAULT_CACHE);
}
return $response;
} }
} }

View file

@ -0,0 +1,32 @@
<?php
class CurlWithCache extends Curl
{
const DEFAULT_CACHE = 600000;
public static function doCurl($method, $url, $params = [], $options = [])
{
$useCache = ($options['cache'] ?? true) && Apc::isEnabled();
$cacheTimeout = is_numeric($options['cache'] ?? null) ? $options['cache'] : static::DEFAULT_CACHE;
unset($options['cache']);
if ($useCache)
{
$cacheKey = md5('z' . $url . $method . serialize($options) . serialize($params));
$cachedData = apc_fetch($cacheKey);
if ($cachedData)
{
return $cachedData;
}
}
$response = parent::doCurl($method, $url, $params, $options);
if ($useCache)
{
apc_store($cacheKey, $response, $cacheTimeout);
}
return $response;
}
}