News for hackers. Stuff that matters.
Re:Building a Cache in Elixir by losvedir on Tuesday July 12, 2022 @07:03AM (#32068137)
The purging is a scan with `next()` and a `lookup()` for each item. Since the logic is a simple `<` comparison, could this be done with a single `match_delete` instead? (Unfortunately, ETS match specifications are quite wonky, so without a lot of fiddling myself, I can't suggest exactly what the comparison logic would look like, but I know that you can express certain things like comparisons with it.) That would cut down on the back-and-forth between the elixir process and the ETS process.
Re:Building a Cache in Elixir by notamy on Tuesday July 12, 2022 @08:17AM (#32068758)
> Unfortunately, ETS match specifications are quite wonky

Matchspecs are gnarly! Very annoying to wrap your head around, and it's closer to being an AST than anything.

Shameless plug: I had written a library at one point to make matchspecs with Mnesia easier, but it might also work with ETS (haven't tested unfortunately): https://github.com/queer/lethe

Re:Building a Cache in Elixir by bluesnowmonkey on Tuesday July 12, 2022 @03:01PM (#32074303)
Yes, match_delete would work. Even a little better, select_delete will return the number of entries that were purged. To delete everything where the third element in a tuple is greater than a constant, should be something like:

  :ets.select_delete(table, [{{:_, :_, :"$1"}, [], [{:>, :"$1", {:const, expires}}]}])
Re:Building a Cache in Elixir by asabil on Tuesday July 12, 2022 @08:23AM (#32068803)
That's what `ets:fun2ms` at least in Erlang. Not sure how does that map in Elixir?