Class: Sinicum::Controllers::GlobalStateCache
- Inherits:
-
Object
- Object
- Sinicum::Controllers::GlobalStateCache
- Defined in:
- app/controllers/sinicum/controllers/global_state_cache.rb
Overview
Public: Filter to cache the result of a page if no changes in Magnolia occured.
Constant Summary collapse
- DEPLOY_REVISION_FILE =
"REVISION"
- THREAD_LOCAL_VAR_NAME =
"__sinicum_global_state_cache"
Class Method Summary collapse
Instance Method Summary collapse
- #cache_response ⇒ Object
-
#initialize(controller) ⇒ GlobalStateCache
constructor
A new instance of GlobalStateCache.
- #render_or_proceed ⇒ Object
Constructor Details
#initialize(controller) ⇒ GlobalStateCache
Returns a new instance of GlobalStateCache.
9 10 11 12 |
# File 'app/controllers/sinicum/controllers/global_state_cache.rb', line 9 def initialize(controller) @controller = controller @global_jcr_cache_key = Sinicum::Jcr::Cache::GlobalCache.new.current_key end |
Class Method Details
.after(controller) ⇒ Object
22 23 24 25 26 27 28 29 30 31 |
# File 'app/controllers/sinicum/controllers/global_state_cache.rb', line 22 def self.after(controller) if do_cache? begin instance = Thread.current[THREAD_LOCAL_VAR_NAME] instance.cache_response if instance ensure Thread.current[THREAD_LOCAL_VAR_NAME] = nil end end end |
.before(controller) ⇒ Object
14 15 16 17 18 19 20 |
# File 'app/controllers/sinicum/controllers/global_state_cache.rb', line 14 def self.before(controller) if do_cache? instance = new(controller) Thread.current[THREAD_LOCAL_VAR_NAME] = instance instance.render_or_proceed end end |
Instance Method Details
#cache_response ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'app/controllers/sinicum/controllers/global_state_cache.rb', line 45 def cache_response response = @controller.response if @controller.request.get? && response.cache_control[:public] && response.cache_control[:max_age] && response.cache_control[:max_age] > 0 cache_content = { body: response.body, cache_control: response.cache_control, status: response.status } Rails.cache.write(cache_key, cache_content) end end |
#render_or_proceed ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 |
# File 'app/controllers/sinicum/controllers/global_state_cache.rb', line 33 def render_or_proceed cached = Rails.cache.fetch(cache_key) if cached @controller.response.cache_control.merge!(cached[:cache_control]) @controller.response.status = cached[:status] @controller.response.headers["X-SCache"] = "true" @controller.render text: cached[:body] else @controller.response.headers["X-SCache"] = "false" end end |