Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[7.x] Cache::lock support for the database cache driver #32639

Merged
merged 7 commits into from
May 4, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/Illuminate/Cache/CacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,11 @@ protected function createDatabaseDriver(array $config)

return $this->repository(
new DatabaseStore(
$connection, $config['table'], $this->getPrefix($config)
$connection,
$config['table'],
$this->getPrefix($config),
$config['lock_table'] ?? 'cache_locks',
$config['lock_lottery'] ?? [2, 100]
)
);
}
Expand Down
138 changes: 138 additions & 0 deletions src/Illuminate/Cache/DatabaseLock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php

namespace Illuminate\Cache;

use Illuminate\Database\Connection;
use Illuminate\Database\QueryException;

class DatabaseLock extends Lock
{
/**
* The database connection instance.
*
* @var \Illuminate\Database\Connection
*/
protected $connection;

/**
* The database table name.
*
* @var string
*/
protected $table;

/**
* The prune probability odds.
*
* @var string
GrahamCampbell marked this conversation as resolved.
Show resolved Hide resolved
*/
protected $lottery;

/**
* Create a new lock instance.
*
* @param \Illuminate\Database\Connection $connection
* @param string $table
* @param string $name
* @param int $seconds
* @param string|null $owner
* @param array $lottery
* @return void
*/
public function __construct(Connection $connection, $table, $name, $seconds, $owner = null, $lottery = [2, 100])
{
parent::__construct($name, $seconds, $owner);

$this->connection = $connection;
$this->table = $table;
$this->lottery = $lottery;
}

/**
* Attempt to acquire the lock.
*
* @return bool
*/
public function acquire()
{
$acquired = false;

try {
$this->connection->table($this->table)->insert([
'id' => $this->name,
'owner' => $this->owner,
'expires_at' => $this->expiresAt(),
]);

$acquired = true;
} catch (QueryException $e) {
$updated = $this->connection->table($this->table)
->where('id', $this->name)
->where(function ($query) {
return $query->where('owner', $this->owner)->orWhere('expires_at', '<=', time());
})->update([
'owner' => $this->owner,
'expires_at' => $this->expiresAt(),
]);

$acquired = $updated >= 1;
}

if (random_int(1, $this->lottery[1]) <= $this->lottery[0]) {
$this->connection->table($this->table)->where('expires_at', '<=', time())->delete();
}

return $acquired;
}

/**
* Get the UNIX timestamp indicating when the lock should expire.
*
* @return int
*/
protected function expiresAt()
{
return $this->seconds > 0 ? time() + $this->seconds : now()->addDays(1)->getTimestamp();
}

/**
* Release the lock.
*
* @return bool
*/
public function release()
{
if ($this->isOwnedByCurrentProcess()) {
$this->connection->table($this->table)
->where('id', $this->name)
->where('owner', $this->owner)
->delete();

return true;
}

return false;
}

/**
* Releases this lock in disregard of ownership.
*
* @return void
*/
public function forceRelease()
{
$this->connection->table($this->table)
->where('id', $this->name)
->delete();
}

/**
* Returns the owner value written into the driver for this lock.
*
* @return string
*/
protected function getCurrentOwner()
{
return optional($this->connection->table($this->table)->where('id', $this->name)->first())->token;
}
}
56 changes: 55 additions & 1 deletion src/Illuminate/Cache/DatabaseStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,41 @@ class DatabaseStore implements Store
*/
protected $prefix;

/**
* The name of the cache locks table.
*
* @var string
*/
protected $lockTable;

/**
* A array representation of the lock lottery odds.
*
* @var string
*/
protected $lockLottery;

/**
* Create a new database store.
*
* @param \Illuminate\Database\ConnectionInterface $connection
* @param string $table
* @param string $prefix
* @param string $lockTable
* @param array $lockLottery
* @return void
*/
public function __construct(ConnectionInterface $connection, $table, $prefix = '')
public function __construct(ConnectionInterface $connection,
$table,
$prefix = '',
$lockTable = 'cache_locks',
$lockLottery = [2, 100])
{
$this->table = $table;
$this->prefix = $prefix;
$this->connection = $connection;
$this->lockTable = $lockTable;
$this->lockLottery = $lockLottery;
}

/**
Expand Down Expand Up @@ -205,6 +227,38 @@ public function forever($key, $value)
return $this->put($key, $value, 315360000);
}

/**
* Get a lock instance.
*
* @param string $name
* @param int $seconds
* @param string|null $owner
* @return \Illuminate\Contracts\Cache\Lock
*/
public function lock($name, $seconds = 0, $owner = null)
{
return new DatabaseLock(
$this->connection,
$this->lockTable,
$this->prefix.$name,
$seconds,
$owner,
$this->lockLottery
);
}

/**
* Restore a lock instance using the owner identifier.
*
* @param string $name
* @param string $owner
* @return \Illuminate\Contracts\Cache\Lock
*/
public function restoreLock($name, $owner)
{
return $this->lock($name, 0, $owner);
}

/**
* Remove an item from the cache.
*
Expand Down