redis-hash 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +8 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +37 -0
- data/LICENSE.txt +21 -0
- data/README.md +116 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/redis/hash.rb +118 -0
- data/redis-hash.gemspec +30 -0
- metadata +110 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: aed98af107edcc4945a66ad7cfedc3b068ce1ee4
|
4
|
+
data.tar.gz: afaaeb9760728471e5fb052f87ba31c8d9a62049
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: dcf3f11332840f8a78887831f4861e3fe49afc259090c4a0e9982477eb46087f4b24d329958bf69cb30595c85a7fbd616031f56028723255081d2a49959b0c9f
|
7
|
+
data.tar.gz: 852e42e4efb1c20fcb46f43923e6afaf5834fa591443499a95e6107016cf4dbe02908a4eba277b2e87b438c265775f77497019121ba0ec8e8058762c3208686a
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
redis-hash (0.0.1)
|
5
|
+
redis
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
diff-lcs (1.3)
|
11
|
+
rake (10.5.0)
|
12
|
+
redis (4.1.0)
|
13
|
+
rspec (3.8.0)
|
14
|
+
rspec-core (~> 3.8.0)
|
15
|
+
rspec-expectations (~> 3.8.0)
|
16
|
+
rspec-mocks (~> 3.8.0)
|
17
|
+
rspec-core (3.8.0)
|
18
|
+
rspec-support (~> 3.8.0)
|
19
|
+
rspec-expectations (3.8.2)
|
20
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
21
|
+
rspec-support (~> 3.8.0)
|
22
|
+
rspec-mocks (3.8.0)
|
23
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
24
|
+
rspec-support (~> 3.8.0)
|
25
|
+
rspec-support (3.8.0)
|
26
|
+
|
27
|
+
PLATFORMS
|
28
|
+
ruby
|
29
|
+
|
30
|
+
DEPENDENCIES
|
31
|
+
bundler (~> 1.16)
|
32
|
+
rake (~> 10.0)
|
33
|
+
redis-hash!
|
34
|
+
rspec
|
35
|
+
|
36
|
+
BUNDLED WITH
|
37
|
+
1.16.5
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Misha Conway
|
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,116 @@
|
|
1
|
+
# RedisHash
|
2
|
+
|
3
|
+
Lightweight wrapper over redis hashes.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'redis-hash'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install redis-hash
|
20
|
+
|
21
|
+
## Getting started
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
h = RedisHash.new 'completed_customer_ids'
|
25
|
+
```
|
26
|
+
|
27
|
+
Or you can pass in your own instance of the Redis class.
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
h = RedisHash.new 'completed_customer_ids', Redis.new(:host => "10.0.1.1", :port => 6380, :db => 15)
|
31
|
+
```
|
32
|
+
|
33
|
+
A third option is to instead pass your Redis configurations.
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
h = RedisHash.new 'completed_customer_ids', :host => "10.0.1.1", :port => 6380, :db => 15
|
37
|
+
```
|
38
|
+
|
39
|
+
## Using the hash
|
40
|
+
|
41
|
+
You can add data to the hash using the set method.
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
# setting a single item
|
45
|
+
h.set {"hello" => "world"}
|
46
|
+
|
47
|
+
# setting multiple items
|
48
|
+
h.add {"hello" => "world", "goodbye" => "universe"}
|
49
|
+
```
|
50
|
+
|
51
|
+
You can read data from the hash with the get method
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
# reading a single key
|
55
|
+
h.get "hello"
|
56
|
+
|
57
|
+
# reading multiple keys
|
58
|
+
h.get "hello", "goodbye"
|
59
|
+
```
|
60
|
+
|
61
|
+
You can remove keys from the hash with the remove method
|
62
|
+
```ruby
|
63
|
+
# removing a single key
|
64
|
+
h.remove "hello"
|
65
|
+
|
66
|
+
# removing multiple keys
|
67
|
+
h.remove "hello", "goodbye"
|
68
|
+
|
69
|
+
```
|
70
|
+
|
71
|
+
|
72
|
+
You can get the size of the hash.
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
h.size
|
76
|
+
```
|
77
|
+
|
78
|
+
You can see if a key exists in the hash.
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
h.include? "hello"
|
82
|
+
```
|
83
|
+
|
84
|
+
You can get all items in the hash.
|
85
|
+
|
86
|
+
```ruby
|
87
|
+
h.all
|
88
|
+
```
|
89
|
+
|
90
|
+
You can get all keys in the hash.
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
h.keys
|
94
|
+
```
|
95
|
+
|
96
|
+
You can get all values in the hash.
|
97
|
+
|
98
|
+
```ruby
|
99
|
+
h.values
|
100
|
+
```
|
101
|
+
|
102
|
+
The hash can be cleared of all items
|
103
|
+
```ruby
|
104
|
+
h.clear
|
105
|
+
```
|
106
|
+
|
107
|
+
The hash can also be hash to expire (in seconds).
|
108
|
+
```ruby
|
109
|
+
# expire in five minutes
|
110
|
+
h.expire 60*5
|
111
|
+
```
|
112
|
+
|
113
|
+
You can enumerate the hash in batches.
|
114
|
+
```ruby
|
115
|
+
#enumerate through the hash in batches of 100 items per redis op
|
116
|
+
h.enumerator(100).each{ |i| puts i }
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "redis/hash"
|
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(__FILE__)
|
data/bin/setup
ADDED
data/lib/redis/hash.rb
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
require "redis"
|
2
|
+
|
3
|
+
class RedisHash
|
4
|
+
attr_reader :name
|
5
|
+
|
6
|
+
VERSION = "0.0.1"
|
7
|
+
|
8
|
+
class InvalidNameException < StandardError; end;
|
9
|
+
class InvalidRedisConfigException < StandardError; end;
|
10
|
+
|
11
|
+
def initialize(name, redis_or_options = {})
|
12
|
+
name = name.to_s if name.kind_of? Symbol
|
13
|
+
|
14
|
+
raise InvalidNameException.new unless name.kind_of?(String) && name.size > 0
|
15
|
+
@name = name
|
16
|
+
@redis = if redis_or_options.kind_of? Redis
|
17
|
+
redis_or_options
|
18
|
+
elsif redis_or_options.kind_of? Hash
|
19
|
+
::Redis.new redis_or_options
|
20
|
+
else
|
21
|
+
raise InvalidRedisConfigException.new
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def get *keys
|
26
|
+
keys = keys.flatten
|
27
|
+
if keys.size > 0
|
28
|
+
values = if 1 == keys.size
|
29
|
+
@redis.hget name, keys.first
|
30
|
+
else
|
31
|
+
@redis.hmget name, *keys
|
32
|
+
end
|
33
|
+
|
34
|
+
keys.each_with_index.map do |k,i|
|
35
|
+
[k, values[i]]
|
36
|
+
end.to_h
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def set hash
|
41
|
+
if hash.size > 0
|
42
|
+
if 1 == hash.size
|
43
|
+
@redis.hset name, hash.keys.first, hash.values.first
|
44
|
+
else
|
45
|
+
@redis.hmset name, *(hash.map { |k, v| [k, v] }.flatten)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def set_if_does_not_exist key, value
|
51
|
+
@redis.hsetnx(name, key, value)
|
52
|
+
end
|
53
|
+
|
54
|
+
def increment_integer_key key, increment_amount
|
55
|
+
@redis.hincrby(name, key, increment_amount)
|
56
|
+
end
|
57
|
+
|
58
|
+
def increment_float_key key, increment_amount
|
59
|
+
@redis.hincrbyfloat(name, key, increment_amount)
|
60
|
+
end
|
61
|
+
|
62
|
+
def remove *keys
|
63
|
+
keys = keys.flatten
|
64
|
+
if keys.size > 0
|
65
|
+
@redis.hdel name, *keys
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def all
|
70
|
+
@redis.hgetall name
|
71
|
+
end
|
72
|
+
|
73
|
+
def keys
|
74
|
+
@redis.hkeys name
|
75
|
+
end
|
76
|
+
|
77
|
+
def values
|
78
|
+
@redis.hvals name
|
79
|
+
end
|
80
|
+
|
81
|
+
def include? key
|
82
|
+
@redis.hexists(name, key)
|
83
|
+
end
|
84
|
+
|
85
|
+
def size
|
86
|
+
@redis.hlen name
|
87
|
+
end
|
88
|
+
|
89
|
+
alias count size
|
90
|
+
|
91
|
+
def scan cursor = 0, amount = 10, match = "*"
|
92
|
+
@redis.hscan name, cursor, :count => amount, :match => match
|
93
|
+
end
|
94
|
+
|
95
|
+
def enumerator(slice_size = 10)
|
96
|
+
cursor = 0
|
97
|
+
Enumerator.new do |yielder|
|
98
|
+
loop do
|
99
|
+
cursor, items = scan cursor, slice_size
|
100
|
+
items.each do |item|
|
101
|
+
yielder << {item.first => item.last}
|
102
|
+
end
|
103
|
+
raise StopIteration if cursor.to_i.zero?
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
def clear
|
109
|
+
@redis.del name
|
110
|
+
{}
|
111
|
+
end
|
112
|
+
|
113
|
+
alias flush clear
|
114
|
+
|
115
|
+
def expire seconds
|
116
|
+
@redis.expire name, seconds
|
117
|
+
end
|
118
|
+
end
|
data/redis-hash.gemspec
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "redis/hash"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "redis-hash"
|
8
|
+
spec.version = RedisHash::VERSION
|
9
|
+
spec.authors = ["Misha Conway"]
|
10
|
+
spec.email = ["mishaAconway@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Lightweight wrapper over redis hashes.}
|
13
|
+
spec.description = %q{Lightweight wrapper over redis hashes.}
|
14
|
+
spec.homepage = "https://github.com/MishaConway/ruby-redis-hash"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Specify which files should be added to the gem when it is released.
|
18
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
19
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
20
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
21
|
+
end
|
22
|
+
spec.bindir = "exe"
|
23
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
|
+
spec.require_paths = ["lib"]
|
25
|
+
|
26
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
27
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
28
|
+
spec.add_development_dependency "rspec"
|
29
|
+
spec.add_runtime_dependency 'redis'
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: redis-hash
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Misha Conway
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-03-21 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.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.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: redis
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: Lightweight wrapper over redis hashes.
|
70
|
+
email:
|
71
|
+
- mishaAconway@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- Gemfile
|
78
|
+
- Gemfile.lock
|
79
|
+
- LICENSE.txt
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- bin/console
|
83
|
+
- bin/setup
|
84
|
+
- lib/redis/hash.rb
|
85
|
+
- redis-hash.gemspec
|
86
|
+
homepage: https://github.com/MishaConway/ruby-redis-hash
|
87
|
+
licenses:
|
88
|
+
- MIT
|
89
|
+
metadata: {}
|
90
|
+
post_install_message:
|
91
|
+
rdoc_options: []
|
92
|
+
require_paths:
|
93
|
+
- lib
|
94
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
requirements: []
|
105
|
+
rubyforge_project:
|
106
|
+
rubygems_version: 2.6.14
|
107
|
+
signing_key:
|
108
|
+
specification_version: 4
|
109
|
+
summary: Lightweight wrapper over redis hashes.
|
110
|
+
test_files: []
|