diff --git a/lib/rack/redis_throttle.rb b/lib/rack/redis_throttle.rb index d96ca6f..e1d6fdb 100644 --- a/lib/rack/redis_throttle.rb +++ b/lib/rack/redis_throttle.rb @@ -3,9 +3,6 @@ require 'redis' require 'hiredis' require 'redis-namespace' -require 'active_support/core_ext/hash/reverse_merge' -require 'active_support/core_ext/time/calculations' -require 'active_support/core_ext/date/calculations' module Rack module RedisThrottle diff --git a/lib/rack/redis_throttle/connection.rb b/lib/rack/redis_throttle/connection.rb index cd4667c..19c44df 100644 --- a/lib/rack/redis_throttle/connection.rb +++ b/lib/rack/redis_throttle/connection.rb @@ -5,9 +5,9 @@ module RedisThrottle class Connection def self.create(options={}) - url = redis_provider || 'redis://localhost:6379/0' - options.reverse_merge!({ url: url }) - client = Redis.connect(url: options[:url], driver: :hiredis) + options[:url] = redis_provider || 'redis://localhost:6379/0' unless options.has_key?(:url) + method = Redis::VERSION.to_i >= 3 ? :new : :connect + client = Redis.send(method, url: options[:url], driver: :hiredis) Redis::Namespace.new("redis-throttle:#{ENV['RACK_ENV']}:rate", redis: client) end diff --git a/lib/rack/redis_throttle/limiter.rb b/lib/rack/redis_throttle/limiter.rb index 7c1bf58..5f34d01 100644 --- a/lib/rack/redis_throttle/limiter.rb +++ b/lib/rack/redis_throttle/limiter.rb @@ -5,7 +5,7 @@ module RedisThrottle class Limiter < Rack::Throttle::Limiter def initialize(app, options = {}) - options.reverse_merge!({ cache: Rack::RedisThrottle::Connection.create }) + options[:cache] = Rack::RedisThrottle::Connection.create(options) unless options.has_key?(:cache) @app, @options = app, options end @@ -53,7 +53,7 @@ def cache_incr(request) begin key = cache_key(request) count = cache.incr(key) - cache.expire(key, 1.day) if count == 1 + cache.expire(key, 86400) if count == 1 count rescue Redis::BaseConnectionError => e puts "ERROR: Redis connection not available. Rescuing cache.incr(key)" if ENV['DEBUG'] diff --git a/redis_throttle.gemspec b/redis_throttle.gemspec index f3cef09..08e875c 100644 --- a/redis_throttle.gemspec +++ b/redis_throttle.gemspec @@ -30,7 +30,6 @@ Gem::Specification.new do |gem| gem.add_dependency 'redis' gem.add_dependency 'hiredis' gem.add_dependency 'redis-namespace' - gem.add_dependency 'activesupport' gem.add_development_dependency 'rake' gem.add_development_dependency 'rspec' @@ -45,4 +44,5 @@ Gem::Specification.new do |gem| gem.add_development_dependency 'guard-rspec' gem.add_development_dependency 'fuubar' gem.add_development_dependency 'growl' + gem.add_development_dependency 'activesupport' end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 43b4f07..db43473 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -5,6 +5,7 @@ require 'mock_redis' require 'rspec' require 'timecop' +require 'active_support/core_ext/time/calculations' require File.dirname(__FILE__) + '/fixtures/fake_app'