sorted_ttl_list v1.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

Backup the table to dets

delete a key from the list

Check if a table exists

Flush the content of a table

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()
element() :: {key, score, expire, data}

A response element

expire()
expire() :: Integer.t

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

key()
key() :: String.t

The element key

score()
score() :: Integer.t

The score to do the sorting

table()
table() :: String.t

The tablename

ttl()
ttl() :: Integer.t

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

Functions

backup(table)
backup(table) :: Integer.t

Backup the table to dets

Parameters

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

Examples

iex>{:ok, tid} = SortedTtlList.start_link( "test_table_backup", true )
...>SortedTtlList.push( tid, "mykey", 23, 3600, nil )
...>SortedTtlList.backup( tid )
:ok
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" )
...>{ "mykey", 23, _expire_ts, %{ some: "additional", data2: "save" } } = 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
flush(table)
flush(table) :: Integer.t

Flush the content of a table

Parameters

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

Examples

iex>{:ok, tid} = SortedTtlList.start_link( "test_table" )
...>0 = SortedTtlList.size( tid )
...>{ "mykey", 23, _expire_ts, nil } = SortedTtlList.push( tid, "mykey", 23, 3600, nil )
...>1 = SortedTtlList.size( tid )
...>SortedTtlList.flush( tid )
...>SortedTtlList.size( tid )
0
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" )
...>{ "mykey", 23, _expire_ts, %{ some: "additional", data2: "save" } } = 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" )
...>{ "mykey", 23, _expire_ts, %{ some: "additional", data2: "save" } } = 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, reverse \\ false)
list(table, Boolean.t) :: [element]

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" )
...>{ "mykeyA", 23, _expire_ts, nil } = SortedTtlList.push( tid, "mykeyA", 23, 3600, nil )
...>{ "mykeyB", 13, _expire_ts, nil } = 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( 1337 )
...>{ "mykeyA", 23, _expire_ts, nil }= SortedTtlList.push( tid, "mykeyA", 23, 3600, nil )
...>{ "mykeyB", 13, _expire_ts, nil }= SortedTtlList.push( tid, "mykeyB", 13, 3600, nil )
...>[ { "mykeyA", 23, _tsB, nil }, { "mykeyB", 13, _tsA, nil } ] = SortedTtlList.list( tid, true )
...>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" )
...>{ "mykey", 23, _expire_ts, %{ some: "additional", data2: "save" } } = 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 )
...>{ "mykey", 23, _expire_ts, nil } = SortedTtlList.push( tid, "mykey", 23, 3600, nil )
...>SortedTtlList.size( tid )
1
start_link(tablename, persist \\ false)
start_link(String.t, boolean) :: {:ok, {pid, boolean}}

Start a table

Parameters

  • tablename (Binary|Atom) The table to create.
  • persist (Boolean, default: true) Persist this table on start/exits from/to the disk.

Examples

iex>{isok, _pid } = SortedTtlList.start_link( "hello" )
...>isok
:ok

iex>{isok, _pid } = SortedTtlList.start_link( "helloPersist", true )
...>isok
:ok