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

[6.x] Introduce a locking mechanism for the array cache driver #30253

Merged
merged 1 commit into from
Oct 14, 2019
Merged
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
98 changes: 98 additions & 0 deletions src/Illuminate/Cache/ArrayLock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

namespace Illuminate\Cache;

use Carbon\Carbon;

class ArrayLock extends Lock
{
/**
* The parent array cache store.
*
* @var \Illuminate\Cache\ArrayStore
*/
protected $store;

/**
* Create a new lock instance.
*
* @param \Illuminate\Cache\ArrayStore $store
* @param string $name
* @param int $seconds
* @param string|null $owner
* @return void
*/
public function __construct($store, $name, $seconds, $owner = null)
{
parent::__construct($name, $seconds, $owner);

$this->store = $store;
}

/**
* Attempt to acquire the lock.
*
* @return bool
*/
public function acquire()
{
$expiration = $this->store->locks[$this->name]['expiresAt'] ?? Carbon::now()->addSecond();

if ($this->exists() && $expiration->isFuture()) {
return false;
}

$this->store->locks[$this->name] = [
'owner' => $this->owner,
'expiresAt' => $this->seconds === 0 ? null : Carbon::now()->addSeconds($this->seconds),
];

return true;
}

/**
* Determine if the current lock exists.
*
* @return bool
*/
protected function exists()
{
return isset($this->store->locks[$this->name]);
}

/**
* Release the lock.
*
* @return bool
*/
public function release()
{
if (! $this->isOwnedByCurrentProcess()) {
return false;
}

$this->forceRelease();

return true;
}

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

/**
* Releases this lock in disregard of ownership.
*
* @return void
*/
public function forceRelease()
{
unset($this->store->locks[$this->name]);
}
}
35 changes: 34 additions & 1 deletion src/Illuminate/Cache/ArrayStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

namespace Illuminate\Cache;

use Illuminate\Contracts\Cache\LockProvider;
use Illuminate\Support\InteractsWithTime;

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

Expand All @@ -15,6 +16,13 @@ class ArrayStore extends TaggableStore
*/
protected $storage = [];

/**
* The array of locks.
*
* @var array
*/
public $locks = [];

/**
* Retrieve an item from the cache by key.
*
Expand Down Expand Up @@ -162,4 +170,29 @@ protected function toTimestamp($seconds)
{
return $seconds > 0 ? $this->availableAt($seconds) : 0;
}

/**
* 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 ArrayLock($this, $name, $seconds, $owner);
}

/**
* 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);
}
}
73 changes: 73 additions & 0 deletions tests/Cache/CacheArrayStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,77 @@ public function testCacheKey()
$store = new ArrayStore;
$this->assertEmpty($store->getPrefix());
}

public function testCannotAquireLockTwice()
{
$store = new ArrayStore;
$lock = $store->lock('foo', 10);

$this->assertTrue($lock->acquire());
$this->assertFalse($lock->acquire());
}

public function testCanAquireLockAgainAfterExpiry()
{
Carbon::setTestNow(Carbon::now());
$store = new ArrayStore;
$lock = $store->lock('foo', 10);
$lock->acquire();
Carbon::setTestNow(Carbon::now()->addSeconds(10));

$this->assertTrue($lock->acquire());
}

public function testLockExpirationLowerBoundary()
{
Carbon::setTestNow(Carbon::now());
$store = new ArrayStore;
$lock = $store->lock('foo', 10);
$lock->acquire();
Carbon::setTestNow(Carbon::now()->addSeconds(10)->subMicrosecond());

$this->assertFalse($lock->acquire());
}

public function testLockWithNoExpirationNeverExpires()
{
Carbon::setTestNow(Carbon::now());
$store = new ArrayStore;
$lock = $store->lock('foo');
$lock->acquire();
Carbon::setTestNow(Carbon::now()->addYears(100));

$this->assertFalse($lock->acquire());
}

public function testCanAcquireLockAfterRelease()
{
$store = new ArrayStore;
$lock = $store->lock('foo', 10);
$lock->acquire();

$this->assertTrue($lock->release());
$this->assertTrue($lock->acquire());
}

public function testAnotherOwnerCannotReleaseLock()
{
$store = new ArrayStore;
$owner = $store->lock('foo', 10);
$wannabeOwner = $store->lock('foo', 10);
$owner->acquire();

$this->assertFalse($wannabeOwner->release());
}

public function testAnotherOwnerCanForceReleaseALock()
{
$store = new ArrayStore;
$owner = $store->lock('foo', 10);
$wannabeOwner = $store->lock('foo', 10);
$owner->acquire();
$wannabeOwner->forceRelease();

$this->assertTrue($wannabeOwner->acquire());
}
}