Class: RedisHash

Inherits:
Object
  • Object
show all
Defined in:
lib/redis_hash.rb

Defined Under Namespace

Classes: InvalidNameException, InvalidRedisConfigException

Constant Summary collapse

VERSION =
"0.0.4"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, redis_or_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
# File 'lib/redis_hash.rb', line 11

def initialize(name, redis_or_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
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/redis_hash.rb', line 4

def name
  @name
end

Instance Method Details

#allObject



77
78
79
# File 'lib/redis_hash.rb', line 77

def all
	with{|redis| redis.hgetall name}
end

#clearObject Also known as: flush



116
117
118
119
# File 'lib/redis_hash.rb', line 116

def clear
	with{|redis| redis.del name}
	{}
end

#enumerator(slice_size = 10) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/redis_hash.rb', line 103

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



123
124
125
# File 'lib/redis_hash.rb', line 123

def expire seconds
	with{|redis| redis.expire name, seconds}
end

#get(*keys) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/redis_hash.rb', line 31

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
	end
end

#include?(key) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/redis_hash.rb', line 89

def include? key
	with{|redis| redis.hexists(name, key)}
end

#increment_float_key(key, increment_amount = 1) ⇒ Object



66
67
68
# File 'lib/redis_hash.rb', line 66

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



62
63
64
# File 'lib/redis_hash.rb', line 62

def increment_integer_key key, increment_amount = 1
	with{|redis| redis.hincrby(name, key, increment_amount)}
end

#keysObject



81
82
83
# File 'lib/redis_hash.rb', line 81

def keys
	with{|redis| redis.hkeys name}
end

#remove(*keys) ⇒ Object



70
71
72
73
74
75
# File 'lib/redis_hash.rb', line 70

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



99
100
101
# File 'lib/redis_hash.rb', line 99

def scan cursor = 0, amount = 10, match = "*"
	with{|redis| redis.hscan name, cursor, :count => amount, :match => match}
end

#set(hash) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/redis_hash.rb', line 46

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



58
59
60
# File 'lib/redis_hash.rb', line 58

def set_if_does_not_exist hash
	with{|redis| redis.hsetnx(name, hash.keys.first, hash.values.first)}
end

#sizeObject Also known as: count



93
94
95
# File 'lib/redis_hash.rb', line 93

def size
	with{|redis| redis.hlen name}
end

#valuesObject



85
86
87
# File 'lib/redis_hash.rb', line 85

def values
	with{|redis| redis.hvals name}
end