From 8df66b1d1b2407b16c9ee465a877c214b69a5da0 Mon Sep 17 00:00:00 2001 From: Christian Schneider Date: Wed, 29 Jul 2020 15:41:46 +0200 Subject: Simplify _lock/_unlock by testing for lock parameter in caller --- it_url.class | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/it_url.class b/it_url.class index cc38fff..d700b7e 100644 --- a/it_url.class +++ b/it_url.class @@ -514,7 +514,7 @@ static function get_cache($p = array()) if (!$p['id'] && $p['maxage']) it::error("calling get_cache with maxage and without id"); - $p += ['timeout' => 10, 'maxage' => 86400, 'cleanbefore' => 7200, 'it_error' => $p['safety'] == 0 ? false : ($p['safety'] == 2 ? ['fatal' => true] : [])]; + $p += ['timeout' => 10, 'maxage' => 86400, 'cleanbefore' => 7200, 'lock' => true, 'it_error' => $p['safety'] == 0 ? false : ($p['safety'] == 2 ? ['fatal' => true] : [])]; $p['totaltimeout'] = $p['timeout']; $path = it_url::get_cache_filename($p); # Must be before changing cachedir below $p['cachedir'] = it_url::get_cache_dir($p); @@ -529,7 +529,7 @@ static function get_cache($p = array()) { $fileexists = $filemtime !== true; - if ($lock = it_url::_lock($path, $p)) + if ($lock = !$p['lock'] ?: it_url::_lock($path)) { # Touch existing file to prevent locking other getters while refreshing if ($fileexists) @@ -548,7 +548,7 @@ static function get_cache($p = array()) else @unlink($path); # Expired and failed to get - it_url::_unlock($path, $lock, $p); + it_url::_unlock($path, $lock); } else { @@ -580,7 +580,7 @@ static function get_cache($p = array()) if ($filemtime = $newfile ? true : it_url::_expired($path, $p['maxage'])) # Outdated(non-zero int) or non-existant(true)? { - if ($lock = it_url::_lock($path, $p)) + if ($lock = !$p['lock'] ?: it_url::_lock($path)) { # Touch existing file to prevent locking other getters while refreshing if ($filemtime !== true) @@ -603,7 +603,7 @@ static function get_cache($p = array()) } $cachemiss = 1; - it_url::_unlock($path, $lock, $p); + it_url::_unlock($path, $lock); } else { @@ -681,10 +681,10 @@ static function _expired($path, $maxage) * @param $path File to lock * @return Lock handle if successfully locked file */ -static function _lock($path, $p = []) +static function _lock($path) { $force = EDC('nocache') || (($mtime = @filemtime("$path.lock")) && (time() - $mtime > 30)); # expire forgotten locks - return ($p['lock'] ?? true) ? @it::fopen("$path.lock", $force ? "w" : "x") : true; + return @it::fopen("$path.lock", $force ? "w" : "x"); } /** @@ -692,9 +692,9 @@ static function _lock($path, $p = []) * @param $path File to unlock * @param $lock Handle to lock acquird by _lock */ -static function _unlock($path, $lock, $p = []) +static function _unlock($path, $lock) { - if ($p['lock'] ?? true) + if (is_resource($lock)) { fclose($lock); @unlink("$path.lock"); -- cgit v1.2.3