Ruby redis Cluster Manager

Source: Internet
Author: User
Tags redis cluster
#===================================================== ========================================================== ============#==> 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

 






Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.