redis_cluster 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +36 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +49 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/redis_cluster/client.rb +72 -0
- data/lib/redis_cluster/configuration.rb +9 -0
- data/lib/redis_cluster/crc16.rb +90 -0
- data/lib/redis_cluster/node.rb +47 -0
- data/lib/redis_cluster/pool.rb +56 -0
- data/lib/redis_cluster/slot.rb +16 -0
- data/lib/redis_cluster/version.rb +3 -0
- data/lib/redis_cluster.rb +24 -0
- data/redis_cluster.gemspec +34 -0
- metadata +119 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3a14207e035c4ed8c3ca40d247a0fd966f01bb01
|
4
|
+
data.tar.gz: 1de7c4b10fc4b48ca75e32857679b997c84b02c0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1ac77627825033b1f00f0154854ac68d8ebbd2b4bbbdb692eee2012ba2173f09f92233571e2e3069dad13720bf37184691aa27108c6de3404b95b74c21a345ab
|
7
|
+
data.tar.gz: 6b2a77c7391682071c667978eca9222403fbbe6ac525bf94ea81aeaa16085a37d92bbe9f3c1fb2fad4ce762d03046551efa2250a8aa66e8922b71d64b694d5c7
|
data/.gitignore
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
/.config
|
4
|
+
/coverage/
|
5
|
+
/InstalledFiles
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/spec/examples.txt
|
9
|
+
/test/tmp/
|
10
|
+
/test/version_tmp/
|
11
|
+
/tmp/
|
12
|
+
|
13
|
+
## Specific to RubyMotion:
|
14
|
+
.dat*
|
15
|
+
.repl_history
|
16
|
+
build/
|
17
|
+
|
18
|
+
## Documentation cache and generated files:
|
19
|
+
/.yardoc/
|
20
|
+
/_yardoc/
|
21
|
+
/doc/
|
22
|
+
/rdoc/
|
23
|
+
|
24
|
+
## Environment normalization:
|
25
|
+
/.bundle/
|
26
|
+
/vendor/bundle
|
27
|
+
/lib/bundler/man/
|
28
|
+
|
29
|
+
# for a library or gem, you might want to ignore these files since the code is
|
30
|
+
# intended to run in multiple environments; otherwise, check them in:
|
31
|
+
# Gemfile.lock
|
32
|
+
# .ruby-version
|
33
|
+
# .ruby-gemset
|
34
|
+
|
35
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
36
|
+
.rvmrc
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, and in the interest of
|
4
|
+
fostering an open and welcoming community, we pledge to respect all people who
|
5
|
+
contribute through reporting issues, posting feature requests, updating
|
6
|
+
documentation, submitting pull requests or patches, and other activities.
|
7
|
+
|
8
|
+
We are committed to making participation in this project a harassment-free
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
+
body size, race, ethnicity, age, religion, or nationality.
|
12
|
+
|
13
|
+
Examples of unacceptable behavior by participants include:
|
14
|
+
|
15
|
+
* The use of sexualized language or imagery
|
16
|
+
* Personal attacks
|
17
|
+
* Trolling or insulting/derogatory comments
|
18
|
+
* Public or private harassment
|
19
|
+
* Publishing other's private information, such as physical or electronic
|
20
|
+
addresses, without explicit permission
|
21
|
+
* Other unethical or unprofessional conduct
|
22
|
+
|
23
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
24
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
25
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
26
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
27
|
+
threatening, offensive, or harmful.
|
28
|
+
|
29
|
+
By adopting this Code of Conduct, project maintainers commit themselves to
|
30
|
+
fairly and consistently applying these principles to every aspect of managing
|
31
|
+
this project. Project maintainers who do not follow or enforce the Code of
|
32
|
+
Conduct may be permanently removed from the project team.
|
33
|
+
|
34
|
+
This code of conduct applies both within project spaces and in public spaces
|
35
|
+
when an individual is representing the project or its community.
|
36
|
+
|
37
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
38
|
+
reported by contacting a project maintainer at wangzhichao@caishuo.com. All
|
39
|
+
complaints will be reviewed and investigated and will result in a response that
|
40
|
+
is deemed necessary and appropriate to the circumstances. Maintainers are
|
41
|
+
obligated to maintain confidentiality with regard to the reporter of an
|
42
|
+
incident.
|
43
|
+
|
44
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
45
|
+
version 1.3.0, available at
|
46
|
+
[http://contributor-covenant.org/version/1/3/0/][version]
|
47
|
+
|
48
|
+
[homepage]: http://contributor-covenant.org
|
49
|
+
[version]: http://contributor-covenant.org/version/1/3/0/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 sam
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# RedisCluster
|
2
|
+
|
3
|
+
RedisCluster is rewrited from [https://github.com/antirez/redis-rb-cluster](https://github.com/antirez/redis-rb-cluster)
|
4
|
+
|
5
|
+
Now is developing, and not use in any production environments. Welcome to test and contribute codes!
|
6
|
+
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'redis_cluster'
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install redis_cluster
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
Doing!
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
hosts = [{host: 127.0.0.1, port: 7000}, {host: 127.0.0.1, port: 7001}]
|
30
|
+
rs = RedisCluster.new hosts
|
31
|
+
rs.set "test", 1
|
32
|
+
rs.get "test"
|
33
|
+
```
|
34
|
+
|
35
|
+
## Development
|
36
|
+
|
37
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
38
|
+
|
39
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
40
|
+
|
41
|
+
## Contributing
|
42
|
+
|
43
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/zhchsf/redis_cluster. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
44
|
+
|
45
|
+
|
46
|
+
## License
|
47
|
+
|
48
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
49
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "redis_cluster"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
module RedisCluster
|
2
|
+
|
3
|
+
class Client
|
4
|
+
|
5
|
+
def initialize(startup_hosts, global_configs)
|
6
|
+
@startup_hosts = startup_hosts
|
7
|
+
@pool = Pool.new
|
8
|
+
@locking = false
|
9
|
+
reload_pool_nodes
|
10
|
+
end
|
11
|
+
|
12
|
+
def execute(method, args)
|
13
|
+
ttl = Configuration::REQUEST_TTL
|
14
|
+
asking = false
|
15
|
+
try_random_node = false
|
16
|
+
|
17
|
+
while ttl > 0
|
18
|
+
ttl -= 1
|
19
|
+
begin
|
20
|
+
return @pool.execute(method, args, {asking: asking, random_node: try_random_node})
|
21
|
+
rescue Errno::ECONNREFUSED, Redis::TimeoutError, Redis::CannotConnectError, Errno::EACCES
|
22
|
+
try_random_node = true
|
23
|
+
sleep 0.1 if ttl < Configuration::REQUEST_TTL/2
|
24
|
+
rescue => e
|
25
|
+
err_code = e.to_s.split.first
|
26
|
+
raise e if !%w(MOVED ASK).include?(err_code)
|
27
|
+
|
28
|
+
if err_code == "ASK"
|
29
|
+
asking = true
|
30
|
+
else
|
31
|
+
reload_pool_nodes
|
32
|
+
sleep 0.1 if ttl < Configuration::REQUEST_TTL/2
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def method_missing(method, *args, &block)
|
39
|
+
execute(method, args)
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
def reload_pool_nodes
|
45
|
+
return if @locking
|
46
|
+
@locking = true
|
47
|
+
@startup_hosts.each do |options|
|
48
|
+
begin
|
49
|
+
redis = Node.redis(options)
|
50
|
+
slots_mapping = redis.cluster("slots").group_by{|x| x[2]}
|
51
|
+
@pool.delete_except!(slots_mapping.keys)
|
52
|
+
slots_mapping.each do |host, infos|
|
53
|
+
slots_ranges = infos.map {|x| x[0]..x[1] }
|
54
|
+
@pool.add_node!({host: host[0], port: host[1]}, slots_ranges)
|
55
|
+
end
|
56
|
+
rescue
|
57
|
+
next
|
58
|
+
end
|
59
|
+
break
|
60
|
+
end
|
61
|
+
@locking = false
|
62
|
+
fresh_startup_nodes
|
63
|
+
end
|
64
|
+
|
65
|
+
def fresh_startup_nodes
|
66
|
+
@pool.nodes.each {|node| @startup_hosts.push(node.host_hash) }
|
67
|
+
@startup_hosts.uniq!
|
68
|
+
end
|
69
|
+
|
70
|
+
end # end client
|
71
|
+
|
72
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
# From: antirez's redis-rb-cluster
|
2
|
+
#
|
3
|
+
# Copyright (C) 2013 Salvatore Sanfilippo <antirez@gmail.com>
|
4
|
+
#
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
# a copy of this software and associated documentation files (the
|
7
|
+
# "Software"), to deal in the Software without restriction, including
|
8
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
# the following conditions:
|
12
|
+
#
|
13
|
+
# The above copyright notice and this permission notice shall be
|
14
|
+
# included in all copies or substantial portions of the Software.
|
15
|
+
#
|
16
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
23
|
+
#
|
24
|
+
# -----------------------------------------------------------------------------
|
25
|
+
#
|
26
|
+
# This is the CRC16 algorithm used by Redis Cluster to hash keys.
|
27
|
+
# Implementation according to CCITT standards.
|
28
|
+
#
|
29
|
+
# This is actually the XMODEM CRC 16 algorithm, using the
|
30
|
+
# following parameters:
|
31
|
+
#
|
32
|
+
# Name : "XMODEM", also known as "ZMODEM", "CRC-16/ACORN"
|
33
|
+
# Width : 16 bit
|
34
|
+
# Poly : 1021 (That is actually x^16 + x^12 + x^5 + 1)
|
35
|
+
# Initialization : 0000
|
36
|
+
# Reflect Input byte : False
|
37
|
+
# Reflect Output CRC : False
|
38
|
+
# Xor constant to output CRC : 0000
|
39
|
+
# Output for "123456789" : 31C3
|
40
|
+
|
41
|
+
module RedisCluster
|
42
|
+
|
43
|
+
module CRC16
|
44
|
+
|
45
|
+
def self.crc16(bytes)
|
46
|
+
crc = 0
|
47
|
+
bytes.each_byte{|b|
|
48
|
+
crc = ((crc<<8) & 0xffff) ^ XMODEMCRC16Lookup[((crc>>8)^b) & 0xff]
|
49
|
+
}
|
50
|
+
crc
|
51
|
+
end
|
52
|
+
|
53
|
+
XMODEMCRC16Lookup = [
|
54
|
+
0x0000,0x1021,0x2042,0x3063,0x4084,0x50a5,0x60c6,0x70e7,
|
55
|
+
0x8108,0x9129,0xa14a,0xb16b,0xc18c,0xd1ad,0xe1ce,0xf1ef,
|
56
|
+
0x1231,0x0210,0x3273,0x2252,0x52b5,0x4294,0x72f7,0x62d6,
|
57
|
+
0x9339,0x8318,0xb37b,0xa35a,0xd3bd,0xc39c,0xf3ff,0xe3de,
|
58
|
+
0x2462,0x3443,0x0420,0x1401,0x64e6,0x74c7,0x44a4,0x5485,
|
59
|
+
0xa56a,0xb54b,0x8528,0x9509,0xe5ee,0xf5cf,0xc5ac,0xd58d,
|
60
|
+
0x3653,0x2672,0x1611,0x0630,0x76d7,0x66f6,0x5695,0x46b4,
|
61
|
+
0xb75b,0xa77a,0x9719,0x8738,0xf7df,0xe7fe,0xd79d,0xc7bc,
|
62
|
+
0x48c4,0x58e5,0x6886,0x78a7,0x0840,0x1861,0x2802,0x3823,
|
63
|
+
0xc9cc,0xd9ed,0xe98e,0xf9af,0x8948,0x9969,0xa90a,0xb92b,
|
64
|
+
0x5af5,0x4ad4,0x7ab7,0x6a96,0x1a71,0x0a50,0x3a33,0x2a12,
|
65
|
+
0xdbfd,0xcbdc,0xfbbf,0xeb9e,0x9b79,0x8b58,0xbb3b,0xab1a,
|
66
|
+
0x6ca6,0x7c87,0x4ce4,0x5cc5,0x2c22,0x3c03,0x0c60,0x1c41,
|
67
|
+
0xedae,0xfd8f,0xcdec,0xddcd,0xad2a,0xbd0b,0x8d68,0x9d49,
|
68
|
+
0x7e97,0x6eb6,0x5ed5,0x4ef4,0x3e13,0x2e32,0x1e51,0x0e70,
|
69
|
+
0xff9f,0xefbe,0xdfdd,0xcffc,0xbf1b,0xaf3a,0x9f59,0x8f78,
|
70
|
+
0x9188,0x81a9,0xb1ca,0xa1eb,0xd10c,0xc12d,0xf14e,0xe16f,
|
71
|
+
0x1080,0x00a1,0x30c2,0x20e3,0x5004,0x4025,0x7046,0x6067,
|
72
|
+
0x83b9,0x9398,0xa3fb,0xb3da,0xc33d,0xd31c,0xe37f,0xf35e,
|
73
|
+
0x02b1,0x1290,0x22f3,0x32d2,0x4235,0x5214,0x6277,0x7256,
|
74
|
+
0xb5ea,0xa5cb,0x95a8,0x8589,0xf56e,0xe54f,0xd52c,0xc50d,
|
75
|
+
0x34e2,0x24c3,0x14a0,0x0481,0x7466,0x6447,0x5424,0x4405,
|
76
|
+
0xa7db,0xb7fa,0x8799,0x97b8,0xe75f,0xf77e,0xc71d,0xd73c,
|
77
|
+
0x26d3,0x36f2,0x0691,0x16b0,0x6657,0x7676,0x4615,0x5634,
|
78
|
+
0xd94c,0xc96d,0xf90e,0xe92f,0x99c8,0x89e9,0xb98a,0xa9ab,
|
79
|
+
0x5844,0x4865,0x7806,0x6827,0x18c0,0x08e1,0x3882,0x28a3,
|
80
|
+
0xcb7d,0xdb5c,0xeb3f,0xfb1e,0x8bf9,0x9bd8,0xabbb,0xbb9a,
|
81
|
+
0x4a75,0x5a54,0x6a37,0x7a16,0x0af1,0x1ad0,0x2ab3,0x3a92,
|
82
|
+
0xfd2e,0xed0f,0xdd6c,0xcd4d,0xbdaa,0xad8b,0x9de8,0x8dc9,
|
83
|
+
0x7c26,0x6c07,0x5c64,0x4c45,0x3ca2,0x2c83,0x1ce0,0x0cc1,
|
84
|
+
0xef1f,0xff3e,0xcf5d,0xdf7c,0xaf9b,0xbfba,0x8fd9,0x9ff8,
|
85
|
+
0x6e17,0x7e36,0x4e55,0x5e74,0x2e93,0x3eb2,0x0ed1,0x1ef0
|
86
|
+
].freeze
|
87
|
+
|
88
|
+
end # end CRC16
|
89
|
+
|
90
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module RedisCluster
|
2
|
+
|
3
|
+
class Node
|
4
|
+
# slots is a range array: [1..100, 300..500]
|
5
|
+
attr_accessor :slots
|
6
|
+
|
7
|
+
#
|
8
|
+
# basic requires:
|
9
|
+
# {host: xxx.xxx.xx.xx, port: xxx}
|
10
|
+
# redis cluster don't support select db, use default 0
|
11
|
+
#
|
12
|
+
def initialize(opts)
|
13
|
+
@options = opts
|
14
|
+
@slots = []
|
15
|
+
end
|
16
|
+
|
17
|
+
def name
|
18
|
+
"#{@options[:host]}:#{@options[:port]}"
|
19
|
+
end
|
20
|
+
|
21
|
+
def host_hash
|
22
|
+
{host: @options[:host], port: @options[:port]}
|
23
|
+
end
|
24
|
+
|
25
|
+
def has_slot?(slot)
|
26
|
+
slots.any? {|range| range.include? slot }
|
27
|
+
end
|
28
|
+
|
29
|
+
def asking
|
30
|
+
execute(:asking)
|
31
|
+
end
|
32
|
+
|
33
|
+
def execute(method, args)
|
34
|
+
connection.send(method, *args)
|
35
|
+
end
|
36
|
+
|
37
|
+
def connection
|
38
|
+
@connection ||= self.class.redis(@options)
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.redis(options)
|
42
|
+
Redis.new(options)
|
43
|
+
end
|
44
|
+
|
45
|
+
end # end Node
|
46
|
+
|
47
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
module RedisCluster
|
2
|
+
|
3
|
+
class Pool
|
4
|
+
attr_reader :nodes
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
@nodes = []
|
8
|
+
end
|
9
|
+
|
10
|
+
def add_node!(options, slots)
|
11
|
+
new_node = Node.new(options)
|
12
|
+
node = @nodes.find {|n| n.name == new_node.name } || new_node
|
13
|
+
node.slots = slots
|
14
|
+
@nodes.push(node).uniq!
|
15
|
+
end
|
16
|
+
|
17
|
+
def delete_except!(new_hosts)
|
18
|
+
names = new_hosts.map {|host, port| "#{host}:#{port}" }
|
19
|
+
@nodes.delete_if {|n| !names.include?(n.name) }
|
20
|
+
end
|
21
|
+
|
22
|
+
# other_options:
|
23
|
+
# asking
|
24
|
+
# random_node
|
25
|
+
def execute(method, args, other_options)
|
26
|
+
key = key_by_command(method, args)
|
27
|
+
raise "not usable" if key.nil?
|
28
|
+
|
29
|
+
node = other_options[:random_node] ? random_node : node_by(key)
|
30
|
+
node.asking if other_options[:asking]
|
31
|
+
node.execute(method, args)
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def node_by(key)
|
37
|
+
slot = Slot.slot_by(key)
|
38
|
+
@nodes.find {|node| node.has_slot?(slot) }
|
39
|
+
end
|
40
|
+
|
41
|
+
def random_node
|
42
|
+
@nodes.sample
|
43
|
+
end
|
44
|
+
|
45
|
+
def key_by_command(method, args)
|
46
|
+
case method.to_s.downcase
|
47
|
+
when 'info', 'multi', 'exec', 'slaveof', 'config', 'shutdown'
|
48
|
+
nil
|
49
|
+
else
|
50
|
+
return args.first
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
end # end pool
|
55
|
+
|
56
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module RedisCluster
|
2
|
+
|
3
|
+
class Slot
|
4
|
+
KEY_PATTERN = /\{([^\}]*)\}/
|
5
|
+
|
6
|
+
# key "{xxx}ooo" will calculate "xxx" for slot
|
7
|
+
# if key is "{}dddd", calculate "{}dddd" for slot
|
8
|
+
def self.slot_by(key)
|
9
|
+
KEY_PATTERN =~ key
|
10
|
+
key = $1 if $1 && !$1.empty?
|
11
|
+
CRC16.crc16(key) % Configuration::HASH_SLOTS
|
12
|
+
end
|
13
|
+
|
14
|
+
end # end Slot
|
15
|
+
|
16
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require "redis_cluster/version"
|
2
|
+
|
3
|
+
module RedisCluster
|
4
|
+
|
5
|
+
class << self
|
6
|
+
|
7
|
+
# startup_hosts examples:
|
8
|
+
# [{host: 'xxx', port: 'xxx'}, {host: 'xxx', port: 'xxx'}, ...]
|
9
|
+
# global_configs:
|
10
|
+
# now not used, options for later versions
|
11
|
+
def new(startup_hosts, *global_configs)
|
12
|
+
@client = Client.new(startup_hosts, global_configs)
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
require "redis_cluster/client"
|
20
|
+
require "redis_cluster/node"
|
21
|
+
require "redis_cluster/pool"
|
22
|
+
require "redis_cluster/slot"
|
23
|
+
require "redis_cluster/crc16"
|
24
|
+
require "redis_cluster/configuration"
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'redis_cluster/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "redis_cluster"
|
8
|
+
spec.version = RedisCluster::VERSION
|
9
|
+
spec.authors = ["wangzc"]
|
10
|
+
spec.email = ["zhchsf@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{redis cluster client}
|
13
|
+
spec.description = %q{redis cluster client}
|
14
|
+
spec.homepage = "http://www.iruby.com.cn"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
18
|
+
# delete this section to allow pushing this gem to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata['allowed_push_host'] = "https://rubygems.org"
|
21
|
+
else
|
22
|
+
raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
23
|
+
end
|
24
|
+
|
25
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
|
30
|
+
spec.add_dependency "redis", "~> 3.2"
|
31
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
32
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
33
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
34
|
+
end
|
metadata
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: redis_cluster
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.6
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- wangzc
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-04-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: redis
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.2'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.11'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.11'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
description: redis cluster client
|
70
|
+
email:
|
71
|
+
- zhchsf@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- CODE_OF_CONDUCT.md
|
78
|
+
- Gemfile
|
79
|
+
- LICENSE.txt
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- bin/console
|
83
|
+
- bin/setup
|
84
|
+
- lib/redis_cluster.rb
|
85
|
+
- lib/redis_cluster/client.rb
|
86
|
+
- lib/redis_cluster/configuration.rb
|
87
|
+
- lib/redis_cluster/crc16.rb
|
88
|
+
- lib/redis_cluster/node.rb
|
89
|
+
- lib/redis_cluster/pool.rb
|
90
|
+
- lib/redis_cluster/slot.rb
|
91
|
+
- lib/redis_cluster/version.rb
|
92
|
+
- redis_cluster.gemspec
|
93
|
+
homepage: http://www.iruby.com.cn
|
94
|
+
licenses:
|
95
|
+
- MIT
|
96
|
+
metadata:
|
97
|
+
allowed_push_host: https://rubygems.org
|
98
|
+
post_install_message:
|
99
|
+
rdoc_options: []
|
100
|
+
require_paths:
|
101
|
+
- lib
|
102
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
112
|
+
requirements: []
|
113
|
+
rubyforge_project:
|
114
|
+
rubygems_version: 2.4.8
|
115
|
+
signing_key:
|
116
|
+
specification_version: 4
|
117
|
+
summary: redis cluster client
|
118
|
+
test_files: []
|
119
|
+
has_rdoc:
|