Skip to content

Commit

Permalink
[8.x] Ability to specify a separate lock connection
Browse files Browse the repository at this point in the history
  • Loading branch information
paras-malhotra committed Dec 15, 2020
1 parent f5f331c commit fe37ece
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 11 deletions.
26 changes: 17 additions & 9 deletions src/Illuminate/Cache/CacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,11 @@ protected function createRedisDriver(array $config)

$connection = $config['connection'] ?? 'default';

return $this->repository(new RedisStore($redis, $this->getPrefix($config), $connection));
$store = new RedisStore($redis, $this->getPrefix($config), $connection);

$store->setLockConnection($config['lock_connection'] ?? $connection);

return $this->repository($store);
}

/**
Expand All @@ -212,15 +216,19 @@ protected function createDatabaseDriver(array $config)
{
$connection = $this->app['db']->connection($config['connection'] ?? null);

return $this->repository(
new DatabaseStore(
$connection,
$config['table'],
$this->getPrefix($config),
$config['lock_table'] ?? 'cache_locks',
$config['lock_lottery'] ?? [2, 100]
)
$store = new DatabaseStore(
$connection,
$config['table'],
$this->getPrefix($config),
$config['lock_table'] ?? 'cache_locks',
$config['lock_lottery'] ?? [2, 100]
);

$store->setLockConnection(
$this->app['db']->connection($config['lock_connection'] ?? $config['connection'] ?? null)
);

return $this->repository($store);
}

/**
Expand Down
10 changes: 10 additions & 0 deletions src/Illuminate/Cache/DatabaseLock.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,16 @@ public function forceRelease()
->delete();
}

/**
* Get the name of the database connection.
*
* @return string
*/
public function getConnectionName()
{
return $this->connection->getName();
}

/**
* Returns the owner value written into the driver for this lock.
*
Expand Down
20 changes: 19 additions & 1 deletion src/Illuminate/Cache/DatabaseStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ class DatabaseStore implements LockProvider, Store
*/
protected $connection;

/**
* The database connection instance for the lock.
*
* @var \Illuminate\Database\ConnectionInterface
*/
protected $lockConnection;

/**
* The name of the cache table.
*
Expand Down Expand Up @@ -265,7 +272,7 @@ public function forever($key, $value)
public function lock($name, $seconds = 0, $owner = null)
{
return new DatabaseLock(
$this->connection,
$this->lockConnection ?? $this->connection,
$this->lockTable,
$this->prefix.$name,
$seconds,
Expand Down Expand Up @@ -331,6 +338,17 @@ public function getConnection()
return $this->connection;
}

/**
* Set the lock connection to be used.
*
* @param \Illuminate\Database\ConnectionInterface $connection
* @return void
*/
public function setLockConnection($connection)
{
$this->lockConnection = $connection;
}

/**
* Get the cache key prefix.
*
Expand Down
10 changes: 10 additions & 0 deletions src/Illuminate/Cache/RedisLock.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ public function forceRelease()
$this->redis->del($this->name);
}

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

/**
* Returns the owner value written into the driver for this lock.
*
Expand Down
30 changes: 29 additions & 1 deletion src/Illuminate/Cache/RedisStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ class RedisStore extends TaggableStore implements LockProvider
*/
protected $connection;

/**
* The name of the connection that should be used for locks.
*
* @var string
*/
protected $lockConnection;

/**
* Create a new Redis store.
*
Expand Down Expand Up @@ -181,7 +188,7 @@ public function forever($key, $value)
*/
public function lock($name, $seconds = 0, $owner = null)
{
return new RedisLock($this->connection(), $this->prefix.$name, $seconds, $owner);
return new RedisLock($this->lockConnection(), $this->prefix.$name, $seconds, $owner);
}

/**
Expand Down Expand Up @@ -242,6 +249,16 @@ public function connection()
return $this->redis->connection($this->connection);
}

/**
* Get the Redis connection instance for the lock.
*
* @return \Illuminate\Redis\Connections\Connection
*/
public function lockConnection()
{
return $this->redis->connection($this->lockConnection ?? $this->connection);
}

/**
* Set the connection name to be used.
*
Expand All @@ -253,6 +270,17 @@ public function setConnection($connection)
$this->connection = $connection;
}

/**
* Set the lock connection name to be used.
*
* @param string $connection
* @return void
*/
public function setLockConnection($connection)
{
$this->lockConnection = $connection;
}

/**
* Get the Redis database instance.
*
Expand Down
7 changes: 7 additions & 0 deletions tests/Integration/Cache/RedisCacheLockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ public function testRedisLocksCanBeAcquiredAndReleased()
Cache::store('redis')->lock('foo')->release();
}

public function testRedisLockCanHaveASeparateConnection()
{
$this->app['config']->set('cache.stores.redis.lock_connection', 'default');

$this->assertSame('default', Cache::store('redis')->lock('foo')->getConnectionName());
}

public function testRedisLocksCanBlockForSeconds()
{
Carbon::setTestNow();
Expand Down
8 changes: 8 additions & 0 deletions tests/Integration/Database/DatabaseLockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ protected function setUp(): void
});
}

public function testLockCanHaveASeparateConnection()
{
$this->app['config']->set('cache.stores.database.lock_connection', 'test');
$this->app['config']->set('database.connections.test', $this->app['config']->get('database.connections.mysql'));

$this->assertSame('test', Cache::driver('database')->lock('foo')->getConnectionName());
}

public function testLockCanBeAcquired()
{
$lock = Cache::driver('database')->lock('foo');
Expand Down

0 comments on commit fe37ece

Please sign in to comment.