#==========================================================================================
# => redis集群管理器
#==========================================================================================
class CacheGroupManager

attr_reader :redis_groups # redis 集群
attr_reader :redis_group_keys # redis 集群hash组

def initialize(cache_addrs)

@redis_groups = {}
@redis_group_keys = []

for addr in cache_addrs
create_cache_node(addr)
end

@redis_group_keys = @redis_groups.keys.sort

end


#==========================================================================================
# => 创建节点
#==========================================================================================
def create_cache_node(addr)
ip = addr.split(":")[0]
port = addr.split(":")[1].to_i
redis = Redis.new( :host=> ip, :port => port);

# 创建虚拟节点
for i in 0..2
hash = HashCode.hash(addr + "#{i}")
@redis_groups[hash] = redis
end

end

#==========================================================================================
# => 找到近期的cache点
#==========================================================================================
def find_near_cache(hash)
start = find(@redis_group_keys, hash, 0, @redis_group_keys.size - 1)
for i in start...@redis_group_keys.size
if(@redis_group_keys[i] >= hash)
return @redis_groups[@redis_group_keys[i]]
end
end
# 假设找了一轮..都找不到..
return @redis_groups[@redis_group_keys.first]
end


#==========================================================================================
# => 折中找到開始搜寻点
#==========================================================================================
def find(keys, v, start, tail)
mid = keys[start + tail / 2]
if(tail - start == 1)
return start
end
if(tail - start == 0)
return start
end
if(mid > v)
find(keys, v, start, tail / 2)
elsif mid < v
find(keys, v, start + tail / 2, tail)
else mid == v
return start + tail / 2
end
end


#==========================================================================================
# => 通过key找到cache点
#==========================================================================================
def get_cache_from_key(key)

hash = HashCode.hash(key)

cache = find_near_cache(hash)

return cache;
end


#==========================================================================================
# => 自增一个key
#==========================================================================================
def incr(key)
cache = get_cache_from_key(key)
return cache.incr(key)
end

#==========================================================================================
# => 设置一个value
#==========================================================================================
def set(key, value)
cache = get_cache_from_key(key)
return cache.set(key, value)
end

#==========================================================================================
# => 获取一个vaue
#==========================================================================================
def get(key)
cache = get_cache_from_key(key)
return cache.get(key)
end




end



近期大概的研究了一下一致性hash.

简单点描写叙述就是

key做一次hash

cache也做hash


操作key的时候,依据key的hash找到cache.

以key的hash为起点.找到下一个cache的hash,那个cache就是这个数据要储存的地方


详细參考

http://www.nowamagic.net/librarys/veda/detail/1336