...
| Excerpt |
|---|
Redis is a useful solution when design systems on server , and it takes minimum time required required the minimum latency between the client and server for handling message. We can easily build a thread application if we use shell_exec based on & and wait. PHP also provides a solution named as pthreads. but unfortunately the pthreads extension cannot be used in a web server environment. Threading in PHP should therefore remain to CLI-based applications only. |
...
Below is a practical example to implement Redis application in Threadcomparing the normal and thread processing :
| Code Block |
|---|
<?php
$prev_time = 0;
function tick()
{
global $prev_time;
$cur_time = microtime(true);
if ($prev_time) $diff = $cur_time - $prev_time;
else $diff = "";
echo $cur_time . " (" . $diff . ")\n";
$prev_time = $cur_time;
}
function redis_set($host,$port,$key,$value,$run_in_thread=false)
{
$obj = [ "v" => $value ];
$value = str_replace( "\"", "\\\"", json_encode( $obj, JSON_UNESCAPED_UNICODE));
$cmd="redis-cli -h {$host} -p {$port} set \"{$key}\" \"{$value}\"";
if ($run_in_thread==true) $cmd .= " &";
$resp=trim( shell_exec( $cmd));
if ($resp=="OK") return NULL;
else
{
echo "{$resp}\n";
return $resp;
}
}
function redis_get($host,$port,$key)
{
$cmd = "redis-cli -h {$host} -p {$port} get \"{$key}\"";
$resp=trim(shell_exec( $cmd));
$obj=json_decode($resp,JSON_UNESCAPED_UNICODE);
return $obj['v'];
}
echo "Error testing\n";
echo redis_set( "10.0.1.10", 6379, "chun", "kang1");
echo redis_get( "10.0.1.10", 6379, "chun"). "\n";
echo "Test Redis call in Normal mode!!\n";
$prev_time = 0;
tick();
redis_set( "10.0.1.11", 6379, "chun", "kang1");
redis_set( "10.0.1.12", 6379, "chun", "kang2");
redis_set( "10.0.1.13", 6379, "chun", "kang3");
tick();
echo redis_get( "10.0.1.11", 6379, "chun"). "\n";
echo redis_get( "10.0.1.12", 6379, "chun"). "\n";
echo redis_get( "10.0.1.13", 6379, "chun"). "\n";
tick();
echo "\n";
echo "Test same things in Thread mode\n";
$prev_time = 0;
tick();
redis_set( "10.0.1.11", 6379, "chun", "thread-kang1", true);
redis_set( "10.0.1.12", 6379, "chun", "thread-kang2", true);
redis_set( "10.0.1.13", 6379, "chun", "thread-kang3", true);
tick();
shell_exec("wait"); // wait until all workers to complete the job
echo redis_get( "10.0.1.11", 6379, "chun"). "\n";
echo redis_get( "10.0.1.12", 6379, "chun"). "\n";
echo redis_get( "10.0.1.13", 6379, "chun"). "\n";
tick();
?> |
...