Created
April 14, 2012 16:11
-
-
Save joel/2385486 to your computer and use it in GitHub Desktop.
config/initializers/redis.rb
This file contains hidden or 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
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