Skip to content

Commit 5fdf770

Browse files
authored
Merge pull request #38 from WyriHaximus/master
Allow for cache adapter injection
2 parents 9cc5079 + 5a3ef5e commit 5fdf770

3 files changed

Lines changed: 62 additions & 5 deletions

File tree

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,21 @@ $loop->run();
5353
If the first call returns before the second, only one query will be executed.
5454
The second result will be served from cache.
5555

56+
### Custom cache adapter
57+
58+
By default, the above will use an in memory cache.
59+
60+
You can also specify a custom cache implementing [`CacheInterface`](https://github.com/reactphp/cache) to handle the record cache instead:
61+
62+
```php
63+
$cache = new React\Cache\ArrayCache();
64+
$loop = React\EventLoop\Factory::create();
65+
$factory = new React\Dns\Resolver\Factory();
66+
$dns = $factory->createCached('8.8.8.8', $loop, $cache);
67+
```
68+
69+
See also the wiki for possible [cache implementations](https://github.com/reactphp/react/wiki/Users#cache-implementations).
70+
5671
## Install
5772

5873
The recommended way to install this library is [through Composer](http://getcomposer.org).

src/Resolver/Factory.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace React\Dns\Resolver;
44

55
use React\Cache\ArrayCache;
6+
use React\Cache\CacheInterface;
67
use React\Dns\Query\Executor;
78
use React\Dns\Query\CachedExecutor;
89
use React\Dns\Query\RecordCache;
@@ -21,10 +22,14 @@ public function create($nameserver, LoopInterface $loop)
2122
return new Resolver($nameserver, $executor);
2223
}
2324

24-
public function createCached($nameserver, LoopInterface $loop)
25+
public function createCached($nameserver, LoopInterface $loop, CacheInterface $cache = null)
2526
{
27+
if (!($cache instanceof CacheInterface)) {
28+
$cache = new ArrayCache();
29+
}
30+
2631
$nameserver = $this->addPortToServerIfMissing($nameserver);
27-
$executor = $this->createCachedExecutor($loop);
32+
$executor = $this->createCachedExecutor($loop, $cache);
2833

2934
return new Resolver($nameserver, $executor);
3035
}
@@ -39,9 +44,9 @@ protected function createRetryExecutor(LoopInterface $loop)
3944
return new RetryExecutor($this->createExecutor($loop));
4045
}
4146

42-
protected function createCachedExecutor(LoopInterface $loop)
47+
protected function createCachedExecutor(LoopInterface $loop, CacheInterface $cache)
4348
{
44-
return new CachedExecutor($this->createRetryExecutor($loop), new RecordCache(new ArrayCache()));
49+
return new CachedExecutor($this->createRetryExecutor($loop), new RecordCache($cache));
4550
}
4651

4752
protected function addPortToServerIfMissing($nameserver)

tests/Resolver/FactoryTest.php

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,30 @@ public function createCachedShouldCreateResolverWithCachedExecutor()
3939
$resolver = $factory->createCached('8.8.8.8:53', $loop);
4040

4141
$this->assertInstanceOf('React\Dns\Resolver\Resolver', $resolver);
42-
$this->assertInstanceOf('React\Dns\Query\CachedExecutor', $this->getResolverPrivateMemberValue($resolver, 'executor'));
42+
$executor = $this->getResolverPrivateMemberValue($resolver, 'executor');
43+
$this->assertInstanceOf('React\Dns\Query\CachedExecutor', $executor);
44+
$recordCache = $this->getCachedExecutorPrivateMemberValue($executor, 'cache');
45+
$recordCacheCache = $this->getRecordCachePrivateMemberValue($recordCache, 'cache');
46+
$this->assertInstanceOf('React\Cache\CacheInterface', $recordCacheCache);
47+
$this->assertInstanceOf('React\Cache\ArrayCache', $recordCacheCache);
48+
}
49+
50+
/** @test */
51+
public function createCachedShouldCreateResolverWithCachedExecutorWithCustomCache()
52+
{
53+
$cache = $this->getMock('React\Cache\CacheInterface');
54+
$loop = $this->getMock('React\EventLoop\LoopInterface');
55+
56+
$factory = new Factory();
57+
$resolver = $factory->createCached('8.8.8.8:53', $loop, $cache);
58+
59+
$this->assertInstanceOf('React\Dns\Resolver\Resolver', $resolver);
60+
$executor = $this->getResolverPrivateMemberValue($resolver, 'executor');
61+
$this->assertInstanceOf('React\Dns\Query\CachedExecutor', $executor);
62+
$recordCache = $this->getCachedExecutorPrivateMemberValue($executor, 'cache');
63+
$recordCacheCache = $this->getRecordCachePrivateMemberValue($recordCache, 'cache');
64+
$this->assertInstanceOf('React\Cache\CacheInterface', $recordCacheCache);
65+
$this->assertSame($cache, $recordCacheCache);
4366
}
4467

4568
/**
@@ -75,4 +98,18 @@ private function getResolverPrivateMemberValue($resolver, $field)
7598
$reflector->setAccessible(true);
7699
return $reflector->getValue($resolver);
77100
}
101+
102+
private function getCachedExecutorPrivateMemberValue($resolver, $field)
103+
{
104+
$reflector = new \ReflectionProperty('React\Dns\Query\CachedExecutor', $field);
105+
$reflector->setAccessible(true);
106+
return $reflector->getValue($resolver);
107+
}
108+
109+
private function getRecordCachePrivateMemberValue($resolver, $field)
110+
{
111+
$reflector = new \ReflectionProperty('React\Dns\Query\RecordCache', $field);
112+
$reflector->setAccessible(true);
113+
return $reflector->getValue($resolver);
114+
}
78115
}

0 commit comments

Comments
 (0)