-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Memcached classes reorganized * Memcached classes reorganized * Memcached classes reorganized - formatting * Memcached classes reorganized - formatting
- Loading branch information
Bartek Olewiński
authored
Dec 18, 2018
1 parent
64fe395
commit 1eec7d1
Showing
4 changed files
with
70 additions
and
20 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
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 |
---|---|---|
|
@@ -3,19 +3,20 @@ | |
|
||
namespace Assertis\Util; | ||
|
||
use InvalidArgumentException; | ||
use Memcached; | ||
|
||
/** | ||
* This class should be used when we will migrate to PHP 7.2 | ||
* It extends DistributedMemcacheMutex because all other methods like add, remove etc. | ||
* are available in memcached also so there is no reason to overwrite methods. | ||
* | ||
* @author Łukasz Nowak <[email protected]> | ||
* @author Bartłomiej Olewiński <[email protected]> | ||
* Class DistributedMemcachedMutex | ||
* @package Assertis\Util | ||
*/ | ||
class DistributedMemcachedMutex extends DistributedMemcacheMutex | ||
class DistributedMemcachedMutex extends DistributedMutex | ||
{ | ||
const MEMCACHE_RESPOND_ERR_MSG = 'Server %s not responding.'; | ||
|
||
/** | ||
* DistributedMemcachedMutex constructor. | ||
|
@@ -25,4 +26,35 @@ public function __construct(Memcached $memcached) | |
{ | ||
$this->memcache = $memcached; | ||
} | ||
} | ||
|
||
/** | ||
* @param string $name | ||
* @param int $expirationTimeInSeconds | ||
* | ||
* @throws InvalidArgumentException | ||
* @throws AlreadyLockedException | ||
*/ | ||
public function lock($name, $expirationTimeInSeconds = self::FIVE_MINUTES_SECONDS) | ||
{ | ||
$this->assertServersAddedToMemcachedAndResponding(); | ||
|
||
if (false === $this->memcache->add($name, 1, $expirationTimeInSeconds)) { | ||
throw new AlreadyLockedException($name); | ||
} | ||
} | ||
|
||
protected function assertServersAddedToMemcachedAndResponding() | ||
{ | ||
$serverVersions = $this->memcache->getversion(); | ||
|
||
if (count($serverVersions) == 0) { | ||
throw new InvalidArgumentException(self::MEMCACHE_ERR_MSG); | ||
} | ||
|
||
foreach ($serverVersions as $server => $version) { | ||
if ($version == '255.255.255') { //https://secure.php.net/manual/en/memcached.getversion.php#111539 | ||
throw new InvalidArgumentException(sprintf(self::MEMCACHE_RESPOND_ERR_MSG, $server)); | ||
} | ||
} | ||
} | ||
} |
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,30 @@ | ||
<?php | ||
/** | ||
* @author Bartłomiej Olewiński <[email protected]> | ||
*/ | ||
|
||
namespace Assertis\Util; | ||
|
||
use Memcache; | ||
use Memcached; | ||
|
||
abstract class DistributedMutex | ||
{ | ||
const FIVE_MINUTES_SECONDS = 300; | ||
const MEMCACHE_ERR_MSG = 'No servers added to memcache connection.'; | ||
|
||
/** | ||
* @var Memcache|Memcached | ||
*/ | ||
protected $memcache; | ||
|
||
abstract public function lock($name, $expirationTimeInSeconds); | ||
|
||
/** | ||
* @param string $name | ||
*/ | ||
public function unlock($name) | ||
{ | ||
$this->memcache->delete($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