Class: Red

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

Direct Known Subclasses

RedExpire, RedJSON

Defined Under Namespace

Classes: RedisUnavailable

Constant Summary collapse

REDIS_ERRORS =
[
  RedisUnavailable,
  Redis::CannotConnectError,
  Redis::ConnectionError,
  Redis::TimeoutError
]

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:



49
50
51
52
# File 'lib/redness/red.rb', line 49

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

Class Attribute Details

.redisObject

Returns the value of attribute redis.



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

def redis
  @redis
end

Class Method Details

.client_versionObject



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

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

.delete(key) ⇒ Object



21
22
23
# File 'lib/redness/red.rb', line 21

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

Instance Method Details

#execute_with_uncertainty(fail_return = []) ⇒ Object



29
30
31
32
33
# File 'lib/redness/red.rb', line 29

def execute_with_uncertainty(fail_return = [])
  yield
rescue *REDIS_ERRORS
  fail_return
end

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



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/redness/red.rb', line 35

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



25
26
27
# File 'lib/redness/red.rb', line 25

def redis
  Red.redis
end