From 1eec7d110f2839de6c89c152e842ab33fce6a240 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Olewi=C5=84ski?= Date: Tue, 18 Dec 2018 10:07:12 +0100 Subject: [PATCH] Memcached handle (#43) * Memcached classes reorganized * Memcached classes reorganized * Memcached classes reorganized - formatting * Memcached classes reorganized - formatting --- src/DistributedMemcacheMutex.php | 13 +--------- src/DistributedMemcachedMutex.php | 40 +++++++++++++++++++++++++++---- src/DistributedMutex.php | 30 +++++++++++++++++++++++ tests/MemcachedStub.php | 7 +++--- 4 files changed, 70 insertions(+), 20 deletions(-) create mode 100644 src/DistributedMutex.php diff --git a/src/DistributedMemcacheMutex.php b/src/DistributedMemcacheMutex.php index e98ce4e..09e0e48 100644 --- a/src/DistributedMemcacheMutex.php +++ b/src/DistributedMemcacheMutex.php @@ -8,11 +8,8 @@ /** * Utility methods for manipulating distributed locks using memcache */ -class DistributedMemcacheMutex +class DistributedMemcacheMutex extends DistributedMutex { - const FIVE_MINUTES_SECONDS = 300; - const MEMCACHE_ERR_MSG = 'No servers added to memcache connection.'; - /** * @var Memcache */ @@ -50,12 +47,4 @@ protected function assertServersAddedToMemcache() throw new InvalidArgumentException(self::MEMCACHE_ERR_MSG); } } - - /** - * @param string $name - */ - public function unlock($name) - { - $this->memcache->delete($name); - } } diff --git a/src/DistributedMemcachedMutex.php b/src/DistributedMemcachedMutex.php index 7ea090c..122e73b 100644 --- a/src/DistributedMemcachedMutex.php +++ b/src/DistributedMemcachedMutex.php @@ -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 + * @author Bartłomiej Olewiński * 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; } -} \ No newline at end of file + + /** + * @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)); + } + } + } +} diff --git a/src/DistributedMutex.php b/src/DistributedMutex.php new file mode 100644 index 0000000..b86ce7d --- /dev/null +++ b/src/DistributedMutex.php @@ -0,0 +1,30 @@ + + */ + +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); + } +} diff --git a/tests/MemcachedStub.php b/tests/MemcachedStub.php index 5554df5..8d34689 100644 --- a/tests/MemcachedStub.php +++ b/tests/MemcachedStub.php @@ -16,15 +16,14 @@ class MemcachedStub extends Memcached /** * @var bool */ - private $areServersAdded = false; + private $serversAdded = []; /** * @return MemcachedStub */ public function withServersAdded() { - $this->areServersAdded = true; - + $this->serversAdded = ['localhost:11211' => '1.2.6']; return $this; } @@ -59,6 +58,6 @@ public function delete($key, $timeout = 0) */ public function getversion() { - return $this->areServersAdded; + return $this->serversAdded; } }