Skip to content
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
2 changes: 1 addition & 1 deletion src/APCuL1.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public function delete($event_id, Address $address)
return apcu_clear_cache();
} elseif ($address->isEntireBin()) {
$prefix = $this->getLocalKey($address);
$pattern = '/^' . $prefix . '.*/';
$pattern = '/^' . preg_quote($prefix) . '.*/';
$matching = $this->getIterator($pattern, APC_ITER_KEY);
if (!$matching) {
// @codeCoverageIgnoreStart
Expand Down
6 changes: 2 additions & 4 deletions src/DatabaseL2.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,9 @@ public function getEntry(Address $address)

$unserialized_value = @unserialize($last_matching_entry->value);

// If unserialization failed, miss.
// If unserialization failed, raise an exception.
if ($unserialized_value === false && $last_matching_entry->value !== serialize(false)) {
// @TODO: Warn or throw an exception.
$this->misses++;
return null;
throw new UnserializationException($address, $last_matching_entry->value);
}

$last_matching_entry->value = $unserialized_value;
Expand Down
4 changes: 1 addition & 3 deletions src/StaticL2.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@ public function getEntry(Address $address)

// If unserialization failed, miss.
if ($unserialized_value === false && $last_matching_entry->value !== serialize(false)) {
// @TODO: Warn or throw an exception.
$this->misses++;
return null;
throw new UnserializationException($address, $last_matching_entry->value);
}

$last_matching_entry->value = $unserialized_value;
Expand Down
26 changes: 26 additions & 0 deletions src/UnserializationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace LCache;

class UnserializationException extends \Exception
{
protected $address;
protected $serialized_data;

public function __construct(Address $address, $serialized_data)
{
$this->address = $address;
$this->serialized_data = $serialized_data;
parent::__construct('Failed to unserialize on cache get');
}

public function __toString()
{
return __CLASS__ . ': Cache bin "' . $this->address->getBin() . '" and key "' . $this->address->getKey() . '"' . PHP_EOL;
}

public function getSerializedData()
{
return $this->serialized_data;
}
}
57 changes: 52 additions & 5 deletions tests/LCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ public function testAddressSerialization()
$this->assertEquals(strpos($entire_mybin->serialize(), $mybin_mykey->serialize()), 0);
}

public function performFailedUnserializationTest($l2)
protected function performFailedUnserializationTest($l2)
{
$l1 = new StaticL1();
$pool = new Integrated($l1, $l2);
Expand All @@ -651,24 +651,71 @@ public function performFailedUnserializationTest($l2)
$this->assertNull($l1->get($myaddr));
$this->assertEquals(0, $l1->getHits());
$this->assertEquals(1, $l1->getMisses());
}

$myaddr2 = new Address('mybin', 'mykey2');
$l2->set('anypool', $myaddr2, $invalid_object, null, [], true);
$this->assertNull($pool->get($myaddr2));
$this->assertNull($l1->get($myaddr2));
protected function performCaughtUnserializationOnGetTest($l2)
{
$l1 = new StaticL1();
$pool = new Integrated($l1, $l2);
$invalid_object = 'O:10:"HelloWorl":0:{}';
$myaddr = new Address('mybin', 'performCaughtUnserializationOnGetTest');
$l2->set('anypool', $myaddr, $invalid_object, null, [], true);
try {
$pool->get($myaddr);
$this->assertTrue(false); // Should not reach here.
} catch (UnserializationException $e) {
$this->assertEquals($invalid_object, $e->getSerializedData());

// The text of the exception should include the class name, bin, and key.
$this->assertRegExp('/^' . preg_quote('LCache\UnserializationException: Cache') . '/', strval($e));
$this->assertRegExp('/bin "' . preg_quote($myaddr->getBin()) . '"/', strval($e));
$this->assertRegExp('/key "' . preg_quote($myaddr->getKey()) . '"/', strval($e));
}
}

public function testDatabaseL2FailedUnserialization()
{
$this->createSchema();
$l2 = new DatabaseL2($this->dbh);
$this->performFailedUnserializationTest($l2);
$this->performCaughtUnserializationOnGetTest($l2);
}

public function testStaticL2FailedUnserialization()
{
$l2 = new StaticL2();
$this->performFailedUnserializationTest($l2);
$this->performCaughtUnserializationOnGetTest($l2);
}

// Callers should expect an UnserializationException.
protected function performFailedUnserializationOnGetTest($l2)
{
$l1 = new StaticL1();
$pool = new Integrated($l1, $l2);
$invalid_object = 'O:10:"HelloWorl":0:{}';
$myaddr = new Address('mybin', 'performFailedUnserializationOnGetTest');
$l2->set('anypool', $myaddr, $invalid_object, null, [], true);
$pool->get($myaddr);
}

/**
* @expectedException LCache\UnserializationException
*/
public function testDatabaseL2FailedUnserializationOnGet()
{
$this->createSchema();
$l2 = new DatabaseL2($this->dbh);
$this->performFailedUnserializationOnGetTest($l2);
}

/**
* @expectedException LCache\UnserializationException
*/
public function testStaticL2FailedUnserializationOnGet()
{
$l2 = new StaticL2();
$this->performFailedUnserializationOnGetTest($l2);
}

public function performGarbageCollectionTest($l2)
Expand Down