Skip to content

Commit

Permalink
Memcached handle (#43)
Browse files Browse the repository at this point in the history
* 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
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 20 deletions.
13 changes: 1 addition & 12 deletions src/DistributedMemcacheMutex.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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);
}
}
40 changes: 36 additions & 4 deletions src/DistributedMemcachedMutex.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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));
}
}
}
}
30 changes: 30 additions & 0 deletions src/DistributedMutex.php
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);
}
}
7 changes: 3 additions & 4 deletions tests/MemcachedStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -59,6 +58,6 @@ public function delete($key, $timeout = 0)
*/
public function getversion()
{
return $this->areServersAdded;
return $this->serversAdded;
}
}

0 comments on commit 1eec7d1

Please sign in to comment.