View Source CacheMe (Cache Me v0.0.1)

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.

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

23:17:20.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.

changelog

Changelog

0-0-1-2022-05-12

0.0.1 (2022-05-12)

First release for testing.

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.

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

23:17:20.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.

changelog

Changelog

0-0-1-2022-05-12

0.0.1 (2022-05-12)

First release for testing.

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"