ttl_map (util v1.3.5)

View Source

Map with TTL key/value eviction.

An insert of a Key/Value pair in the map will store the timestamp of the maybe_add. Additionally a queue of maybe_adds is maintained by this container, which is checked on each insert and the expired Key/Value pairs are evicted from the map.

Author: Serge Aleynikov <saleyn at gmail dot com>

Summary

Functions

Evict stale items from the map given the current timestamp Now.

Evict stale items (up to the Limit) from the map given the current timestamp Now.

Create a new map with a given TTL time for inserted items

Create a new map with a given TTL time for inserted items. Opts is a list of options

Get the current timestamp in microseconds since Unix epoch.

Get the number of items in the map.

Try to add a Key/Value pair to the map. If more than TTL time elapsed since the last insert of the Key or the Key is not found in the map, the value is inserted, otherwise no insertion is made.

Types

ttl_map()

-type ttl_map() :: #ttl_map{ets :: term(), q :: term(), ttl :: term()}.

Functions

evict/2

-spec evict(ttl_map(), non_neg_integer()) -> ttl_map().

Evict stale items from the map given the current timestamp Now.

evict/3

-spec evict(ttl_map(), non_neg_integer(), non_neg_integer()) -> ttl_map().

Evict stale items (up to the Limit) from the map given the current timestamp Now.

new(TTL)

Create a new map with a given TTL time for inserted items

new(TTL, Opts)

Create a new map with a given TTL time for inserted items. Opts is a list of options:

  • name — The name of the ETS table (defaults to undefined)
  • access — The access level of the ETS table (defaults to private)

now()

-spec now() -> non_neg_integer().

Get the current timestamp in microseconds since Unix epoch.

size/1

-spec size(ttl_map()) -> non_neg_integer().

Get the number of items in the map.

try_add/4

-spec try_add(ttl_map(), any(), any(), non_neg_integer()) -> {ttl_map(), Inserted :: boolean()}.

Try to add a Key/Value pair to the map. If more than TTL time elapsed since the last insert of the Key or the Key is not found in the map, the value is inserted, otherwise no insertion is made.