123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- <?php
-
- namespace Illuminate\Cache;
-
- use Illuminate\Contracts\Cache\Lock as LockContract;
-
- class RedisLock extends Lock implements LockContract
- {
- /**
- * The Redis factory implementation.
- *
- * @var \Illuminate\Redis\Connections\Connection
- */
- protected $redis;
-
- /**
- * Create a new lock instance.
- *
- * @param \Illuminate\Redis\Connections\Connection $redis
- * @param string $name
- * @param int $seconds
- * @return void
- */
- public function __construct($redis, $name, $seconds)
- {
- parent::__construct($name, $seconds);
-
- $this->redis = $redis;
- }
-
- /**
- * Attempt to acquire the lock.
- *
- * @return bool
- */
- public function acquire()
- {
- $result = $this->redis->setnx($this->name, 1);
-
- if ($result === 1 && $this->seconds > 0) {
- $this->redis->expire($this->name, $this->seconds);
- }
-
- return $result === 1;
- }
-
- /**
- * Release the lock.
- *
- * @return void
- */
- public function release()
- {
- $this->redis->del($this->name);
- }
- }
|