Created
November 22, 2011 08:49
-
-
Save xergio/1385221 to your computer and use it in GitHub Desktop.
class Lock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Lock { | |
private $mc; | |
private $key; | |
function __construct($key=null, $expire=300) { | |
$this->mc = MemoryCache::getInstance($expire); | |
$this->key = is_null($key)? "mv_lock_". rand(1000, 9999): $key; | |
} | |
function acquire() { | |
if ($this->check()) { | |
return null; | |
} | |
return $this->mc->set($this->key, true); | |
} | |
function release() { | |
return $this->mc->delete($this->key); | |
} | |
function check() { | |
if (!Net::isControlF5() and (($result = $this->mc->get($this->key)) !== false)) { | |
throw new LockedException("LOCKED"); | |
} | |
return $result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Esta clase tiene dependencias como el MemoryCache.php, Net.php o el CustomException.php