{"id":18132700,"url":"https://github.com/andreapavoni/plug_ets_cache","last_synced_at":"2025-08-23T15:39:20.641Z","repository":{"id":57534548,"uuid":"88748715","full_name":"andreapavoni/plug_ets_cache","owner":"andreapavoni","description":"A simple caching system based on Plug and ETS.","archived":false,"fork":false,"pushed_at":"2019-02-15T09:37:58.000Z","size":35,"stargazers_count":26,"open_issues_count":1,"forks_count":7,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-07-12T18:52:10.537Z","etag":null,"topics":["caching","elixir","ets","phoenix","plug"],"latest_commit_sha":null,"homepage":"","language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/andreapavoni.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-04-19T13:30:18.000Z","updated_at":"2024-03-14T10:58:35.000Z","dependencies_parsed_at":"2022-09-26T18:21:38.962Z","dependency_job_id":null,"html_url":"https://github.com/andreapavoni/plug_ets_cache","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/andreapavoni/plug_ets_cache","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andreapavoni%2Fplug_ets_cache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andreapavoni%2Fplug_ets_cache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andreapavoni%2Fplug_ets_cache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andreapavoni%2Fplug_ets_cache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/andreapavoni","download_url":"https://codeload.github.com/andreapavoni/plug_ets_cache/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andreapavoni%2Fplug_ets_cache/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":271754937,"owners_count":24815323,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-23T02:00:09.327Z","response_time":69,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["caching","elixir","ets","phoenix","plug"],"created_at":"2024-11-01T13:05:19.479Z","updated_at":"2025-08-23T15:39:20.612Z","avatar_url":"https://github.com/andreapavoni.png","language":"Elixir","readme":"# PlugEtsCache\n\n[![Build Status](https://travis-ci.org/andreapavoni/plug_ets_cache.svg?branch=master)](https://travis-ci.org/andreapavoni/plug_ets_cache)\n\nA simple http response caching system based on [Plug](https://github.com/elixir-lang/plug) and [ETS](http://erlang.org/doc/man/ets.html). It easily integrates in every application that uses Plug, including a Phoenix dedicated adapter.\n\nThe 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.\n\nCache duration can be configured with a combination of a `ttl` value and `ttl_check`. Check [con_cache](https://github.com/sasa1977/con_cache) documentation for more details on this, PlugEtsCache uses it to read/write to ETS.\n\n## Installation\n\nThe package is available in [Hex](https://hex.pm/packages/plug_ets_cache), follow these steps to install:\n\n1. Add `plug_ets_cache` to your list of dependencies in `mix.exs`:\n\n  ```elixir\n  def deps do\n    # Get from hex\n    [{:plug_ets_cache, \"~\u003e 0.3.0\"}]\n    # Or use the latest from master\n    [{:plug_ets_cache, github: \"andreapavoni/plug_ets_cache\"}]\n  end\n  ```\n\n2. Ensure `plug_ets_cache` is started before your application:\n\n  ```elixir\n  def application do\n    [applications: [:plug_ets_cache]]\n  end\n  ```\n\n## Usage\n\nThese are the common steps to setup `PlugEtsCache`:\n\n1. Set configuration in `config/config.exs` (the following values are defaults):\n\n  ```elixir\n  config :plug_ets_cache,\n    db_name: :ets_cache,\n    ttl_check: 60,\n    ttl: 300\n  ```\n\n2. Add `PlugEtsCache.Plug` to your router/plug:\n\n  ```elixir\n  plug PlugEtsCache.Plug\n  ```\n\nNow follow specific instructions below for your use case.\n\n### With Phoenix\n\nBecause Phoenix has a more complex lifecycle when it comes to send a response, it\nhas a special module for this.\n\n1. Add ` use PlugEtsCache.Phoenix`\n2. Call `cache_response` *after you've sent a response*:\n\n  ```elixir\n  defmodule MyApp.SomeController do\n    use MyApp.Web, :controller\n    use PlugEtsCache.Phoenix\n\n    # ...\n\n    def index(conn, _params) do\n      # ...\n      conn\n      |\u003e render(\"index.html\")\n      |\u003e cache_response\n    end\n\n    # ...\n  end\n  ```\n\n### With plain Plug\n\nSupposing a very simple Plug module:\n\n1. Import  `PlugEtsCache.Response.cache_response/1` inside your module\n2. Call `cache_response` *after you've sent a response*:\n\n```elixir\ndefmodule FooController do\n  use Plug.Router\n  import PlugEtsCache.Response, only: [cache_response: 1]\n\n  plug :match\n  plug :dispatch\n\n  get \"/\" do\n    Plug.Conn.fetch_query_params(conn)\n    |\u003e put_resp_content_type(\"text/plain\")\n    |\u003e send_resp(200, \"Hello cache\")\n    |\u003e cache_response\n  end\nend\n```\n\n### Setting TTL\n\n`cache_response/1` will adhere to the `ttl` value in the config, but\nyou can instead use `cache_response/2` to specify a custom `ttl` for each\nresponse. Examples:\n\n```elixir\ncache_response(conn, :timer.hours(1))\n```\n\n```elixir\ncache_response(conn, ttl: :timer.minutes(45))\n```\n\n### Using a custom cache key\n\nIf you need greater control over the key used to cache the request you can use a custom function to build the cache key.\nThe function needs to accept one argument, the `Plug.Conn` struct, and return the key.\n\n```elixir\ncache_response(conn, [cache_key: fn conn -\u003e conn.request_path end, ttl: :timer.minutes(10)])\n```\n\n## Documentation\n\nThe docs can be found at [https://hexdocs.pm/plug_ets_cache](https://hexdocs.pm/plug_ets_cache).\n\n## TODO\n\n* add more detailed docs\n\n## Contributing\n\nEveryone is welcome to contribute to PlugEtsCache and help tackling existing issues!\n\nUse the [issue tracker](https://github.com/andreapavoni/plug_ets_cache/issues) for bug reports or feature requests.\n\nPlease, do your best to follow the [Elixir's Code of Conduct](https://github.com/elixir-lang/elixir/blob/master/CODE_OF_CONDUCT.md).\n\n## License\n\nThis source code is released under MIT License. Check [LICENSE](https://github.com/andreapavoni/plug_ets_cache/blob/master/LICENSE) file for more information.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandreapavoni%2Fplug_ets_cache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fandreapavoni%2Fplug_ets_cache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandreapavoni%2Fplug_ets_cache/lists"}