plug_response_cache v0.1.1 PlugResponseCache View Source
The response cache plug can cache an entire response body for a set amount of time.
To enable the cache, add the plug to your pipeline as early as possible. Here’s what that looks like in a clean Phoenix installation:
pipeline :browser do
plug(:accepts, ["html"])
plug(PlugResponseCache)
plug(:fetch_session)
plug(:fetch_flash)
plug(:protect_from_forgery)
plug(:put_secure_browser_headers)
end
The plug should be configured with the :plug_response_cache
config key. This
belongs in your application’s configuration file:
config :plug_response_cache,
enabled: false,
store: MyApp.ResponseCache.MyCustomStore
profile: MyApp.ResponseCache.MyCustomProfile
enabled
: Enables or disables the response cache entirely (default:true
)store
: A custom cache store — a module that implementsPlugResponseCache.Store
callbacks (default:PlugResponseCache.Stores.Ets
)profile
: A custom cache profile — a module that implementsPlugResponseCache.Profile
callbacks (default:PlugResponseCache.Profiles.Default
)debug
: Logs the response cache’s result on every request whentrue
(default:false
)
Since every option has a default, the response cache will also work without specifying any custom configuration.
Options are also passed to the cache profile, so if a profile has options
simply add them to the list. For example, the default profile
(PlugResponseCache.Profiles.Default
) has an expiration_time
option to
determine for how many minutes the response should be cached.
config :plug_response_cache,
expiration_time: 5
It’s also possible to pass options to the plug. This is useful for enabling or disabling the response cache on the fly, or for toggling the debug mode.
pipeline :browser do
# ...
plug(PlugResponseCache, debug: true)
# ...
end
It’s not recommended to configure the profile & store like this because they might require a supervisor, which needs to be known ahead of time.
Link to this section Summary
Functions
While you can pass options to the plug, it’s recommended to configure the response cache through the application’s config, so the response cache has the necessary processes running
Link to this section Types
hit() :: {:hit, {Plug.Conn.status(), Plug.Conn.body(), expire_time()}}
miss() :: {:miss, :cold} | {:miss, :expired} | {:miss, :request_rejected} | {:miss, :response_rejected}
Link to this section Functions
While you can pass options to the plug, it’s recommended to configure the response cache through the application’s config, so the response cache has the necessary processes running.