-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from arvenil/issue12_add_expire_for_memcache
#12: Add expiration time for memcache lock storage
- Loading branch information
Showing
15 changed files
with
374 additions
and
140 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
extension=memcache.so | ||
extension=memcached.so |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
/** | ||
* This file is part of ninja-mutex. | ||
* | ||
* (C) Kamil Dziedzic <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
namespace NinjaMutex\Lock; | ||
|
||
/** | ||
* Lock implementor | ||
* | ||
* @author Kamil Dziedzic <[email protected]> | ||
*/ | ||
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); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,13 +16,107 @@ | |
* | ||
* @author Kamil Dziedzic <[email protected]> | ||
*/ | ||
class MemcacheLock extends MemcacheLockAbstract | ||
class MemcacheLock extends LockAbstract implements LockExpirationInterface | ||
{ | ||
/** | ||
* Maximum expiration time in seconds (30 days) | ||
* http://php.net/manual/en/memcache.add.php | ||
*/ | ||
const MAX_EXPIRATION = 2592000; | ||
|
||
/** | ||
* Memcache connection | ||
* | ||
* @var Memcache | ||
*/ | ||
protected $memcache; | ||
|
||
/** | ||
* @var int Expiration time of the lock in seconds | ||
*/ | ||
protected $expiration = 0; | ||
|
||
/** | ||
* @param Memcache $memcache | ||
*/ | ||
public function __construct(Memcache $memcache) | ||
{ | ||
parent::__construct($memcache); | ||
parent::__construct(); | ||
|
||
$this->memcache = $memcache; | ||
} | ||
|
||
/** | ||
* @param int $expiration Expiration time of the lock in seconds. If it's equal to zero (default), the lock will never expire. | ||
* Max 2592000s (30 days), if greater it will be capped to 2592000 without throwing an error. | ||
* WARNING: Using value higher than 0 may lead to race conditions. If you set too low expiration time | ||
* e.g. 30s and critical section will run for 31s another process will gain lock at the same time, | ||
* leading to unpredicted behaviour. Use with caution. | ||
*/ | ||
public function setExpiration($expiration) | ||
{ | ||
if ($expiration > static::MAX_EXPIRATION) { | ||
$expiration = static::MAX_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 | ||
* @return bool | ||
*/ | ||
protected function getLock($name, $blocking) | ||
{ | ||
if (!$this->memcache->add($name, serialize($this->getLockInformation()), 0, $this->expiration)) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Release lock | ||
* | ||
* @param string $name name of lock | ||
* @return bool | ||
*/ | ||
public function releaseLock($name) | ||
{ | ||
if (isset($this->locks[$name]) && $this->memcache->delete($name)) { | ||
unset($this->locks[$name]); | ||
|
||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Check if lock is locked | ||
* | ||
* @param string $name name of lock | ||
* @return bool | ||
*/ | ||
public function isLocked($name) | ||
{ | ||
return false !== $this->memcache->get($name); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,13 +16,108 @@ | |
* | ||
* @author Kamil Dziedzic <[email protected]> | ||
*/ | ||
class MemcachedLock extends MemcacheLockAbstract | ||
class MemcachedLock extends LockAbstract implements LockExpirationInterface | ||
{ | ||
/** | ||
* Maximum expiration time in seconds (30 days) | ||
* http://php.net/manual/en/memcached.add.php | ||
*/ | ||
const MAX_EXPIRATION = 2592000; | ||
|
||
/** | ||
* Memcache connection | ||
* | ||
* @var Memcached | ||
*/ | ||
protected $memcached; | ||
|
||
/** | ||
* @var int Expiration time of the lock in seconds | ||
*/ | ||
protected $expiration = 0; | ||
|
||
/** | ||
* @param Memcached $memcached | ||
*/ | ||
public function __construct(Memcached $memcached) | ||
public function __construct($memcached) | ||
{ | ||
parent::__construct(); | ||
|
||
$this->memcached = $memcached; | ||
} | ||
|
||
/** | ||
* @param int $expiration Expiration time of the lock in seconds. If it's equal to zero (default), the lock will never expire. | ||
* Max 2592000s (30 days), if greater it will be capped to 2592000 without throwing an error. | ||
* WARNING: Using value higher than 0 may lead to race conditions. If you set too low expiration time | ||
* e.g. 30s and critical section will run for 31s another process will gain lock at the same time, | ||
* leading to unpredicted behaviour. Use with caution. | ||
*/ | ||
public function setExpiration($expiration) | ||
{ | ||
if ($expiration > static::MAX_EXPIRATION) { | ||
$expiration = static::MAX_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) | ||
{ | ||
parent::__construct($memcached); | ||
if (!isset($this->locks[$name])) { | ||
return false; | ||
} | ||
|
||
unset($this->locks[$name]); | ||
return true; | ||
} | ||
|
||
/** | ||
* @param string $name name of lock | ||
* @param bool $blocking | ||
* @return bool | ||
*/ | ||
protected function getLock($name, $blocking) | ||
{ | ||
if (!$this->memcached->add($name, serialize($this->getLockInformation()), $this->expiration)) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Release lock | ||
* | ||
* @param string $name name of lock | ||
* @return bool | ||
*/ | ||
public function releaseLock($name) | ||
{ | ||
if (isset($this->locks[$name]) && $this->memcached->delete($name)) { | ||
unset($this->locks[$name]); | ||
|
||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Check if lock is locked | ||
* | ||
* @param string $name name of lock | ||
* @return bool | ||
*/ | ||
public function isLocked($name) | ||
{ | ||
return false !== $this->memcached->get($name); | ||
} | ||
} | ||
|
Oops, something went wrong.