# CompilerCache

LRU cache for compiling expressions into functions.

Using `CompilerCache`, it is easy to create custom expressions that perform well.

Instead of relying on using `Code.eval_quoted`, the CompilerCache
system compiles given ASTs into modules.

The compilation cache has a fixed maximum size, and uses a fixed pool
of module names as not to exhaust the BEAM vm's atom table.

Creating a compilation cache is as simple as `use CompilerCache` and
implementing the `create_ast/1` function:

defmodule MyExpressionCache do
  use CompilerCache
  def create_ast(expr) do
    {:ok, ast} = Code.string_to_quoted(expr)
    {ast, []}

The `create_ast/2` function must return an `{ast, opts}` tuple. The opts are the same as those given to [Code.eval_quoted/3](

This cache can then be called like this:

{:ok, _} = MyExpressionCache.start_link()
iex> MyExpressionCache.execute("1 + 1", nil)
iex> MyExpressionCache.execute("2 * input", 3)

The input argument is always called 'input'. There is just one input
argument - use a tuple and pattern matching to use multiple arguments.

After *N* cache misses (default: 2), expressions are cached into a
module on the background by the compiler process. This speeds up
consecutive executions considerable (~10x speedups are not unheard

Cached expressions (modules) are unloaded when they have not been used
after *max_ttl* milliseconds (default: 1000).

The number of cached expressions can be

## Installation

If [available in Hex](, the package can be installed as:

  1. Add `compiler_cache` to your list of dependencies in `mix.exs`:

    def deps do
      [{:compiler_cache, "~> 1.0"}]

  2. Ensure `compiler_cache` is started before your application:

    def application do
      [applications: [:compiler_cache]]

  3. Implement your compiler by using `use CompilerCache` and
     implementing `create_ast/1`, then put it in your supervisor tree:

     worker(MyExpressionCache, [])