#===================================================== ========================================================== ============#==> Redis Cluster Manager #================================== ========================================================== ========================== class cachegroupmanagerattr_reader: redis_groups # redis cluster attr_reader: redis_group_keys # redis cluster hash group Def initialize (cache_addrs) @ redis_groups ={}@ redis_group_keys = [] for ADDR in charge (ADDR) end @ redis_group_keys = @ redis_groups.keys.sortend #======================== ========================================================== ==========================#> create a node #======================== ========================================================== ====================================== def create_cache_node (ADDR) IP = ADDR. split (":") [0] Port = ADDR. split (":") [1]. to_iredis = redis. new (: host => ip,: Port => port); # create a virtual node for I in 0 .. 2 hash = hashcode. hash (ADDR + "# {I }") @ redis_groups [hash] = redisendend #============================ ========================================================== ==============================#> find the nearest cache point #==================== ========================================================== ============================================================== def find_near_cache (hash) start = find (@ redis_group_keys, hash, 0, @ redis_group_keys.size-1) For I in [email protected] _ group_keys.sizeif (@ redis_group_keys [I]> = hash) return @ redis_groups [@ redis_group_keys [I] endend # If a round is found .. cannot be found .. return @ redis_groups [@ redis_group_keys.first] End #========================== ========================================================== ================================#> start searching for a point in compromise #================== ========================================================== ============================================================== def find (keys, v, start, tail) Mid = keys [start + tail/2] If (tail-Start = 1) return startendif (tail-Start = 0) return startendif (mid> V) Find (keys, V, start, tail/2) elsif mid <vfind (keys, V, start + tail/2, tail) else mid = vreturn start + tail/2 endend #============================ ========================================================== ========================================> find the cache point through the key #========== ========================================================== ========================================================== = def get_cache_from_key (key) hash = hashcode. hash (key) cache = find_near_cache (hash) return cache; end #=================================================== ========================================================== ==============#> auto-increment key #============================== ========================================================== ================================== def incr (key) cache = get_cache_from_key (key) return cache. incr (key) end #=================================================== ========================================================== ==============#==> set a value #================================ ========================================================== ====================================== def set (key, value) cache = get_cache_from_key (key) return cache. set (key, value) end #=================================================== ========================================================== ================#> get a vaue #=============================== ========================================================== ====================================== def get (key) cache = get_cache_from_key (key) return cache. get (key) endend
Recently, we have probably studied consistent hash.
The simple description is
Key hash
Cache also performs hash
When you operate on the key, find the cache based on the key hash.
Start with the hash of the key. Find the hash of the next cache, which is the place where the data is to be stored.
Reference
Http://www.nowamagic.net/librarys/veda/detail/1336