Browse files

Add ability to turn on/off caching w/ config var

  • Loading branch information...
1 parent 163da63 commit 3aeff01524e82de0bc34892687d91b9e9898e59b @rwdaigle committed Oct 21, 2012
Showing with 19 additions and 3 deletions.
  1. +5 −1 app/controllers/gists_controller.rb
  2. +12 −1 app/models/gist.rb
  3. +2 −1 config/initializers/cache.rb
View
6 app/controllers/gists_controller.rb
@@ -9,7 +9,7 @@ class GistsController < ApplicationController
# end
def search
- if stale?(etag: search_etag, last_modified: current_user.last_gh_fetch)
+ if(!cache? || stale?(etag: search_etag, last_modified: current_user.last_gh_fetch))
@results = Gist.search(current_user, normalized_query)
if(feeling_lucky_directive? && lucky_result = @results.first)
redirect_to lucky_result.url
@@ -44,4 +44,8 @@ def pjax?
!request.headers['X-PJAX'].blank?
end
+ def cache?
+ CACHE_ACTIVE
+ end
+
end
View
13 app/models/gist.rb
@@ -51,7 +51,7 @@ def search(user, q)
log({ns: self, fn: __method__, query: q, measure: true, at: 'search'}, user)
results = []
begin
- results = Rails.cache.fetch(search_cache_key(user, q)) do
+ results = with_cache(search_cache_key(user, q)) do
if(!q.blank?)
log({ns: self, fn: __method__, query: q, measure: true}, user) do
tire.search do
@@ -88,6 +88,16 @@ def reindex(gists = scoped)
def search_cache_key(user, q)
"#{Rails.env.production? ? CACHE_VERSION : SecureRandom.hex}-user_id:#{user.id}-updated_at:#{user.last_gh_fetch ? user.last_gh_fetch.to_i : "never"}-#{q}"
end
+
+ def with_cache(key)
+ if CACHE_ACTIVE
+ Rails.cache.fetch(key) do
+ yield
+ end
+ else
+ yield
+ end
+ end
end
def to_log
@@ -111,4 +121,5 @@ def indexed_attributes
files: files.collect(&:indexed_attributes)
}
end
+
end
View
3 config/initializers/cache.rb
@@ -1 +1,2 @@
-CACHE_VERSION=ENV['CACHE_VERSION']
+CACHE_VERSION=ENV['CACHE_VERSION']
+CACHE_ACTIVE=ENV['CACHE_ACTIVE'] ? ENV['CACHE_ACTIVE'] == 'true' : Rails.env.production?

0 comments on commit 3aeff01

Please sign in to comment.