template<class T>
struct evo::async::MemcachedServer< T >
Implements Memcached protocol for an async server.
Usage:
- Implement a
HANDLER
class with the event (callback) methods
- inherit MemcachedServerHandlerBase and implement desired methods – see event methods there
- implement
Global
and Shared
nested structs if needed – see Asynchronous I/O for details
- Shared::on_init() is the place to start back-end connections
- implement constructor with expected arguments:
Global&, Shared&
- Define a server type (via typedef) using
MemcachedServer<HANDLER>::Server
– a shortcut for AsyncServer< MemcachedServer<HANDLER> >
- Create a listener Socket to use with the server
- Instantiate a server and then:
- Call run() to run the server
- A handler or another thread may call shutdown() to stop the server
- Example
A simple single-threaded memcached server:
struct Shared : SimpleSharedBase<> {
};
}
StoreResult on_store(StoreParams& params,
SubString& value, Command command, uint64 cas_id) {
switch(command) {
case cSET:
shared.map[params.key] = value;
break;
default:
send_error("Not supported");
return rtHANDLED;
}
}
ResponseType on_get(
const SubString& key, GetAdvParams* adv_params) {
const String* val = shared.map.find(key);
if (val != NULL)
send_value(key, *val);
return rtHANDLED;
}
};
int main() {
const ushort PORT = 11211;
const ulong RD_TIMEOUT_MS = 5000;
const ulong WR_TIMEOUT_MS = 1000;
try {
Server server;
server.run(listener);
return 0;
}