diff --git a/examples/src/middleware/CacheHandler.php b/examples/src/middleware/CacheHandler.php new file mode 100644 index 00000000..2b163c7a --- /dev/null +++ b/examples/src/middleware/CacheHandler.php @@ -0,0 +1,22 @@ +userdata->cache)) { + $key = hprose_serialize($args); + if (isset($this->cache[$name])) { + if (isset($this->cache[$name][$key])) { + return $this->cache[$name][$key]; + } + } + else { + $this->cache[$name] = array(); + } + $result = $next($name, $args, $context); + $this->cache[$name][$key] = $result; + return $result; + } + return $next($name, $args, $context); + } +} diff --git a/examples/src/middleware/CacheHandler2.php b/examples/src/middleware/CacheHandler2.php new file mode 100644 index 00000000..2dc3a5d7 --- /dev/null +++ b/examples/src/middleware/CacheHandler2.php @@ -0,0 +1,16 @@ +userdata->cache)) { + if (isset($this->cache[$request])) { + return $this->cache[$request]; + } + $response = $next($request, $context); + $this->cache[$request] = $response; + return $response; + } + return $next($request, $context); + } +} diff --git a/examples/src/middleware/SizeHandler.php b/examples/src/middleware/SizeHandler.php new file mode 100644 index 00000000..955a1fed --- /dev/null +++ b/examples/src/middleware/SizeHandler.php @@ -0,0 +1,19 @@ +message = $message; + } + public function asynchandle($request, stdClass $context, Closure $next) { + error_log($this->message . ' request size: ' . strlen($request)); + $response = (yield $next($request, $context)); + error_log($this->message . ' response size: ' . strlen($response)); + } + public function synchandle($request, stdClass $context, Closure $next) { + error_log($this->message . ' request size: ' . strlen($request)); + $response = $next($request, $context); + error_log($this->message . ' response size: ' . strlen($response)); + return $response; + } +} diff --git a/examples/src/middleware/StatHandler.php b/examples/src/middleware/StatHandler.php new file mode 100644 index 00000000..59649325 --- /dev/null +++ b/examples/src/middleware/StatHandler.php @@ -0,0 +1,21 @@ +message = $message; + } + public function asynchandle($request, stdClass $context, Closure $next) { + $start = microtime(true); + yield $next($request, $context); + $end = microtime(true); + error_log($this->message . ': It takes ' . ($end - $start) . 'ms.'); + } + public function synchandle($request, stdClass $context, Closure $next) { + $start = microtime(true); + $response = $next($request, $context); + $end = microtime(true); + error_log($this->message . ': It takes ' . ($end - $start) . 'ms.'); + return $response; + } +} diff --git a/examples/src/middleware/exam3/Client.php b/examples/src/middleware/exam3/Client.php new file mode 100644 index 00000000..c6f3b2bd --- /dev/null +++ b/examples/src/middleware/exam3/Client.php @@ -0,0 +1,16 @@ + array("cache" => true))); +$client = Client::create('tcp://127.0.0.1:1143/', false); +$client->addInvokeHandler(array(new CacheHandler(), 'handle')); +$client->addInvokeHandler($logHandler); +var_dump($client->hello("cache world", $cacheSettings)); +var_dump($client->hello("cache world", $cacheSettings)); +var_dump($client->hello("no cache world")); +var_dump($client->hello("no cache world")); diff --git a/examples/src/middleware/exam3/Server.php b/examples/src/middleware/exam3/Server.php new file mode 100644 index 00000000..23d8083f --- /dev/null +++ b/examples/src/middleware/exam3/Server.php @@ -0,0 +1,16 @@ +addFunction('hello'); +$server->debug = true; +$server->addInvokeHandler($logHandler); +$server->start(); + diff --git a/examples/src/middleware/exam4/Client.php b/examples/src/middleware/exam4/Client.php new file mode 100644 index 00000000..ac213158 --- /dev/null +++ b/examples/src/middleware/exam4/Client.php @@ -0,0 +1,9 @@ +addBeforeFilterHandler($logHandler2); +var_dump($client->hello("world")); diff --git a/examples/src/middleware/exam4/Server.php b/examples/src/middleware/exam4/Server.php new file mode 100644 index 00000000..ce3cf0d4 --- /dev/null +++ b/examples/src/middleware/exam4/Server.php @@ -0,0 +1,16 @@ +addFunction('hello'); +$server->debug = true; +$server->addBeforeFilterHandler($logHandler2); +$server->start(); + diff --git a/examples/src/middleware/exam5/Client.php b/examples/src/middleware/exam5/Client.php new file mode 100644 index 00000000..e27c75db --- /dev/null +++ b/examples/src/middleware/exam5/Client.php @@ -0,0 +1,24 @@ + array("cache" => true))); + +$client = Client::create('tcp://127.0.0.1:1143/', false); +$client->addBeforeFilterHandler(array(new CacheHandler2(), 'handle')) + ->addBeforeFilterHandler(array(new StatHandler('BeforeFilter'), 'synchandle')) + ->addBeforeFilterHandler(array(new SizeHandler('Non compressed'), 'synchandle')) + ->addFilter(new CompressFilter()) + ->addAfterFilterHandler(array(new StatHandler('AfterFilter'), 'synchandle')) + ->addAfterFilterHandler(array(new SizeHandler('compressed'), 'synchandle')); + +$value = range(0, 99999); +var_dump(count($client->echo($value, $cacheSettings))); +var_dump(count($client->echo($value, $cacheSettings))); + diff --git a/examples/src/middleware/exam5/Server.php b/examples/src/middleware/exam5/Server.php new file mode 100644 index 00000000..c172a5c5 --- /dev/null +++ b/examples/src/middleware/exam5/Server.php @@ -0,0 +1,16 @@ +addFunction(function($value) { return $value; }, 'echo') + ->addBeforeFilterHandler(array(new StatHandler("BeforeFilter"), 'asynchandle')) + ->addBeforeFilterHandler(array(new SizeHandler("compressedr"), 'asynchandle')) + ->addFilter(new CompressFilter()) + ->addAfterFilterHandler(array(new StatHandler("AfterFilter"), 'asynchandle')) + ->addAfterFilterHandler(array(new SizeHandler("Non compressed"), 'asynchandle')) + ->start(); diff --git a/examples/src/middleware/logHandler2.php b/examples/src/middleware/logHandler2.php new file mode 100644 index 00000000..7fe63fa8 --- /dev/null +++ b/examples/src/middleware/logHandler2.php @@ -0,0 +1,10 @@ +