View Source CacheMe (Cache Me v0.0.6)

CacheMe

Cache functions without hassle.

quick-start

Quick Start

Add cache_me to your list of dependencies in mix.exs:

{:cache_me, "~> 0.0.1"}

Then add use CacheMe to any module using caching and @cache true to any function you would like to cache.

Use CacheMe.uncached(<module>, <function>, <args>) to skip the cache. The syntax is the same as apply/3.

example

Example

defmodule Example do
  use CacheMe
  require Logger

  @url_env "MY_AUTH_URL"
  @url_default "https://example.com/"

  @cache true
  @spec url :: URI.t()
  def url do
    url = System.get_env(@url_env)

    if is_binary(url) do
      URI.parse(url)
    else
      Logger.warn("`#{@url_env}` not set, defaulting to: '#{@url_default}'.")
      URI.parse(@url_default)
    end
  end
end

Use:

iex(1)> Example.url

16:30:03.556 [warning] `MY_AUTH_URL` not set, defaulting to: 'https://example.com/'.
%URI{
  authority: "example.com",
  fragment: nil,
  host: "example.com",
  path: "/",
  port: 443,
  query: nil,
  scheme: "https",
  userinfo: nil
}
iex(2)> Example.url
%URI{
  authority: "example.com",
  fragment: nil,
  host: "example.com",
  path: "/",
  port: 443,
  query: nil,
  scheme: "https",
  userinfo: nil
}

The Example.url/0 logic is only executed once.

It is possible to skip the cache using CacheMe.uncached(<module>, <function>, <args>). In this case CacheMe.uncached(Example, :url, []).

Continuing from above:

iex(3)> CacheMe.uncached(Example, :url, [])

16:30:17.690 [warning] `MY_AUTH_URL` not set, defaulting to: 'https://example.com/'.
%URI{
  authority: "example.com",
  fragment: nil,
  host: "example.com",
  path: "/",
  port: 443,
  query: nil,
  scheme: "https",
  userinfo: nil
}

changelog

Changelog

0-0-5-2022-05-18

0.0.5 (2022-05-18)

Protect against including (use CacheMe) multiple times inside the same module.

0-0-4-2022-05-18

0.0.4 (2022-05-18)

Remove defdelegate from compile logic to better support defp.

0-0-3-2022-05-16

0.0.3 (2022-05-16)

Relax Elixir constraints.

0-0-2-2022-05-16

0.0.2 (2022-05-16)

Add missing :crypto to extra_applications.

0-0-1-2022-05-12

0.0.1 (2022-05-12)

First basic release. Hex

Link to this section Summary

Functions

CacheMe

Cache functions without hassle.

Execute an uncached call to the cached function.

Link to this section Functions

Link to this macro

__using__(opts \\ [])

View Source (macro)

CacheMe

Cache functions without hassle.

quick-start

Quick Start

Add cache_me to your list of dependencies in mix.exs:

{:cache_me, "~> 0.0.1"}

Then add use CacheMe to any module using caching and @cache true to any function you would like to cache.

Use CacheMe.uncached(<module>, <function>, <args>) to skip the cache. The syntax is the same as apply/3.

example

Example

defmodule Example do
  use CacheMe
  require Logger

  @url_env "MY_AUTH_URL"
  @url_default "https://example.com/"

  @cache true
  @spec url :: URI.t()
  def url do
    url = System.get_env(@url_env)

    if is_binary(url) do
      URI.parse(url)
    else
      Logger.warn("`#{@url_env}` not set, defaulting to: '#{@url_default}'.")
      URI.parse(@url_default)
    end
  end
end

Use:

iex(1)> Example.url

16:30:03.556 [warning] `MY_AUTH_URL` not set, defaulting to: 'https://example.com/'.
%URI{
  authority: "example.com",
  fragment: nil,
  host: "example.com",
  path: "/",
  port: 443,
  query: nil,
  scheme: "https",
  userinfo: nil
}
iex(2)> Example.url
%URI{
  authority: "example.com",
  fragment: nil,
  host: "example.com",
  path: "/",
  port: 443,
  query: nil,
  scheme: "https",
  userinfo: nil
}

The Example.url/0 logic is only executed once.

It is possible to skip the cache using CacheMe.uncached(<module>, <function>, <args>). In this case CacheMe.uncached(Example, :url, []).

Continuing from above:

iex(3)> CacheMe.uncached(Example, :url, [])

16:30:17.690 [warning] `MY_AUTH_URL` not set, defaulting to: 'https://example.com/'.
%URI{
  authority: "example.com",
  fragment: nil,
  host: "example.com",
  path: "/",
  port: 443,
  query: nil,
  scheme: "https",
  userinfo: nil
}

changelog

Changelog

0-0-5-2022-05-18

0.0.5 (2022-05-18)

Protect against including (use CacheMe) multiple times inside the same module.

0-0-4-2022-05-18

0.0.4 (2022-05-18)

Remove defdelegate from compile logic to better support defp.

0-0-3-2022-05-16

0.0.3 (2022-05-16)

Relax Elixir constraints.

0-0-2-2022-05-16

0.0.2 (2022-05-16)

Add missing :crypto to extra_applications.

0-0-1-2022-05-12

0.0.1 (2022-05-12)

First basic release. Hex

Link to this function

uncached(module, function, arguments)

View Source
@spec uncached(module :: module(), function :: atom(), args :: [any()]) :: any()

Execute an uncached call to the cached function.

example

Example

iex> CacheMe.uncached(MyExample, :url, [])
"https://example.com"