Skip to content

Instantly share code, notes, and snippets.

@jfredett
Forked from ZachMassia/redistester.rb
Created December 12, 2012 21:42
Show Gist options
  • Save jfredett/4271899 to your computer and use it in GitHub Desktop.
Save jfredett/4271899 to your computer and use it in GitHub Desktop.
# 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
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