Skip to content

Commit

Permalink
Limit cache size to 256 results by default (#127)
Browse files Browse the repository at this point in the history
Limit cache size to 256 results by default
  • Loading branch information
WyriHaximus authored Jul 7, 2019
2 parents 2192705 + 9b52f6c commit fbab3df
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 43 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": "MIT",
"require": {
"php": ">=5.3.0",
"react/cache": "^1.0 || ^0.6 || ^0.5 || ^0.4 || ^0.3",
"react/cache": "^1.0 || ^0.6 || ^0.5",
"react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3.5",
"react/promise": "^2.1 || ^1.2.1",
"react/promise-timer": "^1.2",
Expand Down
8 changes: 2 additions & 6 deletions src/Query/RecordCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function lookup(Query $query)
return $this->cache
->get($id)
->then(function ($value) use ($query, $expiredAt) {
// cache 0.5+ resolves with null on cache miss, return explicit cache miss here
// reject on cache miss
if ($value === null) {
return Promise\reject();
}
Expand Down Expand Up @@ -86,17 +86,13 @@ public function storeRecord($currentTime, Record $record)
->get($id)
->then(
function ($value) {
// return empty bag on cache miss
if ($value === null) {
// cache 0.5+ cache miss resolves with null, return empty bag here
return new RecordBag();
}

// reuse existing bag on cache hit to append new record to it
return unserialize($value);
},
function ($e) {
// legacy cache < 0.5 cache miss rejects promise, return empty bag here
return new RecordBag();
}
)
->then(function (RecordBag $recordBag) use ($id, $currentTime, $record, $cache) {
Expand Down
3 changes: 2 additions & 1 deletion src/Resolver/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ public function create($nameserver, LoopInterface $loop)

public function createCached($nameserver, LoopInterface $loop, CacheInterface $cache = null)
{
// default to keeping maximum of 256 responses in cache unless explicitly given
if (!($cache instanceof CacheInterface)) {
$cache = new ArrayCache();
$cache = new ArrayCache(256);
}

$nameserver = $this->addPortToServerIfMissing($nameserver);
Expand Down
37 changes: 2 additions & 35 deletions tests/Query/RecordCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class RecordCacheTest extends TestCase
* @covers React\Dns\Query\RecordCache
* @test
*/
public function lookupOnNewCacheMissShouldReturnNull()
public function lookupOnCacheMissShouldReturnNull()
{
$query = new Query('igor.io', Message::TYPE_A, Message::CLASS_IN, 1345656451);

Expand All @@ -30,23 +30,6 @@ public function lookupOnNewCacheMissShouldReturnNull()
$this->assertInstanceOf('React\Promise\RejectedPromise', $promise);
}

/**
* @covers React\Dns\Query\RecordCache
* @test
*/
public function lookupOnLegacyCacheMissShouldReturnNull()
{
$query = new Query('igor.io', Message::TYPE_A, Message::CLASS_IN, 1345656451);

$base = $this->getMockBuilder('React\Cache\CacheInterface')->getMock();
$base->expects($this->once())->method('get')->willReturn(\React\Promise\reject());

$cache = new RecordCache($base);
$promise = $cache->lookup($query);

$this->assertInstanceOf('React\Promise\RejectedPromise', $promise);
}

/**
* @covers React\Dns\Query\RecordCache
* @test
Expand All @@ -68,7 +51,7 @@ public function storeRecordPendingCacheDoesNotSetCache()
* @covers React\Dns\Query\RecordCache
* @test
*/
public function storeRecordOnNewCacheMissSetsCache()
public function storeRecordOnCacheMissSetsCache()
{
$query = new Query('igor.io', Message::TYPE_A, Message::CLASS_IN, 1345656451);

Expand All @@ -80,22 +63,6 @@ public function storeRecordOnNewCacheMissSetsCache()
$cache->storeRecord($query->currentTime, new Record('igor.io', Message::TYPE_A, Message::CLASS_IN, 3600, '178.79.169.131'));
}

/**
* @covers React\Dns\Query\RecordCache
* @test
*/
public function storeRecordOnOldCacheMissSetsCache()
{
$query = new Query('igor.io', Message::TYPE_A, Message::CLASS_IN, 1345656451);

$base = $this->getMockBuilder('React\Cache\CacheInterface')->getMock();
$base->expects($this->once())->method('get')->willReturn(\React\Promise\reject());
$base->expects($this->once())->method('set')->with($this->isType('string'), $this->isType('string'));

$cache = new RecordCache($base);
$cache->storeRecord($query->currentTime, new Record('igor.io', Message::TYPE_A, Message::CLASS_IN, 3600, '178.79.169.131'));
}

/**
* @covers React\Dns\Query\RecordCache
* @test
Expand Down

0 comments on commit fbab3df

Please sign in to comment.