Class: RedisUniqueQueue

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

Defined Under Namespace

Classes: InvalidNameException, InvalidRedisConfigException

Constant Summary collapse

VERSION =
"2.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, redis_or_options = {}, more_options = {}) ⇒ RedisUniqueQueue

Returns a new instance of RedisUniqueQueue.



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_unique_queue.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

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#allObject



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

def all
	peek 0, size
end

#backObject



61
62
63
# File 'lib/redis_unique_queue.rb', line 61

def back
	with { |redis| redis.zrevrange(name, 0, 0).first }
end

#clearObject



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

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

#expire(seconds) ⇒ Object



94
95
96
# File 'lib/redis_unique_queue.rb', line 94

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

#frontObject



57
58
59
# File 'lib/redis_unique_queue.rb', line 57

def front
	with { |redis| redis.zrange(name, 0, 0).first }
end

#include?(data) ⇒ Boolean



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

def include? data
	!with { |redis| redis.zscore(name, data).nil? }
end

#peek(index, amount = 1) ⇒ Object



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

def peek index, amount = 1
	with { |redis| redis.zrange name, index, index + amount - 1 }
end

#popObject



45
46
47
# File 'lib/redis_unique_queue.rb', line 45

def pop
	block_on_atomic_attempt { attempt_atomic_pop }
end

#pop_allObject



49
50
51
# File 'lib/redis_unique_queue.rb', line 49

def pop_all
	block_on_atomic_attempt { attempt_atomic_pop_all }
end

#pop_multi(amount) ⇒ Object



53
54
55
# File 'lib/redis_unique_queue.rb', line 53

def pop_multi amount
	block_on_atomic_attempt { attempt_atomic_pop_multi amount }
end

#push(data) ⇒ Object



35
36
37
# File 'lib/redis_unique_queue.rb', line 35

def push data
	[block_on_atomic_attempt { attempt_atomic_push_multi(data) }].flatten.first
end

#push_multi(*values) ⇒ Object



39
40
41
42
43
# File 'lib/redis_unique_queue.rb', line 39

def push_multi *values
	if values.size > 0
		block_on_atomic_attempt { attempt_atomic_push_multi(*values) }
	end
end

#remove(data) ⇒ Object



65
66
67
# File 'lib/redis_unique_queue.rb', line 65

def remove data
	with { |redis| redis.zrem name, data }
end

#remove_item_by_index(index) ⇒ Object



69
70
71
# File 'lib/redis_unique_queue.rb', line 69

def remove_item_by_index index
	with { |redis| redis.zremrangebyrank name, index, index }
end

#sizeObject



73
74
75
# File 'lib/redis_unique_queue.rb', line 73

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