Class: IChannel::Redis

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

Instance Method Summary collapse

Constructor Details

#initialize(serializer, options) ⇒ IChannel::Redis

Parameters:

  • serializer (#dump, #load)

    A serializer.

  • options (Hash)

    A Hash of options to pass onto the redis-rb client.



12
13
14
15
16
17
18
# File 'lib/ichannel/redis.rb', line 12

def initialize(serializer, options)
  @serializer = serializer
  @key = options.delete(:key) || "channel"
  @redis = ::Redis.new(options)
  @last_msg = nil
  @closed = false
end

Instance Method Details

#closeBoolean

Close the channel.

Returns:

  • (Boolean)

    Returns true when the channel has been closed.



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.

Returns:

  • (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.

Returns:

  • (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_msgObject

Returns the last message written to the channel.

Returns:

  • (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.

Returns:

  • (Boolean)

    Returns true when the channel is readable.



144
145
146
# File 'lib/ichannel/redis.rb', line 144

def readable?
  !closed? && !empty?
end

#recvObject Also known as: get

Read an object from the channel.

Returns:

  • (Object)

    The object read from the channel.

Raises:

  • (IOError)

    When the channel is closed or empty.



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.

Parameters:

  • timeout (Fixnum) (defaults to: 1)

    The number of seconds (as an Integer) to wait for a read.

Returns:

  • (Object)

    The object read from the channel.

Raises:

  • (IOError)

    When the channel is closed or empty.

  • (IChannel::TimeoutError)

    When timeout seconds pass and the read has not completed.



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.

Returns:

  • (Boolean)

    Returns true.

See Also:



164
165
166
# File 'lib/ichannel/redis.rb', line 164

def redis?
  true
end

#unix_socket?Boolean

Returns false.

Returns:

  • (Boolean)

    Returns false.

See Also:



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.

Parameters:

  • object (Object)

    The object to add to the channel.

Raises:

  • (IOError)

    When the channel is closed.



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.

Parameters:

  • object (Object)

    An object to add.

  • timeout (Fixnum) (defaults to: 0.1)

    This argument is a no-op for a write to a redis channel.

Raises:

  • (IOError)

    When the channel is closed.



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