Skip to content

Added lockExists to Store interface, fixed locking bugs, added tests. #5381

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

Closed
Closed
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
2 changes: 1 addition & 1 deletion src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ protected function lock(Request $request, Response $entry)

// wait for the lock to be released
$wait = 0;
while (is_file($lock) && $wait < 5000000) {
while ($this->store->isLocked($request) && $wait < 5000000) {
usleep(50000);
$wait += 50000;
}
Expand Down
16 changes: 13 additions & 3 deletions src/Symfony/Component/HttpKernel/HttpCache/Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,20 @@ public function cleanup()
*/
public function lock(Request $request)
{
if (false !== $lock = @fopen($path = $this->getPath($this->getCacheKey($request).'.lck'), 'x')) {
$path = $this->getPath($this->getCacheKey($request).'.lck');
if (!is_dir(dirname($path)) && false === @mkdir(dirname($path), 0777, true)) {
return false;
}

$lock = @fopen($path, 'x');
if (false !== $lock) {
fclose($lock);

$this->locks[] = $path;

return true;
}

return $path;
return !file_exists($path) ?: $path;
}

/**
Expand All @@ -92,6 +97,11 @@ public function unlock(Request $request)
return @unlink($this->getPath($this->getCacheKey($request).'.lck'));
}

public function isLocked(Request $request)
{
return is_file($this->getPath($this->getCacheKey($request).'.lck'));
}

/**
* Locates a cached Response for the Request provided.
*
Expand Down
9 changes: 9 additions & 0 deletions src/Symfony/Component/HttpKernel/HttpCache/StoreInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ public function lock(Request $request);
*/
public function unlock(Request $request);

/**
* Returns whether or not a lock exists.
*
* @param Request $request A Request instance
*
* @return Boolean true if lock exists, false otherwise
*/
public function isLocked(Request $request);

/**
* Purges data for the given URL.
*
Expand Down
12 changes: 12 additions & 0 deletions src/Symfony/Component/HttpKernel/Tests/HttpCache/StoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,18 @@ public function testOverwritesNonVaryingResponseWithStore()
$this->assertCount(2, $this->getStoreMetadata($key));
}

public function testLocking()
{
$req = Request::create('/test', 'get', array(), array(), array(), array('HTTP_FOO' => 'Foo', 'HTTP_BAR' => 'Bar'));
$this->assertTrue($this->store->lock($req));

$path = $this->store->lock($req);
$this->assertTrue($this->store->isLocked($req));

$this->store->unlock($req);
$this->assertFalse($this->store->isLocked($req));
}

protected function storeSimpleEntry($path = null, $headers = array())
{
if (null === $path) {
Expand Down