ruby redis的集群管理器
#==========================================================================================
# => 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
ruby redis的集群管理器的更多相关文章
- Spark的集群管理器
上篇文章谈到Driver节点和Executor节点,但是如果想要运行Driver节点和Executor节点,就不能不说spark的集群管理器.spark的集群管理器大致有三种,一种是自带的standa ...
- Spark集群管理器介绍
Spark可以运行在各种集群管理器上,并通过集群管理器访问集群中的其他机器.Spark主要有三种集群管理器,如果只是想让spark运行起来,可以采用spark自带的独立集群管理器,采用独立部署的模式: ...
- redis cluster集群管理工具redis-trib.rb命令小结-运维笔记
redis-trib.rb是redis官方推出的管理redis集群的工具,集成在redis的源码src目录下,是基于redis提供的集群命令封装成简单.便捷.实用的操作工具.redis-trib.rb ...
- Fleet(集群管理器)
工作原理 fleet 是通过systemd来控制你的集群的,控制的任务被称之为unit(单元),控制的命令是fleetctl unit运行方式 unit的运行方式有两种: standard globa ...
- Kubernetes TensorFlow 默认 特定 集群管理器
Our goal is to foster an ecosystem of components and tools that relieve the burden of running applic ...
- Kubernetes TensorFlow 默认 特定 集群管理器 虚拟化技术
Our goal is to foster an ecosystem of components and tools that relieve the burden of running applic ...
- Redis分布式集群几点说道
原文地址:http://www.cnblogs.com/verrion/p/redis_structure_type_selection.html Redis分布式集群几点说道 Redis数据量日益 ...
- 【Redis】Redis分布式集群几点说道
Redis数据量日益增大,使用的公司越来越多,不仅用于做缓存,同时趋向于存储这一块,这样必促使集群的发展,各个公司也在收集适合自己的集群方案,目前行业用的比较多的是下面几种集群架构,大部分都是采用分片 ...
- redis搭建集群并用TreeSoft管理
前言:redis作为一款高效的NOSQL数据库已经深入贯彻和落实到我们的日常开发代码中,作为缓存.时间控制.数据仓库.队列等使用方法层出不穷,简直是开写代码.居家旅行之必备良药.曾经,我们的项目都是单 ...
随机推荐
- POJ 1664 放苹果【DFS】
题意:给出n个苹果,m个盘子,问有多少种不同的苹果放置方法 可以把它抽象为把一个数n,拆分成a1,a2,a3,---,am,使得它们的和为n, 话说这一题是学习的ppt里面的,它的思路就是搜索 搜索条 ...
- [HNOI2008]越狱 快速幂 逆推
考虑越狱的情况有些复杂,不如考虑总情况减去不越狱的情况. 显然,总情况为 $m^n$ 种,不越狱的情况为 $m*(m-1)*(m-1)*(m-1)....$ 即为 $m*(m-1)^(n-1)$. 做 ...
- Windows下安装Linux虚拟机的用途和好处
Windows一般是办公界面,主要做代码编辑查看,资料查找,还有发邮件,也可以用Windows下的其他的有用软件,Linux主要作为编译工具,基本上开发都是在Linux平台下编译,例如编译驱动就需要在 ...
- readb(), readw(), readl(),writeb(), writew(), writel() 宏函数
参见: http://blog.csdn.net/hustyangju/article/details/20448339
- java实现上传图片
1.将图片上传到tomcat下 2.将相对路径存放到数据库中 @RequestMapping(params="upLoadPicture") @ResponseBody publi ...
- JVM分代通俗解释
JVM分代通俗解释 学习了:https://www.cnblogs.com/zgghb/p/6428395.html
- JAVA:从public static void main(String args[])開始
我们都知道当你要执行一个JAVA文件的时候必需要有一个main函数. 这是为什么呢? 跟C语言的道理一样,当你执行一个文件的时候.你必需要有一个入口函数或者入口地址,在C里面是main函数.相同的在J ...
- SurfaceView左右滑动切换黑屏问题解决方式
在项目中使用的是高德地图,放置MapView的Fragment和其它Fragment放置一个ViewPager中切换:当从MapView的Fragment切换到其它Fragment或者从其它Fragm ...
- Java排序之直接选择排序
public class SelectSort { public static void selectSort(int [] a){ int min; int temp; if(a==null || ...
- Pascal Script
MsgBox http://www.jrsoftware.org/ishelp/index.php?topic=isxfunc_msgbox ExpandConstant http://www.jrs ...