From ce37d1eb9cea31820a898ba09a996b1f424807ce Mon Sep 17 00:00:00 2001 From: Joshua Seigler Date: Sat, 2 Jul 2016 10:04:28 -0400 Subject: [PATCH] improved cache duration --- views/chart.php | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/views/chart.php b/views/chart.php index f5419e3..6d92a7b 100644 --- a/views/chart.php +++ b/views/chart.php @@ -114,7 +114,11 @@ function renderChart( if(is_null($poloniexJson)) { $poloniexJson = getJson($poloniexUrl); // 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 = []; @@ -135,14 +139,14 @@ function renderChart( $im->clear(); $im->destroy(); } - CacheManager::set($chartCacheKey, $result, $dataResolution); - $resultExpires = time() + $dataResolution; + CacheManager::set($chartCacheKey, $result, $cacheTimeSeconds); + $resultExpires = time() + $cacheTimeSeconds; } else { $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') { 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"');