redis-hash 0.0.2 → 0.0.3
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 +4 -4
- data/Gemfile.lock +1 -1
- data/lib/redis_hash.rb +39 -20
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6bb41716c01e4eab76eff18483a814bb3c200b53
|
4
|
+
data.tar.gz: e7f14f833828cc07f90d72b36189c1018b92e390
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 341facd819d2a24fdb89c1116f38df6d1f8dbe1364e0cc2dab4804a9c960228eae0dbbf8b47e5d2063ce0ca69f84c2acc4dfdd28610b53660c4c736efd2c205d
|
7
|
+
data.tar.gz: 4ba25754aab9bbed3c87dbd3da00afaf44236310b821166f137acd974d28131a8d96e39a528547053711ea527f5121a9e51f337f906733d75e00a8957b55eb51
|
data/Gemfile.lock
CHANGED
data/lib/redis_hash.rb
CHANGED
@@ -3,7 +3,7 @@ require "redis"
|
|
3
3
|
class RedisHash
|
4
4
|
attr_reader :name
|
5
5
|
|
6
|
-
VERSION = "0.0.
|
6
|
+
VERSION = "0.0.3"
|
7
7
|
|
8
8
|
class InvalidNameException < StandardError; end;
|
9
9
|
class InvalidRedisConfigException < StandardError; end;
|
@@ -13,10 +13,13 @@ class RedisHash
|
|
13
13
|
|
14
14
|
raise InvalidNameException.new unless name.kind_of?(String) && name.size > 0
|
15
15
|
@name = name
|
16
|
-
@redis = if redis_or_options.kind_of?
|
16
|
+
@redis = if redis_or_options.kind_of?(Redis)
|
17
17
|
redis_or_options
|
18
18
|
elsif redis_or_options.kind_of? Hash
|
19
19
|
::Redis.new redis_or_options
|
20
|
+
elsif defined?(ConnectionPool) && redis_or_options.kind_of?(ConnectionPool)
|
21
|
+
@pooled = true
|
22
|
+
redis_or_options
|
20
23
|
else
|
21
24
|
raise InvalidRedisConfigException.new
|
22
25
|
end
|
@@ -26,9 +29,9 @@ class RedisHash
|
|
26
29
|
keys = keys.flatten
|
27
30
|
if keys.size > 0
|
28
31
|
values = if 1 == keys.size
|
29
|
-
|
32
|
+
with{|redis| redis.hget name, keys.first }
|
30
33
|
else
|
31
|
-
|
34
|
+
with{|redis| redis.hmget name, *keys }
|
32
35
|
end
|
33
36
|
|
34
37
|
keys.each_with_index.map do |k,i|
|
@@ -39,57 +42,59 @@ class RedisHash
|
|
39
42
|
|
40
43
|
def set hash
|
41
44
|
if hash.size > 0
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
45
|
+
with do |redis|
|
46
|
+
if 1 == hash.size
|
47
|
+
redis.hset name, hash.keys.first, hash.values.first
|
48
|
+
else
|
49
|
+
redis.hmset name, *(hash.map { |k, v| [k, v] }.flatten)
|
50
|
+
end
|
46
51
|
end
|
47
52
|
end
|
48
53
|
end
|
49
54
|
|
50
55
|
def set_if_does_not_exist hash
|
51
|
-
|
56
|
+
with{|redis| redis.hsetnx(name, hash.keys.first, hash.values.first)}
|
52
57
|
end
|
53
58
|
|
54
59
|
def increment_integer_key key, increment_amount = 1
|
55
|
-
|
60
|
+
with{|redis| redis.hincrby(name, key, increment_amount)}
|
56
61
|
end
|
57
62
|
|
58
63
|
def increment_float_key key, increment_amount = 1
|
59
|
-
|
64
|
+
with{|redis| redis.hincrbyfloat(name, key, increment_amount)}
|
60
65
|
end
|
61
66
|
|
62
67
|
def remove *keys
|
63
68
|
keys = keys.flatten
|
64
69
|
if keys.size > 0
|
65
|
-
|
70
|
+
with{|redis| redis.hdel name, *keys}
|
66
71
|
end
|
67
72
|
end
|
68
73
|
|
69
74
|
def all
|
70
|
-
|
75
|
+
with{|redis| redis.hgetall name}
|
71
76
|
end
|
72
77
|
|
73
78
|
def keys
|
74
|
-
|
79
|
+
with{|redis| redis.hkeys name}
|
75
80
|
end
|
76
81
|
|
77
82
|
def values
|
78
|
-
|
83
|
+
with{|redis| redis.hvals name}
|
79
84
|
end
|
80
85
|
|
81
86
|
def include? key
|
82
|
-
|
87
|
+
with{|redis| redis.hexists(name, key)}
|
83
88
|
end
|
84
89
|
|
85
90
|
def size
|
86
|
-
|
91
|
+
with{|redis| redis.hlen name}
|
87
92
|
end
|
88
93
|
|
89
94
|
alias count size
|
90
95
|
|
91
96
|
def scan cursor = 0, amount = 10, match = "*"
|
92
|
-
|
97
|
+
with{|redis| redis.hscan name, cursor, :count => amount, :match => match}
|
93
98
|
end
|
94
99
|
|
95
100
|
def enumerator(slice_size = 10)
|
@@ -106,13 +111,27 @@ class RedisHash
|
|
106
111
|
end
|
107
112
|
|
108
113
|
def clear
|
109
|
-
|
114
|
+
with{|redis| redis.del name}
|
110
115
|
{}
|
111
116
|
end
|
112
117
|
|
113
118
|
alias flush clear
|
114
119
|
|
115
120
|
def expire seconds
|
116
|
-
|
121
|
+
with{|redis| redis.expire name, seconds}
|
122
|
+
end
|
123
|
+
|
124
|
+
private
|
125
|
+
|
126
|
+
def with(&block)
|
127
|
+
if pooled?
|
128
|
+
@redis.with(&block)
|
129
|
+
else
|
130
|
+
block.call(@redis)
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
def pooled?
|
135
|
+
!!@pooled
|
117
136
|
end
|
118
137
|
end
|