-
Notifications
You must be signed in to change notification settings - Fork 326
/
Copy pathset-get-server.php
44 lines (35 loc) · 1.35 KB
/
set-get-server.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php
$server = new MemcachedServer();
class Storage {
private $values = array ();
public function set ($key, $value, $expiration) {
$this->values [$key] = array ('value' => $value,
'expires' => time () + $expiration);
}
public function get ($key) {
if (isset ($this->values [$key])) {
if ($this->values [$key] ['expires'] < time ()) {
unset ($this->values [$key]);
return null;
}
return $this->values [$key] ['value'];
}
else
return null;
}
}
$storage = new Storage ();
$server->on (Memcached::ON_GET,
function ($client_id, $key, &$value, &$flags, &$cas) use ($storage) {
echo "Getting key=[$key]" . PHP_EOL;
if (($value = $storage->get ($key)) != null)
return Memcached::RESPONSE_SUCCESS;
return Memcached::RESPONSE_KEY_ENOENT;
});
$server->on (Memcached::ON_SET,
function ($client_id, $key, $value, $flags, $expiration, $cas, &$result_cas) use ($storage) {
echo "Setting key=[$key] value=[$value]" . PHP_EOL;
$storage->set ($key, $value, $expiration);
return Memcached::RESPONSE_SUCCESS;
});
$server->run ("127.0.0.1:3434");