Class: RedisHash
- Inherits:
-
Object
show all
- Defined in:
- lib/redis_hash.rb
Defined Under Namespace
Classes: InvalidNameException, InvalidRedisConfigException
Constant Summary
collapse
- VERSION =
"0.0.8"
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(name, redis_or_options = {}, more_options = {}) ⇒ RedisHash
Returns a new instance of RedisHash.
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/redis_hash.rb', line 11
def initialize(name, redis_or_options = {}, more_options = {})
name = name.to_s if name.kind_of? Symbol
raise InvalidNameException.new unless name.kind_of?(String) && name.size > 0
@name = name
@redis = if redis_or_options.kind_of?(Redis)
redis_or_options
elsif redis_or_options.kind_of? Hash
::Redis.new redis_or_options
elsif defined?(ActiveSupport::Cache::RedisStore) && redis_or_options.kind_of?(ActiveSupport::Cache::RedisStore)
@pooled = redis_or_options.data.kind_of?(ConnectionPool)
redis_or_options.data
elsif defined?(ConnectionPool) && redis_or_options.kind_of?(ConnectionPool)
@pooled = true
redis_or_options
else
raise InvalidRedisConfigException.new
end
if more_options.kind_of?(Hash) && more_options[:expire]
expire more_options[:expire]
end
end
|
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
4
5
6
|
# File 'lib/redis_hash.rb', line 4
def name
@name
end
|
Instance Method Details
#all ⇒ Object
83
84
85
|
# File 'lib/redis_hash.rb', line 83
def all
with{|redis| redis.hgetall name}
end
|
#clear ⇒ Object
Also known as:
flush
122
123
124
125
|
# File 'lib/redis_hash.rb', line 122
def clear
with{|redis| redis.del name}
{}
end
|
#enumerator(slice_size = 10) ⇒ Object
109
110
111
112
113
114
115
116
117
118
119
120
|
# File 'lib/redis_hash.rb', line 109
def enumerator(slice_size = 10)
cursor = 0
Enumerator.new do |yielder|
loop do
cursor, items = scan cursor, slice_size
items.each do |item|
yielder << {item.first => item.last}
end
raise StopIteration if cursor.to_i.zero?
end
end
end
|
#expire(seconds) ⇒ Object
129
130
131
|
# File 'lib/redis_hash.rb', line 129
def expire seconds
with{|redis| redis.expire name, seconds}
end
|
#get(*keys) ⇒ Object
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/redis_hash.rb', line 35
def get *keys
keys = keys.flatten
if keys.size > 0
values = if 1 == keys.size
[with{|redis| redis.hget(name, keys.first) }]
else
with{|redis| redis.hmget name, *keys }
end
keys.each_with_index.map do |k,i|
[k, values[i]]
end.to_h
else
{}
end
end
|
#include?(key) ⇒ Boolean
95
96
97
|
# File 'lib/redis_hash.rb', line 95
def include? key
with{|redis| redis.hexists(name, key)}
end
|
#increment_float_key(key, increment_amount = 1) ⇒ Object
72
73
74
|
# File 'lib/redis_hash.rb', line 72
def increment_float_key key, increment_amount = 1
with{|redis| redis.hincrbyfloat(name, key, increment_amount)}
end
|
#increment_integer_key(key, increment_amount = 1) ⇒ Object
68
69
70
|
# File 'lib/redis_hash.rb', line 68
def increment_integer_key key, increment_amount = 1
with{|redis| redis.hincrby(name, key, increment_amount)}
end
|
#keys ⇒ Object
87
88
89
|
# File 'lib/redis_hash.rb', line 87
def keys
with{|redis| redis.hkeys name}
end
|
#remove(*keys) ⇒ Object
76
77
78
79
80
81
|
# File 'lib/redis_hash.rb', line 76
def remove *keys
keys = keys.flatten
if keys.size > 0
with{|redis| redis.hdel name, keys}
end
end
|
#scan(cursor = 0, amount = 10, match = "*") ⇒ Object
105
106
107
|
# File 'lib/redis_hash.rb', line 105
def scan cursor = 0, amount = 10, match = "*"
with{|redis| redis.hscan name, cursor, :count => amount, :match => match}
end
|
#set(hash) ⇒ Object
52
53
54
55
56
57
58
59
60
61
62
|
# File 'lib/redis_hash.rb', line 52
def set hash
if hash.size > 0
with do |redis|
if 1 == hash.size
redis.hset name, hash.keys.first, hash.values.first
else
redis.hmset name, *(hash.map { |k, v| [k, v] }.flatten)
end
end
end
end
|
#set_if_does_not_exist(hash) ⇒ Object
64
65
66
|
# File 'lib/redis_hash.rb', line 64
def set_if_does_not_exist hash
with{|redis| redis.hsetnx(name, hash.keys.first, hash.values.first)}
end
|
#size ⇒ Object
Also known as:
count
99
100
101
|
# File 'lib/redis_hash.rb', line 99
def size
with{|redis| redis.hlen name}
end
|
#values ⇒ Object
91
92
93
|
# File 'lib/redis_hash.rb', line 91
def values
with{|redis| redis.hvals name}
end
|