-
-
Save jfredett/4271899 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Version 2 -- the cool version | |
require 'redis' | |
require 'forwardable' #standard ruby library | |
class RedisTester | |
# this way, we create the instance and check to see it's running every time | |
# we ask for it automatically. | |
def redis | |
@redis ||= Redis.new | |
raise unless redis_is_running? | |
@redis | |
end | |
delegate [:set, :get] => :redis | |
def redis_is_running? | |
@redis.ping == 'PONG' | |
rescue Redis::CannotConnectError | |
return false | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'redis' | |
# Version 1 | |
class RedisTester | |
# this way, we create the instance and check to see it's running every time | |
# we ask for it automatically. | |
def redis | |
@redis ||= Redis.new | |
raise unless redis_is_running? | |
@redis | |
end | |
def set(key, value) | |
redis.set key, value | |
end | |
def get(key) | |
@redis.get key | |
end | |
def redis_is_running? | |
#no need to explicitly return true, that'll happen here. This | |
# method can either fail or return PONG | |
@redis.ping == 'PONG' | |
#ruby has a special syntax for catching an error in a method like this. | |
# it's just shorthand. | |
rescue Redis::CannotConnectError | |
return false | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment