lbry.com/lib/tools/CurlWithCache.class.php
2016-10-13 16:50:50 -04:00

32 lines
No EOL
763 B
PHP

<?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('x' . $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;
}
}