27 protected function key($item,$id,$user = null){
28 return implode($this->keySeperator,array_merge([$user ?: $this->
component(
'user')->
id,$item],is_array($id) ? $id : [$id]));
37 protected function filename($item,$id,$user = null){
38 return $this->path . $this->
key($item,$id ===
true ? $this->multiIndicator : $id,$user) .
$this->ext;
47 protected function find($item = null,$id = null,$user = null){
48 return glob($this->
filename($item ?:
'*',$id ?:
'*',$user),GLOB_NOSORT);
63 public function exists($item,$id = null,$user = null){
64 $time = time() - $this->timeout;
66 foreach($this->
find($item,$id,$user ?:
'*') as $lock)
67 if(
filemtime($lock) < $time) \Rsi\File::unlink($lock);
68 elseif(($user !==
false) || (\
Rsi\Str::part(basename($lock),$this->keySeperator) != $this->
component(
'user')->
id)) $count++;
77 public function request($item,$id = null){
78 $this->
component(
'alive')->register($this->_name);
79 $lock = $this->
filename($item,$id ?:
true);
82 if(!is_file($lock) && ($this->
exists($item,$id ?:
true,
false) || ($id && $this->
exists($item,
true)))) $lock =
false;
83 else $error = \Rsi\File::write($lock,null,0666) ===
false;
86 if($this->_fred->debug)
throw $e;
89 if($lock && ($error || ($this->
exists($item,$id) > 1) || ($id && $this->
exists($item,
true)))){
90 \Rsi\File::unlink($lock);
93 return $lock && is_file($lock);
101 public function refresh($item = null,$id = null){
102 $result = count($locks = $this->
find($item,$id)) > 0;
103 foreach($locks as $lock)
104 if(!touch($lock) || ($this->
exists($item,$id) > 1) || ($id && $this->
exists($item,
true))){
105 \Rsi\File::unlink($lock);
116 public function release($item = null,$id = null){
118 foreach($this->
find($item,$id) as $lock) $result &= \Rsi\File::unlink($lock);
127 public function owner($item,$id = null){
128 return ($filenames = $this->
find($item,$id,
'*') ?: ($id ? $this->
find($item,
true,
'*') : null))
129 ? \Rsi\Str::part(basename(array_pop($filenames)),$this->keySeperator)
134 if(!$this->_path) $this->_path = $this->
config(
'path') ?: \Rsi\File::tempDir();
139 if($this->_timeout === null) $this->_timeout = $this->
config(
'timeout') ?: $this->
component(
'alive')->interval * 120;
$ext
Extension for lock file.
exists($item, $id=null, $user=null)
Check wether a lock exists.
find($item=null, $id=null, $user=null)
Find lock files.
config($key, $default=null)
Retrieve a config value.
key($item, $id, $user=null)
Generate a lock key.
refresh($item=null, $id=null)
Refresh one or multiple lock(s).
filemtime($filename)
Filemtime with session cache.
owner($item, $id=null)
Who owns a lock.
$multiIndicator
String to indicate a lock for all ID's of an item type.
$keySeperator
String to seperate key info with (may not be present in item type or ID).
release($item=null, $id=null)
Release one or multiple lock(s).
request($item, $id=null)
Request a lock on an item.
$_timeout
Number of seconds after which a lock expires (empty = use double of.
$_path
Path to store the lock files (temp path if empty).
component($name)
Get a component (local or default).
filename($item, $id, $user=null)
Filename for a lock file.