|
1
|
|
|
<?php |
|
2
|
|
|
namespace PHPDaemon\Examples; |
|
3
|
|
|
|
|
4
|
|
|
use PHPDaemon\HTTPRequest\Generic; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* @package Examples |
|
8
|
|
|
* @subpackage Memcache |
|
9
|
|
|
* |
|
10
|
|
|
* @author Vasily Zorin <[email protected]> |
|
11
|
|
|
*/ |
|
12
|
|
|
class ExampleWithMemcache extends \PHPDaemon\Core\AppInstance |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Creates Request. |
|
16
|
|
|
* @param object Request. |
|
17
|
|
|
* @param object Upstream application instance. |
|
18
|
|
|
* @return ExampleWithMemcacheRequest Request. |
|
19
|
|
|
*/ |
|
20
|
|
|
public function beginRequest($req, $upstream) |
|
21
|
|
|
{ |
|
22
|
|
|
return new ExampleWithMemcacheRequest($this, $upstream, $req); |
|
23
|
|
|
} |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
class ExampleWithMemcacheRequest extends Generic |
|
|
|
|
|
|
27
|
|
|
{ |
|
28
|
|
|
|
|
29
|
|
|
public $job; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Constructor. |
|
33
|
|
|
* @return void |
|
34
|
|
|
*/ |
|
35
|
|
|
public function init() |
|
36
|
|
|
{ |
|
37
|
|
|
try { |
|
38
|
|
|
$this->header('Content-Type: text/html'); |
|
39
|
|
|
} catch (\Exception $e) { |
|
|
|
|
|
|
40
|
|
|
} |
|
41
|
|
|
$req = $this; |
|
42
|
|
|
|
|
43
|
|
|
$job = $this->job = new \PHPDaemon\Core\ComplexJob(function () use ($req) { // called when job is done |
|
44
|
|
|
|
|
45
|
|
|
$req->wakeup(); // wake up the request immediately |
|
46
|
|
|
|
|
47
|
|
|
}); |
|
48
|
|
|
$memcache = \PHPDaemon\Clients\Memcache\Pool::getInstance(); |
|
49
|
|
|
|
|
50
|
|
|
$job('testquery', function ($name, $job) use ($memcache) { // registering job named 'testquery' |
|
51
|
|
|
|
|
52
|
|
|
$memcache->stats(function ($memcache) use ($name, $job) { // calling 'stats' |
|
53
|
|
|
|
|
54
|
|
|
$job->setResult($name, $memcache->result); // setting job result |
|
55
|
|
|
|
|
56
|
|
|
}); |
|
57
|
|
|
|
|
58
|
|
|
}); |
|
59
|
|
|
|
|
60
|
|
|
$job(); // let the fun begin |
|
61
|
|
|
|
|
62
|
|
|
$this->sleep(5, true); // setting timeout |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Called when request iterated. |
|
67
|
|
|
* @return integer Status. |
|
|
|
|
|
|
68
|
|
|
*/ |
|
69
|
|
View Code Duplication |
public function run() |
|
|
|
|
|
|
70
|
|
|
{ |
|
71
|
|
|
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" |
|
72
|
|
|
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
|
73
|
|
|
<html xmlns="http://www.w3.org/1999/xhtml"> |
|
74
|
|
|
<head> |
|
75
|
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> |
|
76
|
|
|
<title>Example with Memcache</title> |
|
77
|
|
|
</head> |
|
78
|
|
|
<body> |
|
79
|
|
|
<?php |
|
80
|
|
|
if ($r = $this->job->getResult('testquery')) { |
|
81
|
|
|
echo '<h1>It works! Be happy! ;-)</h1>Result of query: <pre>'; |
|
82
|
|
|
var_dump($r); |
|
|
|
|
|
|
83
|
|
|
echo '</pre>'; |
|
84
|
|
|
} else { |
|
85
|
|
|
echo '<h1>Something went wrong! We have no result.</h1>'; |
|
86
|
|
|
} |
|
87
|
|
|
echo '<br />Request (http) took: ' . round(microtime(true) - $this->attrs->server['REQUEST_TIME_FLOAT'], 6); |
|
88
|
|
|
?> |
|
89
|
|
|
</body> |
|
90
|
|
|
</html> |
|
91
|
|
|
<?php |
|
92
|
|
|
|
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.