TTLCache

Caches a value and expires it after a given TTL. Provides a callback for hooking into expiration

Installation

def deps do
  {:ttl_cache, "~> 0.1", github: "wistia/ttl_cache"}
end

Usage

See TTLCache.Server for the latest documentation

{:ok, pid} = TTLCache.Server.start_link(ttl: 5_000, on_expire: fn {key, _val} -> IO.inspect("#{key} expired") end)
:ok = TTLCache.Server.put(pid, :hello, "world")
{:ok, "world"} = TTLCache.Server.get(pid, :hello)

:timer.sleep(5_000)
# should log

{:ok, nil} = TTLCache.Server.get(pid, :hello)

Refresh Strategies

TTLCache.Server supports several refresh strategies:

You can pass these strategies via TTLCache.Server.start_link:

TTLCache.Server.start_link(refresh_strategy: :on_write)