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 " );
0 commit comments