1. Go to this page and download the library: Download sebk/small-swoole-patterns library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
sebk / small-swoole-patterns example snippets
use Sebk\SmallSwoolePatterns\Array\Map;
// First map
$map1 = (new Map([1, 2, 3], function($value) {
return $value - 1;
}))->run();
// Second map
$map2 = (new Map([4, 6, 8], function($value) {
return $value / 2
}))->run();
// Wait two maps finished
$map1->wait();
$map2->wait();
// Merg results and dump result;
$result = array_merge($map1, $map2);
var_dump($result);
use Sebk\SmallSwoolePatterns\Observable\Observable;
// Create callback
$get = function ($host): string {
return (new Coroutine\Http\Client($host, 80, true))
->get('/');
};
// Create observer
$getObserver = (new Observable($get))
->subscribe(function(string $html) {
// left method handle result
echo $html;
}, function(\Exception $e) {
// Right method handle exceptions
echo 'Can\'t get url : ' . $e->getMessage();
})
;
$getObserver
->run('www.google.com')
->run('www.yahoo.com')
->run('www.qwant.com')
;
$getObserver->wait();
$pool = new \Sebk\SmallSwoolePatterns\Pool\Pool(
new \Sebk\SmallSwoolePatterns\Manager\Connection\PRedisClientManager('tcp://my-redis.net'),
10,
100
);
$client = $pool->get();
$client->get('my-app:key')
$pool->put($client);
$pool = new \Sebk\SmallSwoolePatterns\Pool\Pool(
new \Sebk\SmallSwoolePatterns\Manager\Connection\PRedisClientManager('tcp://my-redis.net'),
10,
100
);
(new \Sebk\SmallSwoolePatterns\Array\Map(range(1, 100), ($i) use($pool) => {
$client = $pool->get();
$client->put('my-app:sum:' . $i, $i +$i);
$pool->put($client);
}));
($pool = new \Sebk\SmallSwoolePatterns\Pool\Pool(
new \Sebk\SmallSwoolePatterns\Manager\Connection\PRedisClientManager('tcp://my-redis.net'),
10,
100
))->activateRateController(100, 10);