From 8ba817478bd1700ec40172b3fdbca9b5dd37314d Mon Sep 17 00:00:00 2001 From: LogMANOriginal Date: Wed, 14 Mar 2018 18:06:36 +0100 Subject: [PATCH] Implement customizable cache timeout (#641) * [BridgeAbstract] Implement customizable cache timeout The customizable cache timeout is used instead of the default cache timeout (CACHE_TIMEOUT) if specified by the caller. * [index] Add new global parameter '_cache_timeout' The _cache_timeout parameter is an optional parameter that can be used to specify a custom cache timeout. This option is enabled by default. It can be disabled using the named constant 'CUSTOM_CACHE_TIMEOUT' which supports two states: > true: Enabled (default) > false: Disabled * [BridgeAbstract] Change scope of 'getCacheTimeout' to public * [html] Add cache timeout parameter to all bridges The timeout parameter only shows if CUSTOM_CACHE_TIMEOUT has been set to true. The default value is automatically set to the value specified in the bridge. * [index] Disable custom cache timeout by default --- index.php | 16 ++++++++++++++++ lib/BridgeAbstract.php | 16 +++++++++++++++- lib/BridgeInterface.php | 16 ++++++++++++++++ lib/html.php | 35 ++++++++++++++++++++++++++++++++++- 4 files changed, 81 insertions(+), 2 deletions(-) diff --git a/index.php b/index.php index 851f0f92..97a76450 100644 --- a/index.php +++ b/index.php @@ -19,6 +19,10 @@ define('PROXY_BYBRIDGE', false); // Comment this line or keep PROXY_NAME empty to display PROXY_URL instead define('PROXY_NAME', 'Hidden Proxy Name'); +// Allows the operator to specify custom cache timeouts via '&_cache_timeout=3600' +// true: enabled, false: disabled (default) +define('CUSTOM_CACHE_TIMEOUT', false); + date_default_timezone_set('UTC'); error_reporting(0); @@ -168,6 +172,16 @@ try { define('NOPROXY', true); } + // Custom cache timeout + $cache_timeout = -1; + if(array_key_exists('_cache_timeout', $params)) { + if(!CUSTOM_CACHE_TIMEOUT) { + throw new \HttpException('This server doesn\'t support "_cache_timeout"!'); + } + + $cache_timeout = filter_var($params['_cache_timeout'], FILTER_VALIDATE_INT); + } + // Initialize cache $cache = Cache::create('FileCache'); $cache->setPath(CACHE_DIR); @@ -178,10 +192,12 @@ try { unset($params['bridge']); unset($params['format']); unset($params['_noproxy']); + unset($params['_cache_timeout']); // Load cache & data try { $bridge->setCache($cache); + $bridge->setCacheTimeout($cache_timeout); $bridge->setDatas($params); } catch(Exception $e) { http_response_code($e->getCode()); diff --git a/lib/BridgeAbstract.php b/lib/BridgeAbstract.php index cd2ff632..0bd1c7c7 100644 --- a/lib/BridgeAbstract.php +++ b/lib/BridgeAbstract.php @@ -14,6 +14,7 @@ abstract class BridgeAbstract implements BridgeInterface { protected $items = array(); protected $inputs = array(); protected $queriedContext = ''; + protected $cacheTimeout; /** * Return cachable datas (extrainfos and items) stored in the bridge @@ -171,7 +172,7 @@ abstract class BridgeAbstract implements BridgeInterface { if(!is_null($this->cache)) { $time = $this->cache->getTime(); if($time !== false - && (time() - static::CACHE_TIMEOUT < $time) + && (time() - $this->getCacheTimeout() < $time) && (!defined('DEBUG') || DEBUG !== true)) { $cached = $this->cache->loadData(); if(isset($cached['items']) && isset($cached['extraInfos'])) { @@ -268,4 +269,17 @@ abstract class BridgeAbstract implements BridgeInterface { public function setCache(\CacheInterface $cache){ $this->cache = $cache; } + + public function setCacheTimeout($timeout){ + if(is_numeric($timeout) && ($timeout < 1 || $timeout > 86400)) { + $this->cacheTimeout = static::CACHE_TIMEOUT; + return; + } + + $this->cacheTimeout = $timeout; + } + + public function getCacheTimeout(){ + return isset($this->cacheTimeout) ? $this->cacheTimeout : static::CACHE_TIMEOUT; + } } diff --git a/lib/BridgeInterface.php b/lib/BridgeInterface.php index a4add2c9..b8f5cf47 100644 --- a/lib/BridgeInterface.php +++ b/lib/BridgeInterface.php @@ -68,4 +68,20 @@ interface BridgeInterface { * @param object CacheInterface The cache instance */ public function setCache(\CacheInterface $cache); + + /** + * Sets the timeout for clearing the cache files. The timeout must be + * specified between 1..86400 seconds (max. 24 hours). The default timeout + * (specified by the bridge maintainer) applies for invalid values. + * + * @param int $timeout The cache timeout in seconds + */ + public function setCacheTimeout($timeout); + + /** + * Returns the cache timeout + * + * @return int Cache timeout + */ + public function getCacheTimeout(); } diff --git a/lib/html.php b/lib/html.php index 4c6c3203..eec47ae0 100644 --- a/lib/html.php +++ b/lib/html.php @@ -75,8 +75,24 @@ CARD; . ((defined('PROXY_NAME') && PROXY_NAME) ? PROXY_NAME : PROXY_URL) . ')
' . PHP_EOL; - } + } if(CUSTOM_CACHE_TIMEOUT) { + $idArg = 'arg-' + . urlencode($bridgeName) + . '-' + . urlencode('_cache_timeout'); + $card .= '' + . PHP_EOL; + + $card .= '
' + . PHP_EOL; + } $card .= $getHelperButtonsFormat($formats); } else { $card .= 'Inactive'; @@ -251,6 +267,23 @@ CARD; . ((defined('PROXY_NAME') && PROXY_NAME) ? PROXY_NAME : PROXY_URL) . ')
' . PHP_EOL; + } if(CUSTOM_CACHE_TIMEOUT) { + $idArg = 'arg-' + . urlencode($bridgeName) + . '-' + . urlencode('_cache_timeout'); + + $card .= '' + . PHP_EOL; + + $card .= '
' + . PHP_EOL; } $card .= $getHelperButtonsFormat($formats); } else {