improved cache duration

This commit is contained in:
Joshua Seigler 2016-07-02 10:04:28 -04:00
parent 25cb66b6e8
commit ce37d1eb9c

View file

@ -114,7 +114,11 @@ function renderChart(
if(is_null($poloniexJson)) { if(is_null($poloniexJson)) {
$poloniexJson = getJson($poloniexUrl); $poloniexJson = getJson($poloniexUrl);
// Write to cache for next time // Write to cache for next time
CacheManager::set('poloniex-json-'.$pair.'-'.$dataDuration, $poloniexJson, $dataResolution); // Expires either in a minute, or 60s after the next data point is supposed to be available
$cacheTimeSeconds = max(60, end($poloniexJson)->date + $dataResolution - time() + 60);
CacheManager::set('poloniex-json-'.$pair.'-'.$dataDuration, $poloniexJson, $cacheTimeSeconds);
} else {
$cacheTimeSeconds = max(60, end($poloniexJson)->date + $dataResolution - time() + 60);
} }
$chartData = []; $chartData = [];
@ -135,14 +139,14 @@ function renderChart(
$im->clear(); $im->clear();
$im->destroy(); $im->destroy();
} }
CacheManager::set($chartCacheKey, $result, $dataResolution); CacheManager::set($chartCacheKey, $result, $cacheTimeSeconds);
$resultExpires = time() + $dataResolution; $resultExpires = time() + $cacheTimeSeconds;
} else { } else {
$resultExpires = CacheManager::getInfo($chartCacheKey)[ 'expired_time' ]; $resultExpires = CacheManager::getInfo($chartCacheKey)[ 'expired_time' ];
$startTime = $resultExpires - $dataResolution; $startTime = $resultExpires - $dataDuration;
} }
header('Expires: '.gmdate('D, d M Y H:i:s', $resultExpires)); header('Expires: '.gmdate(DateTime::RFC1123, $resultExpires));
if ($format == 'svg') { if ($format == 'svg') {
header('Content-type: image/svg+xml; charset=utf-8'); header('Content-type: image/svg+xml; charset=utf-8');
header('Content-Disposition: inline; filename="Dash-chart-' . gmdate('Y-m-d\THis+0', $startTime) . '--' . gmdate('Y-m-d\THis+0') . '.svg"'); header('Content-Disposition: inline; filename="Dash-chart-' . gmdate('Y-m-d\THis+0', $startTime) . '--' . gmdate('Y-m-d\THis+0') . '.svg"');