Skip to content

Instantly share code, notes, and snippets.

@JoelPM
Created January 14, 2016 22:29
Show Gist options
  • Save JoelPM/c2e3123197e502cfac99 to your computer and use it in GitHub Desktop.
Save JoelPM/c2e3123197e502cfac99 to your computer and use it in GitHub Desktop.
A simple plug for setting cache-control header with example of defining a pipeline
# lib/app/plugs/cache_control.ex
defmodule App.Plugs.CacheControll do
import Plug.Conn
def init({val, :seconds}), do: val
def init({val, :minutes}), do: 60*val
def init({val, :hours }), do: 60*60*val
def init({val, :days }), do: 24*60*60*val
def init({val, :weeks }), do: 7*24*60*60*val
def init({val, :months }), do: 30*7*24*60*60*val
def call(conn, seconds) do
Plug.Conn.put_resp_header(conn, "cache-control", "private, max-age=#{seconds}")
end
end
# web/router.ex
defmodule App.Router do
import App.Plugs.Cache
use App.Web, :router
# <snip>
pipeline :cache do
plug App.Plugs.Cache, {12, :months}
end
# <snip>
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment