Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
32 changes: 31 additions & 1 deletion src/Illuminate/Cache/ArrayStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

namespace Illuminate\Cache;

use Illuminate\Contracts\Cache\FlushableLock;
use Illuminate\Contracts\Cache\LockProvider;
use Illuminate\Support\Arr;
use Illuminate\Support\Carbon;
use Illuminate\Support\InteractsWithTime;
use RuntimeException;

class ArrayStore extends TaggableStore implements LockProvider
class ArrayStore extends TaggableStore implements FlushableLock, LockProvider
{
use InteractsWithTime, RetrievesMultipleKeys;

Expand Down Expand Up @@ -206,6 +208,24 @@ public function flush()
return true;
}

/**
* Remove all locks from the store.
*
* @return bool
*
* @throws \RuntimeException
*/
public function flushLocks(): bool
{
if (! $this->hasSeparateLockStore()) {
throw new RuntimeException('Flushing locks is only supported when the lock store is separate from the cache store.');
}

$this->locks = [];

return true;
}

/**
* Get the cache key prefix.
*
Expand Down Expand Up @@ -262,4 +282,14 @@ public function restoreLock($name, $owner)
{
return $this->lock($name, 0, $owner);
}

/**
* Determine if the lock store is separate from the cache store.
*
* @return bool
*/
public function hasSeparateLockStore(): bool
{
return true;
}
}
42 changes: 41 additions & 1 deletion src/Illuminate/Cache/DatabaseStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Cache;

use Closure;
use Illuminate\Contracts\Cache\FlushableLock;
use Illuminate\Contracts\Cache\LockProvider;
use Illuminate\Contracts\Cache\Store;
use Illuminate\Database\ConnectionInterface;
Expand All @@ -14,8 +15,9 @@
use Illuminate\Support\Collection;
use Illuminate\Support\InteractsWithTime;
use Illuminate\Support\Str;
use RuntimeException;

class DatabaseStore implements LockProvider, Store
class DatabaseStore implements FlushableLock, LockProvider, Store
{
use InteractsWithTime;

Expand Down Expand Up @@ -440,6 +442,24 @@ public function flush()
return true;
}

/**
* Remove all locks from the store.
*
* @return bool
*
* @throws \RuntimeException
*/
public function flushLocks(): bool
{
if (! $this->hasSeparateLockStore()) {
throw new RuntimeException('Flushing locks is only supported when the lock store is separate from the cache store.');
}

$this->lockTable()->delete();

return true;
}

/**
* Get a query builder for the cache table.
*
Expand All @@ -450,6 +470,16 @@ protected function table()
return $this->connection->table($this->table);
}

/**
* Get a query builder for the cache locks table.
*
* @return \Illuminate\Database\Query\Builder
*/
protected function lockTable()
{
return $this->lockConnection->table($this->lockTable);
}

/**
* Get the underlying database connection.
*
Expand Down Expand Up @@ -552,4 +582,14 @@ protected function unserialize($value)

return unserialize($value);
}

/**
* Determine if the lock store is separate from the cache store.
*
* @return bool
*/
public function hasSeparateLockStore(): bool
{
return $this->lockTable !== $this->table;
}
}
42 changes: 41 additions & 1 deletion src/Illuminate/Cache/FileStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
namespace Illuminate\Cache;

use Exception;
use Illuminate\Contracts\Cache\FlushableLock;
use Illuminate\Contracts\Cache\LockProvider;
use Illuminate\Contracts\Cache\Store;
use Illuminate\Contracts\Filesystem\LockTimeoutException;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Filesystem\LockableFile;
use Illuminate\Support\InteractsWithTime;
use RuntimeException;

class FileStore implements Store, LockProvider
class FileStore implements FlushableLock, LockProvider, Store
{
use InteractsWithTime, RetrievesMultipleKeys;

Expand Down Expand Up @@ -297,6 +299,34 @@ public function flush()
return true;
}

/**
* Remove all locks from the store.
*
* @return bool
*
* @throws \RuntimeException
*/
public function flushLocks(): bool
{
if (! $this->hasSeparateLockStore()) {
throw new RuntimeException('Flushing locks is only supported when the lock store is separate from the cache store.');
}

if (! $this->files->isDirectory($this->lockDirectory)) {
return false;
}

foreach ($this->files->directories($this->lockDirectory) as $lockDirectory) {
$deleted = $this->files->deleteDirectory($lockDirectory);

if (! $deleted || $this->files->exists($lockDirectory)) {
return false;
}
}

return true;
}

/**
* Retrieve an item and expiry time from the cache by key.
*
Expand Down Expand Up @@ -436,4 +466,14 @@ public function getPrefix()
{
return '';
}

/**
* Determine if the lock store is separate from the cache store.
*
* @return bool
*/
public function hasSeparateLockStore(): bool
{
return $this->lockDirectory !== null && $this->lockDirectory !== $this->directory;
}
}
32 changes: 31 additions & 1 deletion src/Illuminate/Cache/RedisStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Cache;

use Illuminate\Contracts\Cache\FlushableLock;
use Illuminate\Contracts\Cache\LockProvider;
use Illuminate\Contracts\Redis\Factory as Redis;
use Illuminate\Redis\Connections\PhpRedisClusterConnection;
Expand All @@ -10,8 +11,9 @@
use Illuminate\Redis\Connections\PredisConnection;
use Illuminate\Support\LazyCollection;
use Illuminate\Support\Str;
use RuntimeException;

class RedisStore extends TaggableStore implements LockProvider
class RedisStore extends TaggableStore implements FlushableLock, LockProvider
{
use RetrievesMultipleKeys {
many as private manyAlias;
Expand Down Expand Up @@ -289,6 +291,24 @@ public function flush()
return true;
}

/**
* Remove all locks from the store.
*
* @return bool
*
* @throws \RuntimeException
*/
public function flushLocks(): bool
{
if (! $this->hasSeparateLockStore()) {
throw new RuntimeException('Flushing locks is only supported when the lock store is separate from the cache store.');
}

$this->lockConnection()->flushdb();

return true;
}

/**
* Remove all expired tag set entries.
*
Expand Down Expand Up @@ -531,4 +551,14 @@ protected function connectionAwareUnserialize($value, $connection)

return $this->unserialize($value);
}

/**
* Determine if the lock store is separate from the cache store.
*
* @return bool
*/
public function hasSeparateLockStore(): bool
{
return $this->lockConnection !== $this->connection;
}
}
29 changes: 29 additions & 0 deletions src/Illuminate/Cache/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Illuminate\Cache\Events\RetrievingManyKeys;
use Illuminate\Cache\Events\WritingKey;
use Illuminate\Cache\Events\WritingManyKeys;
use Illuminate\Contracts\Cache\FlushableLock;
use Illuminate\Contracts\Cache\Repository as CacheContract;
use Illuminate\Contracts\Cache\Store;
use Illuminate\Contracts\Events\Dispatcher;
Expand Down Expand Up @@ -769,6 +770,24 @@ public function clear(): bool
return $result;
}

/**
* Flush all locks from the cache store.
*
* @return bool
*
* @throws \BadMethodCallException
*/
public function flushLocks(): bool
{
$store = $this->getStore();

if (! $this->supportsFlushingLocks()) {
throw new BadMethodCallException('This cache store does not support flushing locks.');
}

return $store->flushLocks();
}

/**
* Begin executing a new tags operation if the store supports it.
*
Expand Down Expand Up @@ -844,6 +863,16 @@ public function supportsTags()
return method_exists($this->store, 'tags');
}

/**
* Determine if the current store supports flushing locks.
*
* @return bool
*/
public function supportsFlushingLocks(): bool
{
return $this->store instanceof FlushableLock;
}

/**
* Get the default cache time.
*
Expand Down
20 changes: 20 additions & 0 deletions src/Illuminate/Contracts/Cache/FlushableLock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Illuminate\Contracts\Cache;

interface FlushableLock
{
/**
* Flush all locks managed by the store.
*
* @return bool
*/
public function flushLocks(): bool;

/**
* Determine if the lock store is separate from the cache store.
*
* @return bool
*/
public function hasSeparateLockStore(): bool;
}
2 changes: 2 additions & 0 deletions src/Illuminate/Support/Facades/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@
* @method static string getPrefix()
* @method static \Illuminate\Contracts\Cache\Lock lock(string $name, int $seconds = 0, string|null $owner = null)
* @method static \Illuminate\Contracts\Cache\Lock restoreLock(string $name, string $owner)
* @method static bool supportsFlushingLocks()
* @method static bool flushLocks()
*
* @see \Illuminate\Cache\CacheManager
* @see \Illuminate\Cache\Repository
Expand Down
12 changes: 12 additions & 0 deletions tests/Cache/CacheArrayStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,18 @@ public function testItemsCanBeFlushed()
$this->assertNull($store->get('baz'));
}

public function testLocksCanBeFlushed()
{
$store = new ArrayStore;
$store->lock('foo', 10);
$store->lock('bar', 10);
$result = $store->flushLocks();
$this->assertTrue($result);
$this->assertNull($store->get('foo'));
$this->assertNull($store->get('bar'));
$this->assertEmpty($store->locks);
}

public function testCacheKey()
{
$store = new ArrayStore;
Expand Down
13 changes: 13 additions & 0 deletions tests/Cache/CacheDatabaseStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,19 @@ public function testItemsMayBeFlushedFromCache()
$this->assertTrue($result);
}

public function testLocksMayBeFlushedFromCache()
{
$store = $this->getStore();
$connection = m::mock(\Illuminate\Database\ConnectionInterface::class);
$store->setLockConnection($connection);
$table = m::mock(stdClass::class);
$store->getLockConnection()->shouldReceive('table')->once()->with('cache_locks')->andReturn($table);
$table->shouldReceive('delete')->once()->andReturn(2);

$result = $store->flushLocks();
$this->assertTrue($result);
}

public function testIncrementReturnsCorrectValues()
{
$store = $this->getStore();
Expand Down
Loading
Loading