At this point we have a functional memcached client, but since people generally use memcached like a distributed hash table, what we have is not yet sufficient - we need a frontend to multiple instances of memcached.

The idea is simple - have a structure holding multiple memcached clients:

All the setters are straight forward - hash the key to the underlying memcached client, and then call the corresponding single-instance methods on the parameters.

The getters are a bit trickier - since the getters can take in multiple keys as the parameter, and each of the keys might hash to a different underlying client, we should try to group the keys by their hash, and the pass the keys to the correct target, and finally merge the responses:

And finally - memcached/dht-flush-all! does not require hashing; it simply calls the underlying flush-all command for each of the client. But according to the memcached's spec, it is best to stagger the flushing of the instances, so the database will not all of the sudden be overloaded. This pattern is directly built into the memcached/dht-flush-all!, and you can control the staggering by passing in an interval value that determines the gaps between the flushes.