From 07b3fbf98501631bba963cf041f84be2e5dd32cc Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Wed, 1 Jun 2016 17:26:45 +0200 Subject: [PATCH 1/7] Allow for cache adapter injection --- src/Resolver/Factory.php | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/Resolver/Factory.php b/src/Resolver/Factory.php index a2aa801a..1e210a8e 100644 --- a/src/Resolver/Factory.php +++ b/src/Resolver/Factory.php @@ -3,6 +3,7 @@ namespace React\Dns\Resolver; use React\Cache\ArrayCache; +use React\Cache\CacheInterface; use React\Dns\Query\Executor; use React\Dns\Query\CachedExecutor; use React\Dns\Query\RecordCache; @@ -21,10 +22,14 @@ public function create($nameserver, LoopInterface $loop) return new Resolver($nameserver, $executor); } - public function createCached($nameserver, LoopInterface $loop) + public function createCached($nameserver, LoopInterface $loop, CacheInterface $cache = null) { + if (!($cache instanceof CacheInterface)) { + $cache = new ArrayCache(); + } + $nameserver = $this->addPortToServerIfMissing($nameserver); - $executor = $this->createCachedExecutor($loop); + $executor = $this->createCachedExecutor($loop, $cache); return new Resolver($nameserver, $executor); } @@ -39,9 +44,9 @@ protected function createRetryExecutor(LoopInterface $loop) return new RetryExecutor($this->createExecutor($loop)); } - protected function createCachedExecutor(LoopInterface $loop) + protected function createCachedExecutor(LoopInterface $loop, CacheInterface $cache) { - return new CachedExecutor($this->createRetryExecutor($loop), new RecordCache(new ArrayCache())); + return new CachedExecutor($this->createRetryExecutor($loop), new RecordCache($cache)); } protected function addPortToServerIfMissing($nameserver) From 137c65944bb1211f90ae265faed4cea72def5ef1 Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Thu, 2 Jun 2016 11:37:22 +0200 Subject: [PATCH 2/7] Added "Custom cache adapter" readme section --- README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/README.md b/README.md index d674a369..40bd6ed5 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,17 @@ $loop->run(); If the first call returns before the second, only one query will be executed. The second result will be served from cache. +### Custom cache adapter + +You can also specify a cache that [implements](https://github.com/reactphp/react/wiki/Users#cache-implmentations) [`CacheInterface`](https://github.com/reactphp/cache) to handle the record cache instead of the default in memory cache. + +```php +$cache = new React\Cache\ArrayCache(); +$loop = React\EventLoop\Factory::create(); +$factory = new React\Dns\Resolver\Factory(); +$dns = $factory->createCached('8.8.8.8', $loop, $cache); +``` + ## Install The recommended way to install this library is [through Composer](http://getcomposer.org). From ded323216d591713b4810c41f192b8c8135505d8 Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Thu, 2 Jun 2016 17:21:44 +0200 Subject: [PATCH 3/7] Typo in the cache implementations URL --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 40bd6ed5..fd271e9e 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,7 @@ The second result will be served from cache. ### Custom cache adapter -You can also specify a cache that [implements](https://github.com/reactphp/react/wiki/Users#cache-implmentations) [`CacheInterface`](https://github.com/reactphp/cache) to handle the record cache instead of the default in memory cache. +You can also specify a cache that [implements](https://github.com/reactphp/react/wiki/Users#cache-implementations) [`CacheInterface`](https://github.com/reactphp/cache) to handle the record cache instead of the default in memory cache. ```php $cache = new React\Cache\ArrayCache(); From b7702b0d6b23465bdac84e47068f1c9393406c7f Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Sat, 4 Jun 2016 15:44:19 +0200 Subject: [PATCH 4/7] Test that the passed cache is used by the record cache --- tests/Resolver/FactoryTest.php | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tests/Resolver/FactoryTest.php b/tests/Resolver/FactoryTest.php index d4f47719..b04bdded 100644 --- a/tests/Resolver/FactoryTest.php +++ b/tests/Resolver/FactoryTest.php @@ -42,6 +42,23 @@ public function createCachedShouldCreateResolverWithCachedExecutor() $this->assertInstanceOf('React\Dns\Query\CachedExecutor', $this->getResolverPrivateMemberValue($resolver, 'executor')); } + /** @test */ + public function createCachedShouldCreateResolverWithCachedExecutorWithCustomCache() + { + $cache = $this->getMock('React\Cache\CacheInterface'); + $loop = $this->getMock('React\EventLoop\LoopInterface'); + + $factory = new Factory(); + $resolver = $factory->createCached('8.8.8.8:53', $loop, $cache); + + $this->assertInstanceOf('React\Dns\Resolver\Resolver', $resolver); + $executor = $this->getResolverPrivateMemberValue($resolver, 'executor'); + $this->assertInstanceOf('React\Dns\Query\CachedExecutor', $executor); + $recordCache = $this->getCachedExecutorPrivateMemberValue($executor, 'cache'); + $this->assertInstanceOf('React\Cache\CacheInterface', $this->getRecordCachePrivateMemberValue($recordCache, 'cache')); + $this->assertSame($cache, $this->getRecordCachePrivateMemberValue($recordCache, 'cache')); + } + /** * @test * @dataProvider factoryShouldAddDefaultPortProvider @@ -75,4 +92,18 @@ private function getResolverPrivateMemberValue($resolver, $field) $reflector->setAccessible(true); return $reflector->getValue($resolver); } + + private function getCachedExecutorPrivateMemberValue($resolver, $field) + { + $reflector = new \ReflectionProperty('React\Dns\Query\CachedExecutor', $field); + $reflector->setAccessible(true); + return $reflector->getValue($resolver); + } + + private function getRecordCachePrivateMemberValue($resolver, $field) + { + $reflector = new \ReflectionProperty('React\Dns\Query\RecordCache', $field); + $reflector->setAccessible(true); + return $reflector->getValue($resolver); + } } From bf511a29d0c889e697014ba61de14f3f418c90cb Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Sat, 4 Jun 2016 15:45:40 +0200 Subject: [PATCH 5/7] Assert the default is still to use the ArrayCache --- tests/Resolver/FactoryTest.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/Resolver/FactoryTest.php b/tests/Resolver/FactoryTest.php index b04bdded..071a3af4 100644 --- a/tests/Resolver/FactoryTest.php +++ b/tests/Resolver/FactoryTest.php @@ -39,7 +39,11 @@ public function createCachedShouldCreateResolverWithCachedExecutor() $resolver = $factory->createCached('8.8.8.8:53', $loop); $this->assertInstanceOf('React\Dns\Resolver\Resolver', $resolver); - $this->assertInstanceOf('React\Dns\Query\CachedExecutor', $this->getResolverPrivateMemberValue($resolver, 'executor')); + $executor = $this->getResolverPrivateMemberValue($resolver, 'executor'); + $this->assertInstanceOf('React\Dns\Query\CachedExecutor', $executor); + $recordCache = $this->getCachedExecutorPrivateMemberValue($executor, 'cache'); + $this->assertInstanceOf('React\Cache\CacheInterface', $this->getRecordCachePrivateMemberValue($recordCache, 'cache')); + $this->assertInstanceOf('React\Cache\ArrayCache', $this->getRecordCachePrivateMemberValue($recordCache, 'cache')); } /** @test */ From 8ff807a98cadba8c1a05b9e9179fd8ac0d8b9690 Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Sat, 4 Jun 2016 15:49:11 +0200 Subject: [PATCH 6/7] Reduced method call overhead a bit --- tests/Resolver/FactoryTest.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/tests/Resolver/FactoryTest.php b/tests/Resolver/FactoryTest.php index 071a3af4..6b3b9ac8 100644 --- a/tests/Resolver/FactoryTest.php +++ b/tests/Resolver/FactoryTest.php @@ -42,8 +42,9 @@ public function createCachedShouldCreateResolverWithCachedExecutor() $executor = $this->getResolverPrivateMemberValue($resolver, 'executor'); $this->assertInstanceOf('React\Dns\Query\CachedExecutor', $executor); $recordCache = $this->getCachedExecutorPrivateMemberValue($executor, 'cache'); - $this->assertInstanceOf('React\Cache\CacheInterface', $this->getRecordCachePrivateMemberValue($recordCache, 'cache')); - $this->assertInstanceOf('React\Cache\ArrayCache', $this->getRecordCachePrivateMemberValue($recordCache, 'cache')); + $recordCacheCache = $this->getRecordCachePrivateMemberValue($recordCache, 'cache'); + $this->assertInstanceOf('React\Cache\CacheInterface', $recordCacheCache); + $this->assertInstanceOf('React\Cache\ArrayCache', $recordCacheCache); } /** @test */ @@ -59,8 +60,9 @@ public function createCachedShouldCreateResolverWithCachedExecutorWithCustomCach $executor = $this->getResolverPrivateMemberValue($resolver, 'executor'); $this->assertInstanceOf('React\Dns\Query\CachedExecutor', $executor); $recordCache = $this->getCachedExecutorPrivateMemberValue($executor, 'cache'); - $this->assertInstanceOf('React\Cache\CacheInterface', $this->getRecordCachePrivateMemberValue($recordCache, 'cache')); - $this->assertSame($cache, $this->getRecordCachePrivateMemberValue($recordCache, 'cache')); + $recordCacheCache = $this->getRecordCachePrivateMemberValue($recordCache, 'cache'); + $this->assertInstanceOf('React\Cache\CacheInterface', $recordCacheCache); + $this->assertSame($cache, $recordCacheCache); } /** From 5a3ef5ee459f92de67dd9e696d303262700175c5 Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Sat, 4 Jun 2016 16:12:16 +0200 Subject: [PATCH 7/7] More explicit wording: https://github.com/reactphp/dns/pull/38/files/8ff807a98cadba8c1a05b9e9179fd8ac0d8b9690#r65802871 --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index fd271e9e..a044df7b 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,9 @@ The second result will be served from cache. ### Custom cache adapter -You can also specify a cache that [implements](https://github.com/reactphp/react/wiki/Users#cache-implementations) [`CacheInterface`](https://github.com/reactphp/cache) to handle the record cache instead of the default in memory cache. +By default, the above will use an in memory cache. + +You can also specify a custom cache implementing [`CacheInterface`](https://github.com/reactphp/cache) to handle the record cache instead: ```php $cache = new React\Cache\ArrayCache(); @@ -64,6 +66,8 @@ $factory = new React\Dns\Resolver\Factory(); $dns = $factory->createCached('8.8.8.8', $loop, $cache); ``` +See also the wiki for possible [cache implementations](https://github.com/reactphp/react/wiki/Users#cache-implementations). + ## Install The recommended way to install this library is [through Composer](http://getcomposer.org).