[erlang-questions] how: Purging old records from Mnesia table

Gleb Peregud gleber.p@REDACTED
Fri Mar 7 15:13:27 CET 2008

On Thu, Mar 6, 2008 at 7:00 PM, Paul Mineiro <paul-trapexit@REDACTED> wrote:
>  lately i've been maintaining another mnesia table as an ordered_set with
>  the expiration time as the first element in the tuple of the primary key.
>  i then periodically iterate over that (with mnesia:first/1 and
>  mnesia:next/2), terminating the iteration early when possible, which is
>  most of the time.
>  of course it means database modifications have to hit two tables, and
>  unfortunately sometimes introduces the need for transactions where before
>  dirty operations would do.
>  so i'm eager to hear a better solution.
>  p.z. one thing not clear in your original post ... is this persistent
>  data?  cuz if so, timers and ets tables seem ill advised.

Caching server, which i'm working on, would be able to store data in
either ways. User will specify if:
1) Record will not expire (user is responsible for purging it)
2) Record will expire in arbitrary number of seconds
3) Record will expire at given date (user will have to pass unix
timestamp) (expire field =

Of course i'm not going to use any timers for data which will never
expire :) And, as it can be seen in timer module's source, it is
implemented in exacly the same way as you did it in your application.
timer is using ets ordered_set table for storing ordered list of
events which are going to be launched at given time. After handling
some event it will fetch the first item from table and sleep
appropriate time, and so on.

On Fri, Mar 7, 2008 at 3:56 AM, Scott Lystig Fritchie
<fritchie@REDACTED> wrote:
>  When having the expiration even sent by a 2nd party, you must avoid race
>  conditions somehow.  Example:
>     Store tuple {Key, Value0}, expires at time T.
>     Time T arrives
>     Client stores {Key, Value1}, expires at time T2
>     Server deletes Key
>     Client attempts to fetch Key, fetch fails, client is sad.

Thanks for the hint. I will store timer's "handle" in every record to
be able to cancel it :)

Gleb Peregud

Every minute is to be grasped.
Time waits for nobody.
-- Inscription on a Zen Gong

More information about the erlang-questions mailing list