Skip to content

Instantly share code, notes, and snippets.

@joel
Created April 14, 2012 16:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save joel/2385486 to your computer and use it in GitHub Desktop.
Save joel/2385486 to your computer and use it in GitHub Desktop.
config/initializers/redis.rb
class MockRedis
attr_accessor :store
def initialize
@store = {}
end
def flushall
@store = {}
end
def incrby(key, increment)
@store[key] ||= 0
@store[key] += increment
end
def incr(key)
incrby(key, 1)
end
def hincrby(key, field, increment)
@store[key] ||= {}
@store[key][field] ||= 0
@store[key][field] += increment
end
def hvals(key)
@store[key] ||= {}
@store[key].values.map(&:to_s)
end
def hdel(key, *fields)
return 0 if @store[key].nil?
i = 0
fields.each { |field|
tmp = @store[key].delete(field)
i += 1 unless tmp.nil?
}
i
end
def hget(key, field)
@store[key] ||= {}
@store[key][field].try(:to_s)
end
def get(key)
@store[key].try(:to_s)
end
def expire(key, ttl)
end
end
if Rails.env.test?
$redis = MockRedis.new
else
redis = Redis.new(:host => 'localhost', :port => 6379, :thread_safe => true)
$redis = Redis::Namespace.new(Rails.env.to_s, :redis => redis)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment