Class: Red

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

Direct Known Subclasses

RedExpire, RedJSON

Defined Under Namespace

Classes: RedisUnavailable

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

Raises:



42
43
44
45
# File 'lib/redness/red.rb', line 42

def method_missing(method, *args)
  raise RedisUnavailable unless redis
  redis.send(method, *args)
end

Class Attribute Details

.redisObject

Returns the value of attribute redis.



7
8
9
# File 'lib/redness/red.rb', line 7

def redis
  @redis
end

Class Method Details

.client_versionObject



10
11
12
# File 'lib/redness/red.rb', line 10

def self.client_version
  @version ||= Redis::VERSION.scan(/\d+/).map { |s| s.to_i }
end

.delete(key) ⇒ Object



14
15
16
# File 'lib/redness/red.rb', line 14

def self.delete(key)
  redis.del(key)
end

Instance Method Details

#execute_with_uncertainty(fail_return = []) ⇒ Object



22
23
24
25
26
# File 'lib/redness/red.rb', line 22

def execute_with_uncertainty(fail_return = [])
  yield
rescue RedisUnavailable, Redis::CannotConnectError, Redis::TimeoutError
  fail_return
end

#multi_with_caution(fail_return = [], &block) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/redness/red.rb', line 28

def multi_with_caution(fail_return = [], &block)
  begin
    redis.multi(&block) || fail_return
  rescue Redis::TimeoutError
    # The redis client pipelines the commands internally. It's possible the
    # MULTI succeeds, but there's a timeout before reaching the EXEC. Try to
    # issue an extra discard to ensure the transaction is closed.
    redis.discard  # may raise Redis::CommandError if MULTI never succeeded
    raise
  end
rescue
  fail_return
end

#redisObject



18
19
20
# File 'lib/redness/red.rb', line 18

def redis
  Red.redis
end