Skip to content

Instantly share code, notes, and snippets.

@xergio
Created November 22, 2011 08:49
Show Gist options
  • Save xergio/1385221 to your computer and use it in GitHub Desktop.
Save xergio/1385221 to your computer and use it in GitHub Desktop.
class Lock
<?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;
}
}
@xergio
Copy link
Author

xergio commented Nov 22, 2011

Esta clase tiene dependencias como el MemoryCache.php, Net.php o el CustomException.php

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment