sorted_ttl_list v0.1.2 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
Any aditional data to store with this key
The response element will not show the ttl. It will transform it to a timestamp until it is alive.
Functions
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
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 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 the element in the table sorted by score
Parameters
table
(Binary|Atom) The table to list the elements.reverse
(Boolean) Sort in reverse order vom high to low.
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
iex>{:ok, tid} = SortedTtlList.start_link( "test_table" )
...>:ok = SortedTtlList.push( tid, "mykeyA", 23, 3600, nil )
...>:ok = SortedTtlList.push( tid, "mykeyB", 13, 3600, nil )
...>[ { "mykeyA", 23, _tsB, nil }, { "mykeyB", 13, _tsA, nil } ] = SortedTtlList.list( tid, true )
...>SortedTtlList.size( tid )
2
Push a element to the list
Parameters
table
(Binary|Atom) The table name. Make sure to start the table beforekey
(Binary) The key used to save this elementscore
(Integer) The score to sort the list.ttl
(Integer) The time to live in seconds for this keydata
(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
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