Skip to content

Commit b5ce538

Browse files
committedNov 15, 2013
Added more examples
1 parent 59a4c95 commit b5ce538

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed
 

Diff for: ‎server-example/set-get-server.php

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

Diff for: ‎server-example/set-get.php

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
$cache = new Memcached();
4+
$cache->setOption(Memcached::OPT_BINARY_PROTOCOL, true);
5+
$cache->setOption(Memcached::OPT_COMPRESSION, false);
6+
$cache->addServer('localhost', 3434);
7+
8+
$cache->set ('set_key1', 'This is the first key', 10);
9+
var_dump ($cache->get ('set_key1'));
10+
11+
$cache->set ('set_key2', 'This is the second key', 2);
12+
var_dump ($cache->get ('set_key2'));

0 commit comments

Comments
 (0)
Please sign in to comment.