-
Notifications
You must be signed in to change notification settings - Fork 390
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
185 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
class CacheHandler { | ||
private $cache = array(); | ||
function handle($name, array &$args, stdClass $context, Closure $next) { | ||
if (isset($context->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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
class CacheHandler2 { | ||
private $cache = array(); | ||
function handle($request, stdClass $context, Closure $next) { | ||
if (isset($context->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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
class SizeHandler { | ||
private $message; | ||
public function __construct($message) { | ||
$this->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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
class StatHandler { | ||
private $message; | ||
public function __construct($message) { | ||
$this->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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
require_once "../../../vendor/autoload.php"; | ||
require_once '../logHandler.php'; | ||
require_once '../CacheHandler.php'; | ||
|
||
use Hprose\Client; | ||
use Hprose\InvokeSettings; | ||
|
||
$cacheSettings = new InvokeSettings(array("userdata" => 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")); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
require_once "../../../vendor/autoload.php"; | ||
require_once '../logHandler.php'; | ||
|
||
use Hprose\Socket\Server; | ||
|
||
function hello($name) { | ||
return "Hello $name!"; | ||
} | ||
|
||
$server = new Server('tcp://0.0.0.0:1143/'); | ||
$server->addFunction('hello'); | ||
$server->debug = true; | ||
$server->addInvokeHandler($logHandler); | ||
$server->start(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
require_once "../../../vendor/autoload.php"; | ||
require_once '../logHandler2.php'; | ||
|
||
use Hprose\Client; | ||
|
||
$client = Client::create('tcp://127.0.0.1:1143/', false); | ||
$client->addBeforeFilterHandler($logHandler2); | ||
var_dump($client->hello("world")); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
require_once "../../../vendor/autoload.php"; | ||
require_once '../logHandler2.php'; | ||
|
||
use Hprose\Socket\Server; | ||
|
||
function hello($name) { | ||
return "Hello $name!"; | ||
} | ||
|
||
$server = new Server('tcp://0.0.0.0:1143/'); | ||
$server->addFunction('hello'); | ||
$server->debug = true; | ||
$server->addBeforeFilterHandler($logHandler2); | ||
$server->start(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
require_once "../../../vendor/autoload.php"; | ||
require_once '../../filter/CompressFilter.php'; | ||
require_once '../SizeHandler.php'; | ||
require_once '../StatHandler.php'; | ||
require_once '../CacheHandler2.php'; | ||
|
||
use Hprose\Client; | ||
use Hprose\InvokeSettings; | ||
|
||
$cacheSettings = new InvokeSettings(array("userdata" => 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))); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
require_once "../../../vendor/autoload.php"; | ||
require_once '../../filter/CompressFilter.php'; | ||
require_once '../SizeHandler.php'; | ||
require_once '../StatHandler.php'; | ||
|
||
use Hprose\Socket\Server; | ||
|
||
$server = new Server('tcp://0.0.0.0:1143/'); | ||
$server->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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
use Hprose\Future; | ||
|
||
$logHandler2 = function($request, stdClass $context, Closure $next) { | ||
error_log($request); | ||
$response = $next($request, $context); | ||
Future\run('error_log', $response); | ||
return $response; | ||
}; |