Skip to content

Commit

Permalink
Looks like we need separate method
Browse files Browse the repository at this point in the history
  • Loading branch information
arvenil committed Jun 12, 2015
1 parent 3f0b3c5 commit d4f516f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/NinjaMutex/Lock/LockExpirationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,10 @@ interface LockExpirationInterface
* @param int $expiration Expiration time of the lock in seconds.
*/
public function setExpiration($expiration);

/**
* @param string $name
* @return bool
*/
public function clearLock($name);
}
19 changes: 18 additions & 1 deletion src/NinjaMutex/Lock/MemcacheLock.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,23 @@ public function setExpiration($expiration)
$this->expiration = $expiration;
}

/**
* Clear lock without releasing it
* Do not use this method unless you know what you do
*
* @param string $name name of lock
* @return bool
*/
public function clearLock($name)
{
if (!isset($this->locks[$name])) {
return false;
}

unset($this->locks[$name]);
return true;
}

/**
* @param string $name name of lock
* @param bool $blocking
Expand All @@ -83,7 +100,7 @@ protected function getLock($name, $blocking)
*/
public function releaseLock($name)
{
if (isset($this->locks[$name]) && ($this->memcache->delete($name) || !$this->isLocked($name))) {
if (isset($this->locks[$name]) && $this->memcache->delete($name)) {
unset($this->locks[$name]);

return true;
Expand Down
19 changes: 18 additions & 1 deletion src/NinjaMutex/Lock/MemcachedLock.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,23 @@ public function setExpiration($expiration)
$this->expiration = $expiration;
}

/**
* Clear lock without releasing it
* Do not use this method unless you know what you do
*
* @param string $name name of lock
* @return bool
*/
public function clearLock($name)
{
if (!isset($this->locks[$name])) {
return false;
}

unset($this->locks[$name]);
return true;
}

/**
* @param string $name name of lock
* @param bool $blocking
Expand All @@ -83,7 +100,7 @@ protected function getLock($name, $blocking)
*/
public function releaseLock($name)
{
if (isset($this->locks[$name]) && ($this->memcached->delete($name) || !$this->isLocked($name))) {
if (isset($this->locks[$name]) && $this->memcached->delete($name)) {
unset($this->locks[$name]);

return true;
Expand Down
4 changes: 3 additions & 1 deletion tests/NinjaMutex/Lock/LockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ public function testExpiration(LockFabricWithExpirationInterface $lockFabricWith

// Cleanup
$this->assertTrue($lockImplementor->releaseLock($name, 0));
$this->assertTrue($lockImplementorWithExpiration->releaseLock($name, 0));
// Expired lock is unusable, we need to clean it's lock state or otherwise
// it will invoke in __destruct Exception (php*) or Fatal Error (hhvm)
$this->assertTrue($lockImplementorWithExpiration->clearLock($name, 0));
}
}

0 comments on commit d4f516f

Please sign in to comment.