summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorUrban Müller2020-07-27 16:48:43 +0200
committerUrban Müller2020-07-27 16:48:43 +0200
commitba63582f143a5eae9709119bb8cafb365643b36f (patch)
treef0603acc9b7b0778262be56a1f17bbd817289b86
parent7f8317ac9f5bff192e6355ef1be1533b0726ef05 (diff)
downloaditools-ba63582f143a5eae9709119bb8cafb365643b36f.tar.gz
itools-ba63582f143a5eae9709119bb8cafb365643b36f.tar.bz2
itools-ba63582f143a5eae9709119bb8cafb365643b36f.zip
make locking in it_url::get_cache optional
-rw-r--r--it_url.class18
1 files changed, 11 insertions, 7 deletions
diff --git a/it_url.class b/it_url.class
index 9b84996..6fcc941 100644
--- a/it_url.class
+++ b/it_url.class
@@ -501,6 +501,7 @@ static function get_cache_filename($p)
* @param $p['keepfailed'] keep old versions of files if download fails (sending alerts conservatively)
* @param $p['returnheaders'] Return array($path, $headers) instead of simply $path
* @param $p['postprocess'] NOT SUPPORTED, use ::get_cache_contents
+ * @param $p['lock'] prevent multiple requests to same url from different processes [true]
* @return Cache filename or false if fetch failed
*/
static function get_cache($p = array())
@@ -523,7 +524,7 @@ static function get_cache($p = array())
{
$fileexists = $filemtime !== true;
- if ($lock = it_url::_lock($path))
+ if ($lock = it_url::_lock($path, $p))
{
# Touch existing file to prevent locking other getters while refreshing
if ($fileexists)
@@ -542,7 +543,7 @@ static function get_cache($p = array())
else
@unlink($path); # Expired and failed to get
- it_url::_unlock($path, $lock);
+ it_url::_unlock($path, $lock, $p);
}
else
{
@@ -675,10 +676,10 @@ static function _expired($path, $maxage)
* @param $path File to lock
* @return Lock handle if successfully locked file
*/
-static function _lock($path)
+static function _lock($path, $p)
{
$force = EDC('nocache') || (($mtime = @filemtime("$path.lock")) && (time() - $mtime > 30)); # expire forgotten locks
- return @it::fopen("$path.lock", $force ? "w" : "x");
+ return ($p['lock'] ?? true) ? @it::fopen("$path.lock", $force ? "w" : "x") : true;
}
/**
@@ -686,10 +687,13 @@ static function _lock($path)
* @param $path File to unlock
* @param $lock Handle to lock acquird by _lock
*/
-static function _unlock($path, $lock)
+static function _unlock($path, $lock, $p)
{
- fclose($lock);
- @unlink("$path.lock");
+ if ($p['lock'] ?? true)
+ {
+ fclose($lock);
+ @unlink("$path.lock");
+ }
}
/**