Skip to content

Commit

Permalink
feature #27456 [LOCK] Add a PdoStore (jderusse)
Browse files Browse the repository at this point in the history
This PR was merged into the 4.2-dev branch.

Discussion
----------

[LOCK] Add a PdoStore

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #25400
| License       | MIT
| Doc PR        | symfony/symfony-docs#9875

This is an alternative to #25578

Commits
-------

46fe1b0 Add a PdoStore in lock
  • Loading branch information
fabpot committed Sep 4, 2018
2 parents 2ea27cd + 46fe1b0 commit 78ecaf3
Show file tree
Hide file tree
Showing 9 changed files with 506 additions and 21 deletions.
5 changes: 5 additions & 0 deletions src/Symfony/Component/Lock/CHANGELOG.md
@@ -1,6 +1,11 @@
CHANGELOG
=========

4.2.0
-----

* added the PDO Store

3.4.0
-----

Expand Down
15 changes: 6 additions & 9 deletions src/Symfony/Component/Lock/Store/MemcachedStore.php
Expand Up @@ -57,7 +57,7 @@ public function __construct(\Memcached $memcached, int $initialTtl = 300)
*/
public function save(Key $key)
{
$token = $this->getToken($key);
$token = $this->getUniqueToken($key);
$key->reduceLifetime($this->initialTtl);
if (!$this->memcached->add((string) $key, $token, (int) ceil($this->initialTtl))) {
// the lock is already acquired. It could be us. Let's try to put off.
Expand All @@ -80,13 +80,13 @@ public function waitAndSave(Key $key)
public function putOffExpiration(Key $key, $ttl)
{
if ($ttl < 1) {
throw new InvalidArgumentException(sprintf('%s() expects a TTL greater or equals to 1. Got %s.', __METHOD__, $ttl));
throw new InvalidArgumentException(sprintf('%s() expects a TTL greater or equals to 1 second. Got %s.', __METHOD__, $ttl));
}

// Interface defines a float value but Store required an integer.
$ttl = (int) ceil($ttl);

$token = $this->getToken($key);
$token = $this->getUniqueToken($key);

list($value, $cas) = $this->getValueAndCas($key);

Expand Down Expand Up @@ -120,7 +120,7 @@ public function putOffExpiration(Key $key, $ttl)
*/
public function delete(Key $key)
{
$token = $this->getToken($key);
$token = $this->getUniqueToken($key);

list($value, $cas) = $this->getValueAndCas($key);

Expand All @@ -144,13 +144,10 @@ public function delete(Key $key)
*/
public function exists(Key $key)
{
return $this->memcached->get((string) $key) === $this->getToken($key);
return $this->memcached->get((string) $key) === $this->getUniqueToken($key);
}

/**
* Retrieve an unique token for the given key.
*/
private function getToken(Key $key): string
private function getUniqueToken(Key $key): string
{
if (!$key->hasState(__CLASS__)) {
$token = base64_encode(random_bytes(32));
Expand Down

0 comments on commit 78ecaf3

Please sign in to comment.