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
59 changes: 57 additions & 2 deletions src/Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

namespace ElcoBvg\Opcache;

use Illuminate\Cache\FileLock;
use Illuminate\Support\Str;
use Illuminate\Cache\TagSet;
use Illuminate\Cache\TaggableStore;
use Illuminate\Support\Facades\Log;
use Illuminate\Contracts\Cache\Store as StoreContract;
use Illuminate\Cache\RetrievesMultipleKeys;
use Illuminate\Contracts\Cache\LockProvider;

class Store extends TaggableStore implements StoreContract
class Store extends TaggableStore implements StoreContract, LockProvider
{
use RetrievesMultipleKeys;

Expand Down Expand Up @@ -41,6 +43,13 @@ class Store extends TaggableStore implements StoreContract
*/
protected $enabled = false;

/**
* The file cache lock directory.
*
* @var string|null
*/
protected $lockDirectory;

/**
* Create a new OPcache store.
*
Expand All @@ -63,7 +72,7 @@ public function __construct(string $prefix = '', string $directory = '')
*/
$this->directory = $directory ?: config('cache.stores.opcache.path', config('cache.stores.file.path'));
}

/**
* Begin executing a new tags operation.
*
Expand Down Expand Up @@ -401,4 +410,50 @@ public function extendExpiration(string $key, int $seconds = 1)
}
return false;
}

/**
* 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)
{
$directory = $this->lockDirectory ?? $this->directory;
$this->checkDirectory($directory);

return new FileLock(
new static($this->prefix, $directory),
$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);
}

/**
* Set the cache directory where locks should be stored.
*
* @param string|null $lockDirectory
* @return $this
*/
public function setLockDirectory($lockDirectory)
{
$this->lockDirectory = $lockDirectory;

return $this;
}
}
36 changes: 36 additions & 0 deletions tests/OpcacheDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,42 @@ public function testStoreRememberFromCache()
$this->testStoreRemember();
}

public function testStoreLock()
{
$store = $this->getStore();
$lock = $store->lock('test-lock', 10);

$this->assertInstanceOf(\Illuminate\Cache\FileLock::class, $lock);
$this->assertTrue($lock->acquire());
$this->assertTrue($lock->release());
}

public function testStoreRestoreLock()
{
$store = $this->getStore();
$lock = $store->lock('test-lock', 10);

$owner = $lock->owner();
$this->assertTrue($lock->acquire());

$restoredLock = $store->restoreLock('test-lock', $owner);
$this->assertInstanceOf(\Illuminate\Cache\FileLock::class, $restoredLock);
$this->assertTrue($restoredLock->release());
}

public function testStoreLockDirectory()
{
$store = $this->getStore();
$customLockPath = sys_get_temp_dir() . '/opcache-locks';

$store->setLockDirectory($customLockPath);
$lock = $store->lock('test-lock', 10);

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

protected function getStore()
{
return new Store('opcache', sys_get_temp_dir());
Expand Down