Class: IChannel::Redis
- Inherits:
-
Object
- Object
- IChannel::Redis
- Defined in:
- lib/ichannel/redis.rb
Instance Method Summary collapse
-
#close ⇒ Boolean
Close the channel.
-
#closed? ⇒ Boolean
Returns true when the channel is closed.
-
#empty? ⇒ Boolean
Returns true when the channel is empty.
- #initialize(serializer, options) ⇒ IChannel::Redis constructor
-
#last_msg ⇒ Object
Returns the last message written to the channel.
-
#readable? ⇒ Boolean
Returns true when the channel is readable.
-
#recv ⇒ Object
(also: #get)
Read an object from the channel.
-
#recv!(timeout = 1) ⇒ Object
(also: #get!)
The object read from the channel.
-
#redis? ⇒ Boolean
Returns true.
-
#unix_socket? ⇒ Boolean
Returns false.
-
#write(object) ⇒ void
(also: #put)
Add an object to the channel.
-
#write!(object, timeout = 0.1) ⇒ void
(also: #put!)
Add an object to the channel.
Constructor Details
#initialize(serializer, options) ⇒ IChannel::Redis
12 13 14 15 16 17 18 |
# File 'lib/ichannel/redis.rb', line 12 def initialize(serializer, ) @serializer = serializer @key = .delete(:key) || "channel" @redis = ::Redis.new() @last_msg = nil @closed = false end |
Instance Method Details
#close ⇒ Boolean
Close the channel.
34 35 36 37 38 39 |
# File 'lib/ichannel/redis.rb', line 34 def close unless closed? @redis.quit @closed = true end end |
#closed? ⇒ Boolean
Returns true when the channel is closed.
24 25 26 |
# File 'lib/ichannel/redis.rb', line 24 def closed? @closed end |
#empty? ⇒ Boolean
Returns true when the channel is empty.
136 137 138 |
# File 'lib/ichannel/redis.rb', line 136 def empty? @redis.llen(@key) == 0 end |
#last_msg ⇒ Object
Returns the last message written to the channel.
125 126 127 128 129 130 |
# File 'lib/ichannel/redis.rb', line 125 def last_msg while readable? @last_msg = get end @last_msg end |
#readable? ⇒ Boolean
Returns true when the channel is readable.
144 145 146 |
# File 'lib/ichannel/redis.rb', line 144 def readable? !closed? && !empty? end |
#recv ⇒ Object Also known as: get
Read an object from the channel.
90 91 92 |
# File 'lib/ichannel/redis.rb', line 90 def recv recv! nil end |
#recv!(timeout = 1) ⇒ Object Also known as: get!
Returns The object read from the channel.
108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/ichannel/redis.rb', line 108 def recv!(timeout = 1) if closed? raise IOError, 'The channel cannot be read from (closed).' end _, payload = @redis.brpop @key, timeout if payload @last_msg = @serializer.load(payload) else raise IChannel::TimeoutError, "timeout on read after #{timeout} seconds" end end |
#redis? ⇒ Boolean
Returns true.
164 165 166 |
# File 'lib/ichannel/redis.rb', line 164 def redis? true end |
#unix_socket? ⇒ Boolean
Returns false.
154 155 156 |
# File 'lib/ichannel/redis.rb', line 154 def unix_socket? false end |
#write(object) ⇒ void Also known as: put
This method returns an undefined value.
Add an object to the channel.
53 54 55 |
# File 'lib/ichannel/redis.rb', line 53 def write(object) write!(object, nil) end |
#write!(object, timeout = 0.1) ⇒ void Also known as: put!
This method returns an undefined value.
Add an object to the channel.
72 73 74 75 76 77 78 |
# File 'lib/ichannel/redis.rb', line 72 def write!(object, timeout = 0.1) if closed? raise IOError, 'The channel cannot be written to (closed)' end dump = @serializer.dump object @redis.lpush @key, dump end |