diff --git a/README.md b/README.md index 3f92cbe9..66e2fd9f 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,8 @@ $dns->resolve('igor.io')->then(function ($ip) { $loop->run(); ``` +See also the [first example](examples). + Pending DNS queries can be cancelled by cancelling its pending promise like so: ```php @@ -59,7 +61,11 @@ $loop->run(); ``` If the first call returns before the second, only one query will be executed. -The second result will be served from cache. +The second result will be served from an in memory cache. +This is particularly useful for long running scripts where the same hostnames +have to be looked up multiple times. + +See also the [third example](examples). ### Custom cache adapter diff --git a/examples/01-one.php b/examples/01-one.php new file mode 100644 index 00000000..70904581 --- /dev/null +++ b/examples/01-one.php @@ -0,0 +1,18 @@ +create('8.8.8.8', $loop); + +$name = isset($argv[1]) ? $argv[1] : 'www.google.com'; + +$resolver->resolve($name)->then(function ($ip) use ($name) { + echo 'IP for ' . $name . ': ' . $ip . PHP_EOL; +}, 'printf'); + +$loop->run(); diff --git a/examples/02-concurrent.php b/examples/02-concurrent.php new file mode 100644 index 00000000..6c53fbbc --- /dev/null +++ b/examples/02-concurrent.php @@ -0,0 +1,23 @@ +create('8.8.8.8', $loop); + +$names = array_slice($argv, 1); +if (!$names) { + $names = array('google.com', 'www.google.com', 'gmail.com'); +} + +foreach ($names as $name) { + $resolver->resolve($name)->then(function ($ip) use ($name) { + echo 'IP for ' . $name . ': ' . $ip . PHP_EOL; + }, 'printf'); +} + +$loop->run(); diff --git a/examples/03-cached.php b/examples/03-cached.php new file mode 100644 index 00000000..040a1515 --- /dev/null +++ b/examples/03-cached.php @@ -0,0 +1,36 @@ +createCached('8.8.8.8', $loop); + +$name = isset($argv[1]) ? $argv[1] : 'www.google.com'; + +$resolver->resolve($name)->then(function ($ip) use ($name) { + echo 'IP for ' . $name . ': ' . $ip . PHP_EOL; +}, 'printf'); + +$loop->addTimer(1.0, function() use ($name, $resolver) { + $resolver->resolve($name)->then(function ($ip) use ($name) { + echo 'IP for ' . $name . ': ' . $ip . PHP_EOL; + }, 'printf'); +}); + +$loop->addTimer(2.0, function() use ($name, $resolver) { + $resolver->resolve($name)->then(function ($ip) use ($name) { + echo 'IP for ' . $name . ': ' . $ip . PHP_EOL; + }, 'printf'); +}); + +$loop->addTimer(3.0, function() use ($name, $resolver) { + $resolver->resolve($name)->then(function ($ip) use ($name) { + echo 'IP for ' . $name . ': ' . $ip . PHP_EOL; + }, 'printf'); +}); + +$loop->run();