Created
January 14, 2016 22:29
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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