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

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

Link to this type expire_time() View Source
expire_time() :: DateTime.t() | :never
Link to this type miss() View Source
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.