Permalink
Browse files

added ability to disable GET request memoization

  • Loading branch information...
1 parent b900567 commit 557b8e3bbf8e2451b841fba65688310e88163bbb @pauldix pauldix committed Oct 22, 2009
Showing with 19 additions and 2 deletions.
  1. +7 −2 lib/typhoeus/hydra.rb
  2. +12 −0 spec/typhoeus/hydra_spec.rb
View
@@ -1,6 +1,7 @@
module Typhoeus
class Hydra
def initialize(initial_pool_size = 10)
+ @memoize_requests = true
@multi = Multi.new
@easy_pool = []
initial_pool_size.times { @easy_pool << Easy.new }
@@ -29,15 +30,15 @@ def queue(request)
return if assign_to_stub(request)
if request.method == :get
- if @memoized_requests.has_key? request.url
+ if @memoize_requests && @memoized_requests.has_key?(request.url)
if response = @retrieved_from_cache[request.url]
request.response = response
request.call_handlers
else
@memoized_requests[request.url] << request
end
else
- @memoized_requests[request.url] = []
+ @memoized_requests[request.url] = [] if @memoize_requests
get_from_cache_or_queue(request)
end
else
@@ -56,6 +57,10 @@ def run
@memoized_requests = {}
@retrieved_from_cache = {}
end
+
+ def disable_memoization
+ @memoize_requests = false
+ end
def cache_getter(&block)
@cache_getter = block
@@ -109,6 +109,18 @@ def set(key, object, timeout = 0)
first.handled_response.should == second.handled_response
(Time.now - start_time).should < 1.2 # if it had run twice it would be ~ 2 seconds
end
+
+ it "can turn off memoization for GET requests" do
+ hydra = Typhoeus::Hydra.new
+ hydra.disable_memoization
+ first = Typhoeus::Request.new("http://localhost:3000/foo")
+ second = Typhoeus::Request.new("http://localhost:3000/foo")
+ hydra.queue first
+ hydra.queue second
+ hydra.run
+ first.response.body.should include("foo")
+ first.response.object_id.should_not == second.response.object_id
+ end
it "pulls GETs from cache" do
hydra = Typhoeus::Hydra.new

0 comments on commit 557b8e3

Please sign in to comment.