Class: Sitepress::Middleware::RequestCache

Inherits:
Object
  • Object
show all
Defined in:
lib/sitepress/middleware/request_cache.rb

Overview

Reloads the Sitepress cache between requests if cache resources is enabled. This ensures that the Sitepress resources are loaded only once per request for development environments so that the site isn’re reloaded per call. Used from the Rails app and from the stand-alone Sitepress server.

Instance Method Summary collapse

Constructor Details

#initialize(app, site:) ⇒ RequestCache

Returns a new instance of RequestCache.



8
9
10
# File 'lib/sitepress/middleware/request_cache.rb', line 8

def initialize(app, site:)
  @app, @site = app, site
end

Instance Method Details

#call(env) ⇒ Object

Cache resources for the duration of the request, even if caching is disabled.



14
15
16
17
18
19
20
21
22
23
# File 'lib/sitepress/middleware/request_cache.rb', line 14

def call(env)
  cache_resources = @site.cache_resources
  begin
    @site.cache_resources = true
    @app.call env
  ensure
    @site.cache_resources = cache_resources
    @site.clear_resources_cache unless @site.cache_resources
  end
end