Git Product home page Git Product logo

plug_ets_cache's Introduction

PlugEtsCache

Build Status

A simple http response caching system based on Plug and ETS. It easily integrates in every application that uses Plug, including a Phoenix dedicated adapter.

The main use case is when the contents of your web pages don't change in real time and are served to a multitude of visitors. Even if your server response times are in order of few tens of milliseconds, caching pages into ETS (hence into RAM) would shrink times to microseconds.

Cache duration can be configured with a combination of a ttl value and ttl_check. Check con_cache documentation for more details on this, PlugEtsCache uses it to read/write to ETS.

Installation

The package is available in Hex, follow these steps to install:

  1. Add plug_ets_cache to your list of dependencies in mix.exs:
def deps do
  # Get from hex
  [{:plug_ets_cache, "~> 0.3.0"}]
  # Or use the latest from master
  [{:plug_ets_cache, github: "andreapavoni/plug_ets_cache"}]
end
  1. Ensure plug_ets_cache is started before your application:
def application do
  [applications: [:plug_ets_cache]]
end

Usage

These are the common steps to setup PlugEtsCache:

  1. Set configuration in config/config.exs (the following values are defaults):
config :plug_ets_cache,
  db_name: :ets_cache,
  ttl_check: 60,
  ttl: 300
  1. Add PlugEtsCache.Plug to your router/plug:
plug PlugEtsCache.Plug

Now follow specific instructions below for your use case.

With Phoenix

Because Phoenix has a more complex lifecycle when it comes to send a response, it has a special module for this.

  1. Add use PlugEtsCache.Phoenix
  2. Call cache_response after you've sent a response:
defmodule MyApp.SomeController do
  use MyApp.Web, :controller
  use PlugEtsCache.Phoenix

  # ...

  def index(conn, _params) do
    # ...
    conn
    |> render("index.html")
    |> cache_response
  end

  # ...
end

With plain Plug

Supposing a very simple Plug module:

  1. Import PlugEtsCache.Response.cache_response/1 inside your module
  2. Call cache_response after you've sent a response:
defmodule FooController do
  use Plug.Router
  import PlugEtsCache.Response, only: [cache_response: 1]

  plug :match
  plug :dispatch

  get "/" do
    Plug.Conn.fetch_query_params(conn)
    |> put_resp_content_type("text/plain")
    |> send_resp(200, "Hello cache")
    |> cache_response
  end
end

Setting TTL

cache_response/1 will adhere to the ttl value in the config, but you can instead use cache_response/2 to specify a custom ttl for each response. Examples:

cache_response(conn, :timer.hours(1))
cache_response(conn, ttl: :timer.minutes(45))

Using a custom cache key

If you need greater control over the key used to cache the request you can use a custom function to build the cache key. The function needs to accept one argument, the Plug.Conn struct, and return the key.

cache_response(conn, [cache_key: fn conn -> conn.request_path end, ttl: :timer.minutes(10)])

Documentation

The docs can be found at https://hexdocs.pm/plug_ets_cache.

TODO

  • add more detailed docs

Contributing

Everyone is welcome to contribute to PlugEtsCache and help tackling existing issues!

Use the issue tracker for bug reports or feature requests.

Please, do your best to follow the Elixir's Code of Conduct.

License

This source code is released under MIT License. Check LICENSE file for more information.

plug_ets_cache's People

Contributors

andreapavoni avatar jeffjewiss avatar jerodsanto avatar vorce avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

plug_ets_cache's Issues

LayoutView.render/2 is undefined after json Response

Hey guys, this issue does not break the app but I do keep getting a LayoutView.render/2 is undefined error when I do the following:

def get_stuff(conn, _params) do
stuff = Context.list_stuff()
conn
|> put_status(200)
|> render(StuffView, "stuff.json", stuff: stuff)
|> cache_response
end

or

def get_stuff(conn, _params) do
conn
|> put_status(200)
|> json(%{status: "succes", message: "stuff"})
|> cache_response
end

Followed the simple steps given in the docs.. Any opinions on this?

Add coveralls

  • install coveralls elixir package
  • setup with coveralls.json with credentials for Travis

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.