MemcacheRing: python-memcache的一致性哈希實現

I have added MemcacheRing to hash_ring 1.1. This class encapsulates python-memcache's Client class so keys are distributed using consistent hashing.

You can read more about consistent hashing in two other posts:

Usage of MemcacheRing

from hash_ring import MemcacheRing
mc=MemcacheRing(['192.168.0.23:11212','192.168.0.66:11212'])
mc.set('hello','world')
printmc.get('hello')


Implementation:

import memcache
import types
from hash_ring import HashRing
class MemcacheRing(memcache.Client):
    """Extends python-memcache so it uses consistent hashing to
    distribute the keys.
    """
    def __init__(self, servers, *k, **kw):
        self.hash_ring = HashRing(servers)
        memcache.Client.__init__(self, servers, *k, **kw)
        self.server_mapping = {}
        for server_uri, server_obj in zip(servers, self.servers):
            self.server_mapping[server_uri] = server_obj
    def _get_server(self, key):
        if type(key) == types.TupleType:
            return memcache.Client._get_server(key)
        for i in range(self._SERVER_RETRIES):
            iterator = self.hash_ring.iterate_nodes(key)
            for server_uri in iterator:
                server_obj = self.server_mapping[server_uri]
                if server_obj.connect():
                    return server_obj, key
        return None, None

Other than this, I am also using the idea of consistent hashing to implement a distributed hash map. This may also become open-source some time in the future.

轉自:http://amix.dk/blog/post/19370

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章