Skip to content

Commit 86f02af

Browse files
authored
Merge pull request #46 from clue-labs/examples
Add examples
2 parents 751b312 + c1ee739 commit 86f02af

4 files changed

Lines changed: 84 additions & 1 deletion

File tree

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ $dns->resolve('igor.io')->then(function ($ip) {
2626
$loop->run();
2727
```
2828

29+
See also the [first example](examples).
30+
2931
Pending DNS queries can be cancelled by cancelling its pending promise like so:
3032

3133
```php
@@ -59,7 +61,11 @@ $loop->run();
5961
```
6062

6163
If the first call returns before the second, only one query will be executed.
62-
The second result will be served from cache.
64+
The second result will be served from an in memory cache.
65+
This is particularly useful for long running scripts where the same hostnames
66+
have to be looked up multiple times.
67+
68+
See also the [third example](examples).
6369

6470
### Custom cache adapter
6571

examples/01-one.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
use React\Dns\Resolver\Factory;
4+
5+
require __DIR__ . '/../vendor/autoload.php';
6+
7+
$loop = React\EventLoop\Factory::create();
8+
9+
$factory = new Factory();
10+
$resolver = $factory->create('8.8.8.8', $loop);
11+
12+
$name = isset($argv[1]) ? $argv[1] : 'www.google.com';
13+
14+
$resolver->resolve($name)->then(function ($ip) use ($name) {
15+
echo 'IP for ' . $name . ': ' . $ip . PHP_EOL;
16+
}, 'printf');
17+
18+
$loop->run();

examples/02-concurrent.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
use React\Dns\Resolver\Factory;
4+
5+
require __DIR__ . '/../vendor/autoload.php';
6+
7+
$loop = React\EventLoop\Factory::create();
8+
9+
$factory = new Factory();
10+
$resolver = $factory->create('8.8.8.8', $loop);
11+
12+
$names = array_slice($argv, 1);
13+
if (!$names) {
14+
$names = array('google.com', 'www.google.com', 'gmail.com');
15+
}
16+
17+
foreach ($names as $name) {
18+
$resolver->resolve($name)->then(function ($ip) use ($name) {
19+
echo 'IP for ' . $name . ': ' . $ip . PHP_EOL;
20+
}, 'printf');
21+
}
22+
23+
$loop->run();

examples/03-cached.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
use React\Dns\Resolver\Factory;
4+
5+
require __DIR__ . '/../vendor/autoload.php';
6+
7+
$loop = React\EventLoop\Factory::create();
8+
9+
$factory = new Factory();
10+
$resolver = $factory->createCached('8.8.8.8', $loop);
11+
12+
$name = isset($argv[1]) ? $argv[1] : 'www.google.com';
13+
14+
$resolver->resolve($name)->then(function ($ip) use ($name) {
15+
echo 'IP for ' . $name . ': ' . $ip . PHP_EOL;
16+
}, 'printf');
17+
18+
$loop->addTimer(1.0, function() use ($name, $resolver) {
19+
$resolver->resolve($name)->then(function ($ip) use ($name) {
20+
echo 'IP for ' . $name . ': ' . $ip . PHP_EOL;
21+
}, 'printf');
22+
});
23+
24+
$loop->addTimer(2.0, function() use ($name, $resolver) {
25+
$resolver->resolve($name)->then(function ($ip) use ($name) {
26+
echo 'IP for ' . $name . ': ' . $ip . PHP_EOL;
27+
}, 'printf');
28+
});
29+
30+
$loop->addTimer(3.0, function() use ($name, $resolver) {
31+
$resolver->resolve($name)->then(function ($ip) use ($name) {
32+
echo 'IP for ' . $name . ': ' . $ip . PHP_EOL;
33+
}, 'printf');
34+
});
35+
36+
$loop->run();

0 commit comments

Comments
 (0)