class MemcacheClient(memcache.Client):
    """ A memcache subclass. It currently allows you to add a new host at run
time.

only 1/N th cache is wiped out, where N is the number of cache servers.
"""
    def __init__(self, replicas=1,*args, **kwargs):
        self.consistent_hash=ConsistentHash(replicas=replicas)
        super(MemcacheClient, self).__init__(*args, **kwargs)
        

    def _get_server(self, key):
        """ Current implementation of Memcac he client"""
        for i in range(memcache.Client._SERVER_RETRIES):
          server = self.buckets[self.get_server_index(key)]
          if server.connect():
            return server, key
          return None, None
        
    def set_servers(self, servers):
        ret=super(MemcacheClient, self).set_servers(servers)
        self.consistent_hash.setup_servers(servers=self.servers)
        return ret
   
    def get_server_index(self,key):
        '''Returns the number of the machine which key gets sent to.'''
        return self.consistent_hash.get_machine(key)
        

    def add_server(self, server):
        """ Adds a host at runtime to client"""
        # Create a new host entry
        server = memcache._Host(
            server, self.debug, dead_retry=self.dead_retry,
            socket_timeout=self.socket_timeout,
            flush_on_reconnect=self.flush_on_reconnect
        )
        # Add this to our server choices
        self.servers.append(server)
        # Update our buckets
        self.buckets.append(server)
        self.consistent_hash.add_machine(server,len(self.servers)-1)
class MemcacheClient(memcache.Client):
    """ A memcache subclass. It currently allows you to add a new host at run
time.

only 1/N th cache is wiped out, where N is the number of cache servers.
"""
    def __init__(self, replicas=1, *args, **kwargs):
        self.consistent_hash = ConsistentHash(replicas=replicas)
        super(MemcacheClient, self).__init__(*args, **kwargs)

    def _get_server(self, key):
        """ Current implementation of Memcac he client"""
        for i in range(memcache.Client._SERVER_RETRIES):
            server = self.buckets[self.get_server_index(key)]
            if server.connect():
                return server, key
            return None, None

    def set_servers(self, servers):
        ret = super(MemcacheClient, self).set_servers(servers)
        self.consistent_hash.setup_servers(servers=self.servers)
        return ret

    def get_server_index(self, key):
        '''Returns the number of the machine which key gets sent to.'''
        return self.consistent_hash.get_machine(key)

    def add_server(self, server):
        """ Adds a host at runtime to client"""
        # Create a new host entry
        server = memcache._Host(server,
                                self.debug,
                                dead_retry=self.dead_retry,
                                socket_timeout=self.socket_timeout,
                                flush_on_reconnect=self.flush_on_reconnect)
        # Add this to our server choices
        self.servers.append(server)
        # Update our buckets
        self.buckets.append(server)
        self.consistent_hash.add_machine(server, len(self.servers) - 1)