diff --git a/src/Integrated.php b/src/Integrated.php index 2104acf..d91ed33 100644 --- a/src/Integrated.php +++ b/src/Integrated.php @@ -15,8 +15,17 @@ public function __construct($l1, $l2, $overhead_threshold = null) $this->overhead_threshold = $overhead_threshold; } - public function set(Address $address, $value, $ttl = null, array $tags = []) + public function set(Address $address, $value, $ttl_or_expiration = null, array $tags = []) { + $expiration = null; + if (!is_null($ttl_or_expiration)) { + if ($ttl_or_expiration < $_SERVER['REQUEST_TIME']) { + $expiration = $_SERVER['REQUEST_TIME'] + $ttl_or_expiration; + } else { + $expiration = $ttl_or_expiration; + } + } + if (!is_null($this->overhead_threshold)) { $key_overhead = $this->l1->getKeyOverhead($address); @@ -32,7 +41,7 @@ public function set(Address $address, $value, $ttl = null, array $tags = []) // and create a local negative cache entry. $event_id = $this->l2->delete($this->l1->getPool(), $address); if (!is_null($event_id)) { - $this->l1->set($event_id, $address, $value, $ttl); + $this->l1->set($event_id, $address, $value, $expiration); } // Retain this negative cache item for a number of minutes @@ -44,7 +53,6 @@ public function set(Address $address, $value, $ttl = null, array $tags = []) } } - $expiration = is_null($ttl) ? null : $_SERVER['REQUEST_TIME'] + $ttl; $event_id = $this->l2->set($this->l1->getPool(), $address, $value, $expiration, $tags); if (!is_null($event_id)) { $this->l1->set($event_id, $address, $value, $expiration); diff --git a/tests/LCacheTest.php b/tests/LCacheTest.php index f014fa0..411e013 100644 --- a/tests/LCacheTest.php +++ b/tests/LCacheTest.php @@ -841,6 +841,12 @@ public function testAPCuL1Expiration() $myaddr2 = new Address('mybin', 'mykey2'); $l1->set(0, $myaddr2, 'value', $_SERVER['REQUEST_TIME'] - 1); $this->assertNull($l1->get($myaddr2)); + + // Setting an TTL/expiration more than request time should be treated + // as an expiration. + $pool->set($myaddr, 'value', $_SERVER['REQUEST_TIME'] + 1); + $this->assertEquals('value', $pool->get($myaddr)); + $this->assertEquals($_SERVER['REQUEST_TIME'] + 1, $l1->getEntry($myaddr)->expiration); } /**