Skip to content

Commit

Permalink
Fix locking.
Browse files Browse the repository at this point in the history
  • Loading branch information
yunosh committed Jul 1, 2016
1 parent 7239977 commit 1d52447
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion framework/Memcache/lib/Horde/Memcache.php
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ public function lock($key)
{
$i = 0;

while ($this->_memcache->add($this->_key($key . self::LOCK_SUFFIX), 1, self::LOCK_TIMEOUT) === false) {
while ($this->_lockAdd($key) === false) {
usleep(min(pow(2, $i++) * 10000, 100000));
}

Expand All @@ -455,6 +455,24 @@ public function lock($key)
$this->_locks[$key] = true;
}

/**
* Small wrapper around Memcache[d]#add().
*
* @param string $key The key to lock.
*/
protected function _lockAdd($key)
{
if ($this->_memcache instanceof Memcached) {
$this->_memcache->add(
$this->_key($key . self::LOCK_SUFFIX), 1, self::LOCK_TIMEOUT
);
} else {
$this->_memcache->add(
$this->_key($key . self::LOCK_SUFFIX), 1, 0, self::LOCK_TIMEOUT
);
}
}

/**
* Release lock on a key.
*
Expand Down

0 comments on commit 1d52447

Please sign in to comment.