Skip to content

Commit

Permalink
[#128] Including query params in keys for redis cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Chirag Aggarwal committed Feb 26, 2016
1 parent 56a14ab commit 8ea5e31
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/wrest/cache_proxy.rb
Expand Up @@ -40,7 +40,7 @@ def log_cached_response
end

def get
cached_response = @cache_store[@get.uri.to_s]
cached_response = @cache_store[@get.full_uri_string]
return get_fresh_response if cached_response.nil?

if cached_response.expired?
Expand All @@ -61,12 +61,12 @@ def update_cache_headers_for(cached_response, new_response)
end

def cache(response)
@cache_store[@get.uri.uri_string] = response.clone if response && response.cacheable?
@cache_store[@get.full_uri_string] = response.clone if response && response.cacheable?
end

#:nodoc:
def get_fresh_response
@cache_store.delete @get.hash
@cache_store.delete @get.full_uri_string

response = @get.invoke_without_cache_check

Expand Down
1 change: 1 addition & 0 deletions spec/sample_app/Gemfile
@@ -1,5 +1,6 @@
# A sample Gemfile
source "http://rubygems.org"

gem 'rake'
gem 'sinatra'
gem 'json_pure'
5 changes: 5 additions & 0 deletions spec/sample_app/lib/sample_app.rb
Expand Up @@ -187,5 +187,10 @@ def request_headers
When the cache entry at the client expires, it will send a GET request with an If-Modified-Since. This URI will always respond to any validation request with a Not-Modified "
end

get '/query_based_response' do
headers "Expires" => (Time.now + 84600 * 7).httpdate
params.to_s
end

end
end
27 changes: 27 additions & 0 deletions spec/wrest/cache_proxy_spec.rb
Expand Up @@ -225,4 +225,31 @@
@cache_proxy.get
end
end

describe 'redis specific caching', functional: true do
before :all do
Wrest::Caching.enable_redis
end

before :each do
@redis_cache = Wrest::Caching::Redis.new
@request_uri = 'http://localhost:3000/query_based_response'.to_uri
query_params_one = {name: 'Example', age: 21}
query_params_two = {height: 174, units: 'cm'}
@get_one = Wrest::Native::Get.new(@request_uri, query_params_one, {}, {:cache_store => @redis_cache})
@get_two = Wrest::Native::Get.new(@request_uri, query_params_two, {}, {:cache_store => @redis_cache})
end

after :each do
@redis_cache.delete(@get_one)
@redis_cache.delete(@get_two)
end

it 'should have different responses for get request with same scheme, authority, paths but different query params, given that response changes with query params' do
@cache_proxy_one = Wrest::CacheProxy.new(@get_one, @redis_cache)
@cache_proxy_two = Wrest::CacheProxy.new(@get_two, @redis_cache)
expect(@cache_proxy_one.get).to_not eq(@cache_proxy_two.get)
end
end

end

0 comments on commit 8ea5e31

Please sign in to comment.