Caches a value and expires it after a given TTL. Provides a callback for hooking into expiration
ttl_cache
is a simple key-value with time-based expiration. This helps to ensure that the store won't grow unbounded. It backs several of our critical in-memory data stores.
def deps do
{:ttl_cache, "~> 0.1", github: "wistia/ttl_cache"}
end
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)
TTLCache.Server
supports several refresh strategies:
:on_write
- refresh the TTL when the key is written to. Does not care whether or not the value changed:on_read
- refresh the TTL when the key is read. Note that any update (includingget_and_update
) is considered a write:on_read_write
- refresh the TTL when the key is written to or read:never
- never refresh and just remove the key after the TTL
You can pass these strategies via TTLCache.Server.start_link
:
TTLCache.Server.start_link(refresh_strategy: :on_write)