|
| 1 | +<?php |
| 2 | + |
| 3 | +$server = new MemcachedServer(); |
| 4 | + |
| 5 | +$server->on (Memcached::ON_CONNECT, |
| 6 | + function ($remote_addr) { |
| 7 | + echo "Incoming connection from {$remote_addr}" . PHP_EOL; |
| 8 | + return Memcached::RESPONSE_SUCCESS; |
| 9 | + }); |
| 10 | + |
| 11 | +$server->on (Memcached::ON_ADD, |
| 12 | + function ($client_id, $key, $value, $flags, $expiration, &$cas) { |
| 13 | + echo "client_id=[$client_id]: Add key=[$key], value=[$value], flags=[$flags], expiration=[$expiration]" . PHP_EOL; |
| 14 | + $cas = 15; |
| 15 | + return Memcached::RESPONSE_SUCCESS; |
| 16 | + }); |
| 17 | + |
| 18 | +$server->on (Memcached::ON_APPEND, |
| 19 | + function ($client_id, $key, $value, $cas, &$result_cas) { |
| 20 | + echo "client_id=[$client_id]: Append key=[$key], value=[$value], cas=[$cas]" . PHP_EOL; |
| 21 | + return Memcached::RESPONSE_SUCCESS; |
| 22 | + }); |
| 23 | + |
| 24 | +$server->on (Memcached::ON_PREPEND, |
| 25 | + function ($client_id, $key, $value, $cas, &$result_cas) { |
| 26 | + echo "client_id=[$client_id]: Prepend key=[$key], value=[$value], cas=[$cas]" . PHP_EOL; |
| 27 | + return Memcached::RESPONSE_SUCCESS; |
| 28 | + }); |
| 29 | + |
| 30 | +$server->on (Memcached::ON_INCREMENT, |
| 31 | + function ($client_id, $key, $delta, $initial, $expiration, &$result, &$result_cas) { |
| 32 | + echo "client_id=[$client_id]: Incrementing key=[$key], delta=[$delta], initial=[$initial], expiration=[$expiration]" . PHP_EOL; |
| 33 | + return Memcached::RESPONSE_SUCCESS; |
| 34 | + }); |
| 35 | + |
| 36 | +$server->on (Memcached::ON_DECREMENT, |
| 37 | + function ($client_id, $key, $delta, $initial, $expiration, &$result, &$result_cas) { |
| 38 | + echo "client_id=[$client_id]: Decrementing key=[$key], delta=[$delta], initial=[$initial], expiration=[$expiration]" . PHP_EOL; |
| 39 | + return Memcached::RESPONSE_SUCCESS; |
| 40 | + }); |
| 41 | + |
| 42 | +$server->on (Memcached::ON_DELETE, |
| 43 | + function ($client_id, $key, $cas) { |
| 44 | + echo "client_id=[$client_id]: Delete key=[$key], cas=[$cas]" . PHP_EOL; |
| 45 | + return Memcached::RESPONSE_SUCCESS; |
| 46 | + }); |
| 47 | + |
| 48 | +$server->on (Memcached::ON_FLUSH, |
| 49 | + function ($client_id, $when) { |
| 50 | + echo "client_id=[$client_id]: Flush when=[$when]" . PHP_EOL; |
| 51 | + return Memcached::RESPONSE_SUCCESS; |
| 52 | + }); |
| 53 | + |
| 54 | +$server->on (Memcached::ON_GET, |
| 55 | + function ($client_id, $key, &$value, &$flags, &$cas) { |
| 56 | + echo "client_id=[$client_id]: Get key=[$key]" . PHP_EOL; |
| 57 | + $value = "Hello to you client!"; |
| 58 | + return Memcached::RESPONSE_SUCCESS; |
| 59 | + }); |
| 60 | + |
| 61 | +$server->on (Memcached::ON_NOOP, |
| 62 | + function ($client_id) { |
| 63 | + echo "client_id=[$client_id]: Noop" . PHP_EOL; |
| 64 | + return Memcached::RESPONSE_SUCCESS; |
| 65 | + }); |
| 66 | + |
| 67 | +$server->on (Memcached::ON_REPLACE, |
| 68 | + function ($client_id, $key, $value, $flags, $expiration, $cas, &$result_cas) { |
| 69 | + echo "client_id=[$client_id]: Replace key=[$key], value=[$value], flags=[$flags], expiration=[$expiration], cas=[$cas]" . PHP_EOL; |
| 70 | + return Memcached::RESPONSE_SUCCESS; |
| 71 | + }); |
| 72 | + |
| 73 | +$server->on (Memcached::ON_SET, |
| 74 | + function ($client_id, $key, $value, $flags, $expiration, $cas, &$result_cas) { |
| 75 | + echo "client_id=[$client_id]: Set key=[$key], value=[$value], flags=[$flags], expiration=[$expiration], cas=[$cas]" . PHP_EOL; |
| 76 | + return Memcached::RESPONSE_SUCCESS; |
| 77 | + }); |
| 78 | + |
| 79 | +$server->on (Memcached::ON_STAT, |
| 80 | + function ($client_id, $key, &$value) { |
| 81 | + echo "client_id=[$client_id]: Stat key=[$key]" . PHP_EOL; |
| 82 | + $value = "Stat reply"; |
| 83 | + return Memcached::RESPONSE_SUCCESS; |
| 84 | + }); |
| 85 | + |
| 86 | +$server->on (Memcached::ON_QUIT, |
| 87 | + function ($client_id) { |
| 88 | + echo "client_id=[$client_id]: Client quit" . PHP_EOL; |
| 89 | + return Memcached::RESPONSE_SUCCESS; |
| 90 | + }); |
| 91 | + |
| 92 | +$addr = ($_SERVER['argv'][1] ?? "127.0.0.1:3434"); |
| 93 | +echo "Listening on $addr" . PHP_EOL; |
| 94 | +$server->run($addr); |
0 commit comments