sorted_ttl_list v0.1.0 SortedTtlList

Generate a sorted lists with a ttl for every element in the list.

Summary

Types

Any aditional data to store with this key

A response element

The response element will not show the ttl. It will transform it to a timestamp until it is alive

The element key

The score to do the sorting

The tablename

The time to live used by the .push/5 method

Functions

delete a key from the list

Check if a table exists

get a single key

list the element in the table sorted by score

Push a element to the list

Get the size of the table

Types

data()
data :: any

Any aditional data to store with this key

element()

A response element

expire()

The response element will not show the ttl. It will transform it to a timestamp until it is alive.

key()

The element key

score()

The score to do the sorting

table()

The tablename

ttl()

The time to live used by the .push/5 method.

Functions

delete(table, key)
delete(table, key) :: :ok

delete a key from the list

Parameters

  • table (Binary|Atom) The table name.
  • key (Binary) The key used to save this element

Examples

iex>{:ok, _tid} = SortedTtlList.start_link( "test_table" )
...>:ok = SortedTtlList.push( "test_table", "mykey", 23, 3600, %{ some: "additional", data2: "save" } )
...>:ok = SortedTtlList.delete( "test_table", "mykey" )
...>SortedTtlList.size( "test_table" )
0
exists(table)
exists(table) :: Boolean.t

Check if a table exists

Parameters

  • table (Binary|Atom) The table to check.

Examples

iex>{:ok, _tid} = SortedTtlList.start_link( "hello" )
...>SortedTtlList.exists( "hello" )
true

iex>SortedTtlList.exists( "nobody_here" )
false
get(table, key)
get(table, key) :: element | nil

get a single key

Parameters

  • table (Binary|Atom) The table name.
  • key (Binary) The key to get

Examples

iex>{:ok, _tid} = SortedTtlList.start_link( "test_table" )
...>:ok = SortedTtlList.push( "test_table", "mykey", 23, 3600, %{ some: "additional", data2: "save" } )
...>{ "mykey", 23, _expire_timestamp, %{ some: "additional", data2: "save" } } = SortedTtlList.get( "test_table", "mykey" )
...>SortedTtlList.size( "test_table" )
1

iex>{:ok, _tid} = SortedTtlList.start_link( "test_table" )
...>:ok = SortedTtlList.push( "test_table", "mykey", 23, 2, %{ some: "additional", data2: "save" } )
...>{ "mykey", 23, _expire_timestamp, %{ some: "additional", data2: "save" } } = SortedTtlList.get( "test_table", "mykey" )
...>:timer.sleep( 2000 )
...>nil = SortedTtlList.get( "test_table", "mykey" )
...>SortedTtlList.size( "test_table" )
0
list(table)
list(table) :: [element]

list the element in the table sorted by score

Parameters

  • table (Binary|Atom) The table to list the elements.

Examples

iex>{:ok, tid} = SortedTtlList.start_link( "test_table" )
...>:ok = SortedTtlList.push( tid, "mykeyA", 23, 3600, nil )
...>:ok = SortedTtlList.push( tid, "mykeyB", 13, 3600, nil )
...>[ { "mykeyB", 13, _tsA, nil }, { "mykeyA", 23, _tsB, nil } ] = SortedTtlList.list( tid )
...>SortedTtlList.size( tid )
2
push(table, key, score, ttl, data \\ nil)
push(table, key, score, ttl, data) :: :ok

Push a element to the list

Parameters

  • table (Binary|Atom) The table name. Make sure to start the table before
  • key (Binary) The key used to save this element
  • score (Integer) The score to sort the list.
  • ttl (Integer) The time to live in seconds for this key
  • data (Map) optional Additional data that belongs to this key

Examples

iex>{:ok, tid} = SortedTtlList.start_link( "test_table" )
...>:ok = SortedTtlList.push( tid, "mykey", 23, 3600, %{ some: "additional", data2: "save" } )
...>SortedTtlList.size( tid )
1
size(table)
size(table) :: Integer.t

Get the size of the table

Parameters

  • table (Binary|Atom) The table to list the elements.

Examples

iex>{:ok, tid} = SortedTtlList.start_link( "test_table" )
...>0 = SortedTtlList.size( tid )
...>:ok = SortedTtlList.push( tid, "mykey", 23, 3600, nil )
...>SortedTtlList.size( tid )
1
start_link(tablename)
start_link(table) :: {:ok, pid}