I have an ets set table in an elixir app. I need to clean-up records which have their updated_at older than 10 seconds. is there a way I can set expirey or do manually without iterating over all the records(match records based on the timestamps greater than given time)
def clean_stale(previous_key) do
if previous_key == :"$end_of_table" do
device = get(previous_key)
next_key = :ets.next(__MODULE__, previous_key)
if NaiveDateTime.diff(NaiveDateTime.utc_now, device.last_recorded_at) > 10 do
It would still iterate over the table, but unlike your current approach it wouldn’t copy any data from it. To make things easier, you might add an additional “column” to the table with updated_at as a timestamp for a simpler match statement.
How about if I make updated_at a unix timestamp. is there any method which will delete all records having updated_at greater than given timestamp? I mean this approach seems easy. I am not sure how pattern match would work though
Seems like you only have two columns in your ets table, a key and the corresponding struct. And the pattern [:_, :_, :"$1", :_, :_, :_] expects 6. I’d add a third column with the timestamp, and use [:_, :_, :"$1"]. Also note that '_' in erlang is an atom :_ in elixir (I’ve updated my response above).
I thought fun2ms relied on an Erlang macro? It works in iex but requires modified compilation to work outside the console. That’s why the project Nicd linked above exists (https://github.com/ericmj/ex2ms), right?