master

laravel/framework

Last updated at: 29/12/2023 09:21

RedisLock.php

TLDR

The RedisLock.php file is a class that extends the Lock class. It provides methods for acquiring, releasing, and force releasing a lock using a Redis cache.

Methods

__construct

The constructor method for the RedisLock class. It initializes the redis property with a Redis connection and calls the parent constructor.

acquire

Attempts to acquire the lock. If the lock has a specified expiration time, it uses the set method of the Redis connection to set the lock with the given name, owner, and expiration time. If the lock has no expiration time, it uses the setnx method to set the lock with the given name and owner. Returns true if the lock is successfully acquired, false otherwise.

release

Releases the lock. It uses the eval method of the Redis connection to evaluate a Lua script that removes the lock with the given name and owner. Returns true if the lock is successfully released, false otherwise.

forceRelease

Releases the lock without checking ownership. It uses the del method of the Redis connection to delete the lock with the given name.

getCurrentOwner

Returns the current owner value stored in the Redis cache for the lock with the given name.

getConnectionName

Returns the name of the Redis connection used to manage the lock.

<?php

namespace Illuminate\Cache;

class RedisLock extends Lock
{
    /**
     * 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
     * @param  string|null  $owner
     * @return void
     */
    public function __construct($redis, $name, $seconds, $owner = null)
    {
        parent::__construct($name, $seconds, $owner);

        $this->redis = $redis;
    }

    /**
     * Attempt to acquire the lock.
     *
     * @return bool
     */
    public function acquire()
    {
        if ($this->seconds > 0) {
            return $this->redis->set($this->name, $this->owner, 'EX', $this->seconds, 'NX') == true;
        }

        return $this->redis->setnx($this->name, $this->owner) === 1;
    }

    /**
     * Release the lock.
     *
     * @return bool
     */
    public function release()
    {
        return (bool) $this->redis->eval(LuaScripts::releaseLock(), 1, $this->name, $this->owner);
    }

    /**
     * Releases this lock in disregard of ownership.
     *
     * @return void
     */
    public function forceRelease()
    {
        $this->redis->del($this->name);
    }

    /**
     * Returns the owner value written into the driver for this lock.
     *
     * @return string
     */
    protected function getCurrentOwner()
    {
        return $this->redis->get($this->name);
    }

    /**
     * Get the name of the Redis connection being used to manage the lock.
     *
     * @return string
     */
    public function getConnectionName()
    {
        return $this->redis->getName();
    }
}