/src/Symfony/Component/Lock/Store/RedisStore.php

https://github.com/gimler/symfony · PHP · 160 lines · 100 code · 23 blank · 37 comment · 16 complexity · 2cf4080e0a92592e86f8a5d57248b541 MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Lock\Store;
  11. use Symfony\Component\Cache\Traits\RedisProxy;
  12. use Symfony\Component\Lock\Exception\InvalidArgumentException;
  13. use Symfony\Component\Lock\Exception\LockConflictedException;
  14. use Symfony\Component\Lock\Exception\LockExpiredException;
  15. use Symfony\Component\Lock\Key;
  16. use Symfony\Component\Lock\StoreInterface;
  17. /**
  18. * RedisStore is a StoreInterface implementation using Redis as store engine.
  19. *
  20. * @author Jérémy Derussé <jeremy@derusse.com>
  21. */
  22. class RedisStore implements StoreInterface
  23. {
  24. private $redis;
  25. private $initialTtl;
  26. /**
  27. * @param \Redis|\RedisArray|\RedisCluster|\Predis\Client $redisClient
  28. * @param float $initialTtl the expiration delay of locks in seconds
  29. */
  30. public function __construct($redisClient, float $initialTtl = 300.0)
  31. {
  32. if (!$redisClient instanceof \Redis && !$redisClient instanceof \RedisArray && !$redisClient instanceof \RedisCluster && !$redisClient instanceof \Predis\Client && !$redisClient instanceof RedisProxy) {
  33. throw new InvalidArgumentException(sprintf('%s() expects parameter 1 to be Redis, RedisArray, RedisCluster or Predis\Client, %s given', __METHOD__, \is_object($redisClient) ? \get_class($redisClient) : \gettype($redisClient)));
  34. }
  35. if ($initialTtl <= 0) {
  36. throw new InvalidArgumentException(sprintf('%s() expects a strictly positive TTL. Got %d.', __METHOD__, $initialTtl));
  37. }
  38. $this->redis = $redisClient;
  39. $this->initialTtl = $initialTtl;
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function save(Key $key)
  45. {
  46. $script = '
  47. if redis.call("GET", KEYS[1]) == ARGV[1] then
  48. return redis.call("PEXPIRE", KEYS[1], ARGV[2])
  49. elseif redis.call("SET", KEYS[1], ARGV[1], "NX", "PX", ARGV[2]) then
  50. return 1
  51. else
  52. return 0
  53. end
  54. ';
  55. $key->reduceLifetime($this->initialTtl);
  56. if (!$this->evaluate($script, (string) $key, array($this->getToken($key), (int) ceil($this->initialTtl * 1000)))) {
  57. throw new LockConflictedException();
  58. }
  59. if ($key->isExpired()) {
  60. throw new LockExpiredException(sprintf('Failed to store the "%s" lock.', $key));
  61. }
  62. }
  63. public function waitAndSave(Key $key)
  64. {
  65. throw new InvalidArgumentException(sprintf('The store "%s" does not supports blocking locks.', \get_class($this)));
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function putOffExpiration(Key $key, $ttl)
  71. {
  72. $script = '
  73. if redis.call("GET", KEYS[1]) == ARGV[1] then
  74. return redis.call("PEXPIRE", KEYS[1], ARGV[2])
  75. else
  76. return 0
  77. end
  78. ';
  79. $key->reduceLifetime($ttl);
  80. if (!$this->evaluate($script, (string) $key, array($this->getToken($key), (int) ceil($ttl * 1000)))) {
  81. throw new LockConflictedException();
  82. }
  83. if ($key->isExpired()) {
  84. throw new LockExpiredException(sprintf('Failed to put off the expiration of the "%s" lock within the specified time.', $key));
  85. }
  86. }
  87. /**
  88. * {@inheritdoc}
  89. */
  90. public function delete(Key $key)
  91. {
  92. $script = '
  93. if redis.call("GET", KEYS[1]) == ARGV[1] then
  94. return redis.call("DEL", KEYS[1])
  95. else
  96. return 0
  97. end
  98. ';
  99. $this->evaluate($script, (string) $key, array($this->getToken($key)));
  100. }
  101. /**
  102. * {@inheritdoc}
  103. */
  104. public function exists(Key $key)
  105. {
  106. return $this->redis->get((string) $key) === $this->getToken($key);
  107. }
  108. /**
  109. * Evaluates a script in the corresponding redis client.
  110. *
  111. * @return mixed
  112. */
  113. private function evaluate(string $script, string $resource, array $args)
  114. {
  115. if ($this->redis instanceof \Redis || $this->redis instanceof \RedisCluster || $this->redis instanceof RedisProxy) {
  116. return $this->redis->eval($script, array_merge(array($resource), $args), 1);
  117. }
  118. if ($this->redis instanceof \RedisArray) {
  119. return $this->redis->_instance($this->redis->_target($resource))->eval($script, array_merge(array($resource), $args), 1);
  120. }
  121. if ($this->redis instanceof \Predis\Client) {
  122. return \call_user_func_array(array($this->redis, 'eval'), array_merge(array($script, 1, $resource), $args));
  123. }
  124. throw new InvalidArgumentException(sprintf('%s() expects being initialized with a Redis, RedisArray, RedisCluster or Predis\Client, %s given', __METHOD__, \is_object($this->redis) ? \get_class($this->redis) : \gettype($this->redis)));
  125. }
  126. /**
  127. * Retrieves an unique token for the given key.
  128. */
  129. private function getToken(Key $key): string
  130. {
  131. if (!$key->hasState(__CLASS__)) {
  132. $token = base64_encode(random_bytes(32));
  133. $key->setState(__CLASS__, $token);
  134. }
  135. return $key->getState(__CLASS__);
  136. }
  137. }