rails_kvs_driver-redis_driver 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f5068e39bff48ce20cf53e9c5ccc0f97d8953115
4
+ data.tar.gz: f51c7417cf8ae8cf4b230230f6269a99bc7decff
5
+ SHA512:
6
+ metadata.gz: 4e546a1b2d7fc2ac5af8ed89c7628cb003d9e0194b091b9476ad6e35f6a2b108d31de37ee524eeecb31198eb58dea2354032ffd21b57317b5849ac124617b218
7
+ data.tar.gz: 84a26af274a07d9374bdd802916ba71d2e89625333cda6beb42ac4474e8d70f0e7d9874328b16e67c6a68489d74e02ade5fa79ac61da2f54a814a68b17b07dcd
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ vendor
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rails_kvs_driver-redis_driver.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Atsushi Nakamura
2
+
3
+ MIT License
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.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # RailsKvsDriver::RedisDriver
2
+
3
+ Redis Driver for Rails_Kvs_Driver.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'rails_kvs_driver-redis_driver'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install rails_kvs_driver-redis_driver
18
+
19
+ ## Usage
20
+
21
+ comming soon.
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,143 @@
1
+ require "rails_kvs_driver/redis_driver/version"
2
+ require "rails_kvs_driver"
3
+ require "redis"
4
+
5
+ module RailsKvsDriver::RedisDriver
6
+ class Driver < RailsKvsDriver::Base
7
+ # connect with driver config.
8
+ # @return [Boolean] result
9
+ def connect
10
+ begin
11
+ @kvs_inst = Redis.new(host: @driver_config[:host], port: @driver_config[:port])
12
+ return true
13
+ rescue
14
+ return false
15
+ end
16
+ end
17
+
18
+ # get value from kvs.
19
+ # @param key [String] key.
20
+ # @return [String] value.
21
+ def [](key)
22
+ @kvs_inst.get(safe_key(key))
23
+ end
24
+
25
+ # set value to kvs.
26
+ # @param key [String] key.
27
+ # @param value [String] value.
28
+ # @return [Boolean] result
29
+ def []=(key, value)
30
+ @kvs_inst.set(safe_key(key), value)
31
+ end
32
+
33
+ # get all keys from kvs.
34
+ # @return [Array<String>] array of key names.
35
+ def all_keys
36
+ result = Array.new
37
+ pattern = safe_key('*')
38
+ header_len = pattern.length - 1
39
+
40
+ @kvs_inst.keys(pattern).each do |key|
41
+ result.push key[header_len .. -1]
42
+ end
43
+
44
+ return result
45
+ end
46
+
47
+ # delete key from kvs.
48
+ # @return [Boolean] result.
49
+ def delete(key)
50
+ (@kvs_inst.del(safe_key(key)) == 1)
51
+ end
52
+
53
+ # delete all keys from kvs.
54
+ # @return [Boolean] result.
55
+ def delete_all
56
+ keys = @kvs_inst.keys(safe_key('*'))
57
+ del_num = @kvs_inst.del(keys) if keys.length > 0
58
+
59
+ (del_num == keys.length)
60
+ end
61
+
62
+ # add sorted set to kvs.
63
+ # when the key doesn't exist, it's made newly.
64
+ # @note same as sorted set of redis. refer to redis.
65
+ #
66
+ # @param key [String] key of sorted set.
67
+ # @param member [String] member of sorted set.
68
+ # @param score [Float] score of sorted set.
69
+ # @return [Boolean] result.
70
+ def add_sorted_set(key, member, score)
71
+ @kvs_inst.zadd(safe_key(key), score, member)
72
+ end
73
+
74
+ # remove sorted set from kvs.
75
+ # This function doesn't delete a key.
76
+ # @note same as sorted set of redis. refer to redis.
77
+ #
78
+ # @param key [String] key of sorted set.
79
+ # @param member [String] member of sorted set.
80
+ # @return [Boolean] result.
81
+ # @abstract remove sorted set from kvs.
82
+ def remove_sorted_set(key, member)
83
+ @kvs_inst.zrem(safe_key(key), member)
84
+ end
85
+
86
+ # increment score of member from sorted set.
87
+ # @note same as sorted set of redis. refer to redis.
88
+ #
89
+ # @param key [String] key of sorted set.
90
+ # @param member [String] member of sorted set.
91
+ # @param score [Float] increment score.
92
+ # @return [Float] value after increment
93
+ # @abstract increment score of member from sorted set.
94
+ def increment_sorted_set(key, member, score)
95
+ @kvs_inst.zincrby(safe_key(key), score, member)
96
+ end
97
+
98
+ # get the score of member.
99
+ # @note same as sorted set of redis. refer to redis.
100
+ #
101
+ # @param key [String] key of sorted set.
102
+ # @param member [String] member of sorted set.
103
+ # @return [Float] score of member.
104
+ # @abstract get the score of member.
105
+ def sorted_set_score(key, member)
106
+ @kvs_inst.zscore(safe_key(key), member)
107
+ end
108
+
109
+ # get array of sorted set.
110
+ # @note same as sorted set of redis. refer to redis.
111
+ #
112
+ # @param key [String] key of sorted set.
113
+ # @param start [Integer] start index
114
+ # @param stop [Integer] stop index
115
+ # @param reverse [Boolean] order by desc
116
+ # @return [Array<String, Float>>] array of the member and score.
117
+ # @abstract get array of sorted set.
118
+ def sorted_set(key, start=0, stop=-1, reverse=false)
119
+ if reverse
120
+ @kvs_inst.zrevrange(safe_key(key), start, stop, with_scores: true)
121
+ else
122
+ @kvs_inst.zrange(safe_key(key), start, stop, with_scores: true)
123
+ end
124
+ end
125
+
126
+ # count members of sorted set
127
+ # @note same as sorted set of redis. refer to redis.
128
+ #
129
+ # @param key [String] key of sorted set.
130
+ # @return [Integer] members num
131
+ def count_sorted_set_member(key)
132
+ @kvs_inst.zcard(safe_key(key))
133
+ end
134
+
135
+ private
136
+ # get safe key name with @driver_config[:namespace]
137
+ # @param key [String] key
138
+ # @return [String] safe key name
139
+ def safe_key(key)
140
+ "#{@driver_config[:namespace]}::#{key}"
141
+ end
142
+ end
143
+ end
@@ -0,0 +1,5 @@
1
+ module RailsKvsDriver
2
+ module RedisDriver
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rails_kvs_driver/redis_driver/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rails_kvs_driver-redis_driver"
8
+ spec.version = RailsKvsDriver::RedisDriver::VERSION
9
+ spec.authors = ["alfa-jpn"]
10
+ spec.email = ["a.nkmr.ja@gmail.com"]
11
+ spec.description = "redis driver for rails kvs driver."
12
+ spec.summary = "redis driver for rails kvs driver."
13
+ spec.homepage = "https://github.com/alfa-jpn/rails_kvs_driver-redis_driver"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "yard"
25
+
26
+ spec.add_dependency "redis"
27
+ spec.add_dependency "rails_kvs_driver", "0.0.2"
28
+ end
data/spec/.keep ADDED
File without changes
@@ -0,0 +1,132 @@
1
+ # How to use
2
+ # copying #{ProjectRoot}/spec
3
+ #
4
+ # require "common_example"
5
+ #
6
+ # it_should_behave_like 'RailsKvsDriver example', driver_class, driver_config
7
+ #
8
+
9
+ shared_examples_for 'RailsKvsDriver example' do |driver_class, driver_config|
10
+
11
+ it 'inheritance RailsKvsDriver::Base' do
12
+ expect(driver_class.ancestors.include?(RailsKvsDriver::Base)).to be_true
13
+ end
14
+
15
+ context 'connect kvs' do
16
+ before(:each) do
17
+ driver_class::session(driver_config) do |kvs|
18
+ kvs.delete_all
19
+ end
20
+ end
21
+
22
+ after(:each) do
23
+ driver_class::session(driver_config) do |kvs|
24
+ kvs.delete_all
25
+ end
26
+ end
27
+
28
+ it 'call set value' do
29
+ driver_class::session(driver_config) do |kvs|
30
+ expect{ kvs['example'] = 'nico-nico' }.to change{ kvs.all_keys.length }.by(1)
31
+ end
32
+ end
33
+
34
+ it 'call get value' do
35
+ driver_class::session(driver_config) do |kvs|
36
+ expect{ kvs['example'] = 'nico-nico' }.to change{ kvs.all_keys.length }.by(1)
37
+ expect(kvs['example']).to eq('nico-nico')
38
+ end
39
+ end
40
+
41
+ it 'call all keys' do
42
+ driver_class::session(driver_config) do |kvs|
43
+ expect{ kvs['example'] = 'nico-nico' }.to change{ kvs.all_keys.length }.by(1)
44
+ expect(kvs.all_keys.length).to eq(1)
45
+ end
46
+ end
47
+
48
+ it 'call delete value' do
49
+ driver_class::session(driver_config) do |kvs|
50
+ kvs['example'] = 'nico-nico'
51
+ kvs['example2'] = 'movie'
52
+
53
+ expect{ kvs.delete('example') }.to change{ kvs.all_keys.length }.by(-1)
54
+
55
+ expect(kvs['example']).to eq(nil)
56
+ expect(kvs['example2']).to eq('movie')
57
+ end
58
+ end
59
+
60
+ it 'call delete_all' do
61
+ driver_class::session(driver_config) do |kvs|
62
+ kvs['example'] = 'nico-nico'
63
+ kvs['example2'] = 'movie'
64
+
65
+ expect{ kvs.delete_all }.to change{ kvs.all_keys.length }.by(-2)
66
+
67
+ expect(kvs['example']).to eq(nil)
68
+ expect(kvs['example2']).to eq(nil)
69
+ end
70
+ end
71
+
72
+ it 'call add_sorted_set' do
73
+ driver_class::session(driver_config) do |kvs|
74
+ expect{
75
+ kvs.add_sorted_set('example', 'element', 1)
76
+ }.to change{ kvs.all_keys.length }.by(1)
77
+ end
78
+ end
79
+
80
+ it 'call remove_sorted_set' do
81
+ driver_class::session(driver_config) do |kvs|
82
+ kvs.add_sorted_set('example', 'element', 1)
83
+ kvs.add_sorted_set('example', 'element2', 1)
84
+
85
+ expect{
86
+ kvs.remove_sorted_set('example', 'element')
87
+ }.to change{ kvs.count_sorted_set_member('example') }.by(-1)
88
+ end
89
+ end
90
+
91
+ it 'call increment_sorted_set' do
92
+ driver_class::session(driver_config) do |kvs|
93
+ kvs.add_sorted_set('example', 'element', 1)
94
+
95
+ expect{
96
+ kvs.increment_sorted_set('example', 'element', 10)
97
+ }.to change{ kvs.sorted_set_score('example', 'element') }.by(10)
98
+ end
99
+ end
100
+
101
+ it 'call sorted_set' do
102
+ driver_class::session(driver_config) do |kvs|
103
+ kvs.add_sorted_set('example', 'element1', 1)
104
+ kvs.add_sorted_set('example', 'element5', 5)
105
+ kvs.add_sorted_set('example', 'element2', 2)
106
+
107
+ sorted_set = kvs.sorted_set('example')
108
+
109
+ expect(sorted_set[0][0]).to eq('element1')
110
+ expect(sorted_set[1][0]).to eq('element2')
111
+ expect(sorted_set[2][0]).to eq('element5')
112
+ end
113
+ end
114
+
115
+ it 'call sorted_set_score' do
116
+ driver_class::session(driver_config) do |kvs|
117
+ kvs.add_sorted_set('example', 'element5', 5)
118
+ expect(kvs.sorted_set('example')[0][1]).to eq(5)
119
+ end
120
+ end
121
+
122
+ it 'call count_sorted_set_member' do
123
+ driver_class::session(driver_config) do |kvs|
124
+ kvs.add_sorted_set('example', 'element1', 5)
125
+ kvs.add_sorted_set('example', 'element2', 5)
126
+ kvs.add_sorted_set('example', 'element3', 5)
127
+ expect(kvs.count_sorted_set_member('example')).to eq(3)
128
+ end
129
+ end
130
+ end
131
+ end
132
+
@@ -0,0 +1,17 @@
1
+ require 'rspec'
2
+ require 'spec_helper'
3
+ require 'common_example'
4
+
5
+ describe RailsKvsDriver::RedisDriver::Driver do
6
+
7
+ driver_config = {
8
+ :host => 'localhost', # host of KVS.
9
+ :port => 6379, # port of KVS.
10
+ :namespace => 'SPEC::RedisDriver', # namespace of avoid a conflict with key
11
+ :timeout_sec => 5, # timeout seconds.
12
+ :pool_size => 5 # connection pool size.
13
+ }
14
+
15
+ it_should_behave_like 'RailsKvsDriver example', RailsKvsDriver::RedisDriver::Driver, driver_config
16
+
17
+ end
@@ -0,0 +1,8 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'rails_kvs_driver'
4
+ require 'rails_kvs_driver/redis_driver'
5
+
6
+ RSpec.configure do |config|
7
+ config.mock_framework = :rspec
8
+ end
metadata ADDED
@@ -0,0 +1,145 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_kvs_driver-redis_driver
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - alfa-jpn
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: yard
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: redis
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rails_kvs_driver
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '='
88
+ - !ruby/object:Gem::Version
89
+ version: 0.0.2
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '='
95
+ - !ruby/object:Gem::Version
96
+ version: 0.0.2
97
+ description: redis driver for rails kvs driver.
98
+ email:
99
+ - a.nkmr.ja@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - .gitignore
105
+ - Gemfile
106
+ - LICENSE.txt
107
+ - README.md
108
+ - Rakefile
109
+ - lib/rails_kvs_driver/redis_driver.rb
110
+ - lib/rails_kvs_driver/redis_driver/version.rb
111
+ - rails_kvs_driver-redis_driver.gemspec
112
+ - spec/.keep
113
+ - spec/common_example.rb
114
+ - spec/rails_kvs_driver/redis_driver_spec.rb
115
+ - spec/spec_helper.rb
116
+ homepage: https://github.com/alfa-jpn/rails_kvs_driver-redis_driver
117
+ licenses:
118
+ - MIT
119
+ metadata: {}
120
+ post_install_message:
121
+ rdoc_options: []
122
+ require_paths:
123
+ - lib
124
+ required_ruby_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - '>='
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ requirements: []
135
+ rubyforge_project:
136
+ rubygems_version: 2.0.3
137
+ signing_key:
138
+ specification_version: 4
139
+ summary: redis driver for rails kvs driver.
140
+ test_files:
141
+ - spec/.keep
142
+ - spec/common_example.rb
143
+ - spec/rails_kvs_driver/redis_driver_spec.rb
144
+ - spec/spec_helper.rb
145
+ has_rdoc: