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
{
protected static $curlOptions = ['json_response' => true, 'cache' => true];
protected static $curlOptions = ['json_response' => true, 'cache' => true, 'timeout' => 10];
public static function listRoadmapTasks($cache = true)
{
@ -16,7 +16,6 @@ class Asana
158602294500249 => ['Documentation', null],
];
$groupCategories = ['ongoing', 'upcoming'];
$tasks = [];
$tags = [
@ -25,30 +24,30 @@ class Asana
192699565737948 => 'Future'
];
foreach($tags as $tagId => $tagLabel)
foreach ($tags as $tagId => $tagLabel)
{
$taggedTasks = static::get('/tags/' . $tagId . '/tasks', ['completed_since' => 'now'], $cache);
foreach ($taggedTasks as $task)
{
$fullTask = static::get('/tasks/' . $task['id']);
$fullTask = static::get('/tasks/' . $task['id']);
$projectId = $fullTask['memberships'][0]['project']['id'] ?? null;
if ($fullTask['name'] && $projectId && isset($projects[$projectId]))
{
list($projectName, $projectUrl) = $projects[$projectId];
$tasks[$tagLabel][] = array_intersect_key($fullTask, ['name' => null]) + [
'project_label' => $projectName,
'badge' => $projectName,
'date' => $fullTask['due_on'] ?? null,
'body' => $fullTask['notes'],
'url' => $projectUrl,
'group' => $tagLabel,
'assignee' => $fullTask['assignee'] ? ucwords($fullTask['assignee']['name']) : ''
];
'badge' => $projectName,
'date' => $fullTask['due_on'] ?? null,
'body' => $fullTask['notes'],
'url' => $projectUrl,
'group' => $tagLabel,
'assignee' => $fullTask['assignee'] ? ucwords($fullTask['assignee']['name']) : ''
];
}
}
}
foreach($tasks as &$groupTasks)
foreach ($tasks as &$groupTasks)
{
usort($groupTasks, function ($tA, $tB)
{
@ -72,12 +71,12 @@ class Asana
$apiKey = Config::get('asana_key');
$options = [
'headers' => ['Authorization: Bearer ' . $apiKey],
'cache' => $cache
] + static::$curlOptions;
'headers' => ['Authorization: Bearer ' . $apiKey],
'cache' => $cache
] + static::$curlOptions;
$responseData = Curl::get('https://app.asana.com/api/1.0' . $endpoint, $data, $options);
return isset($responseData['data']) ? $responseData['data'] : [];
$responseData = CurlWithCache::get('https://app.asana.com/api/1.0' . $endpoint, $data, $options);
return $responseData['data'] ?? [];
}
}

View file

@ -33,7 +33,7 @@ class Github
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)

View file

@ -2,10 +2,12 @@
class Curl
{
const GET = 'GET',
POST = 'POST',
PUT = 'PUT',
DEFAULT_CACHE = 600000;
const
GET = 'GET',
POST = 'POST',
PUT = 'PUT',
DELETE = 'DELETE';
public static function get($url, $params = [], $options = [])
{
@ -25,13 +27,18 @@ class Curl
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 = [])
{
$defaults = [
'cache' => false,
'headers' => [],
'verify' => true,
'timeout' => 10, //5 was timing out on /roadmap for Asana API
'timeout' => 5,
'follow_redirects' => true,
'user_agent' => null,
'proxy' => null,
@ -39,7 +46,7 @@ class Curl
'cookie' => null,
'json_data' => false,
'json_response' => false,
'retry' => false
'retry' => false,
];
$invalid = array_diff_key($options, $defaults);
@ -55,21 +62,6 @@ class Curl
$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
{
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);
$response = [$statusCode, $headers, $responseContent];
if ($options['cache'])
{
apc_store($cacheKey, $response, is_numeric($options['cache']) ? $options['cache'] : static::DEFAULT_CACHE);
}
return $response;
return [$statusCode, $headers, $responseContent];
}
}

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;
}
}