Skip to content

Instantly share code, notes, and snippets.

@LucasArruda

LucasArruda/redis.rb

Forked from joel/redis.rb
Created Nov 24, 2019
Embed
What would you like to do?
config/initializers/redis.rb
# Mock Redis
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
You can’t perform that action at this time.