Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added lockExists to Store interface, fixed locking bugs, added tests.
  • Loading branch information
msonnabaum authored and fabpot committed Oct 27, 2012
1 parent ecab04c commit 0f3126f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php
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
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
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
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

0 comments on commit 0f3126f

Please sign in to comment.