redis-hash 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +1 -1
  3. data/lib/redis_hash.rb +39 -20
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8dd513ffa6a5374f208a77c5691d904159cd2b30
4
- data.tar.gz: 1fd22758f74c901188e26d6c39609d7439cf74f0
3
+ metadata.gz: 6bb41716c01e4eab76eff18483a814bb3c200b53
4
+ data.tar.gz: e7f14f833828cc07f90d72b36189c1018b92e390
5
5
  SHA512:
6
- metadata.gz: fb7f0371d442f9a920fe35dfdf5a09143834985b1a835301b9040b190595990abd061cdfa212590fdb6a0402e743e7fc8221c972dc3cbbf3ab1c2d8221f5513e
7
- data.tar.gz: fc40ad9e406554dc072f0e8f7c648b870cfa59bf5a83d5fa30bf441c9831fb7b7cfe24ac6eb52ba1fecfb76ed07c2f40e73625ec42494056d3ed55bde3a823f2
6
+ metadata.gz: 341facd819d2a24fdb89c1116f38df6d1f8dbe1364e0cc2dab4804a9c960228eae0dbbf8b47e5d2063ce0ca69f84c2acc4dfdd28610b53660c4c736efd2c205d
7
+ data.tar.gz: 4ba25754aab9bbed3c87dbd3da00afaf44236310b821166f137acd974d28131a8d96e39a528547053711ea527f5121a9e51f337f906733d75e00a8957b55eb51
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- redis-hash (0.0.1)
4
+ redis-hash (0.0.2)
5
5
  redis
6
6
 
7
7
  GEM
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.2"
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? Redis
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
- @redis.hget name, keys.first
32
+ with{|redis| redis.hget name, keys.first }
30
33
  else
31
- @redis.hmget name, *keys
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
- 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)
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
- @redis.hsetnx(name, hash.keys.first, hash.values.first)
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
- @redis.hincrby(name, key, increment_amount)
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
- @redis.hincrbyfloat(name, key, increment_amount)
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
- @redis.hdel name, *keys
70
+ with{|redis| redis.hdel name, *keys}
66
71
  end
67
72
  end
68
73
 
69
74
  def all
70
- @redis.hgetall name
75
+ with{|redis| redis.hgetall name}
71
76
  end
72
77
 
73
78
  def keys
74
- @redis.hkeys name
79
+ with{|redis| redis.hkeys name}
75
80
  end
76
81
 
77
82
  def values
78
- @redis.hvals name
83
+ with{|redis| redis.hvals name}
79
84
  end
80
85
 
81
86
  def include? key
82
- @redis.hexists(name, key)
87
+ with{|redis| redis.hexists(name, key)}
83
88
  end
84
89
 
85
90
  def size
86
- @redis.hlen name
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
- @redis.hscan name, cursor, :count => amount, :match => match
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
- @redis.del name
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
- @redis.expire name, seconds
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redis-hash
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Misha Conway