Skip to content

Commit

Permalink
Change default ets table type to set, and make configurable (#38)
Browse files Browse the repository at this point in the history
* Change default ets table type to `set`, and make configurable

* Document the `:ets_table_type` option

* Add link to mnesia backend

---------

Co-authored-by: Shane Kilkelly <[email protected]>
Co-authored-by: epinault <[email protected]>
  • Loading branch information
3 people authored Jan 9, 2024
1 parent 77d02da commit f4a6431
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/hammer/backend/ets.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ defmodule Hammer.Backend.ETS do
- `expiry_ms`: (integer) time in ms before a bucket is auto-deleted,
should be larger than the expected largest size/duration of a bucket
- `cleanup_interval_ms`: (integer) time between cleanup runs,
- `ets_table_type`: (atom) type of ETS table, defaults to `:set`, and can be
either `:set` or `:ordered_set`
Example:
Expand Down Expand Up @@ -160,6 +162,7 @@ defmodule Hammer.Backend.ETS do
def init(args) do
cleanup_interval_ms = Keyword.get(args, :cleanup_interval_ms)
expiry_ms = Keyword.get(args, :expiry_ms)
ets_table_type = Keyword.get(args, :ets_table_type, :set)

if !expiry_ms do
raise RuntimeError, "Missing required config: expiry_ms"
Expand All @@ -169,9 +172,13 @@ defmodule Hammer.Backend.ETS do
raise RuntimeError, "Missing required config: cleanup_interval_ms"
end

if ets_table_type not in [:set, :ordered_set] do
raise RuntimeError, "Invalid config: ets_table_type '#{ets_table_type}'"
end

case :ets.info(@ets_table_name) do
:undefined ->
:ets.new(@ets_table_name, [:named_table, :ordered_set, :public])
:ets.new(@ets_table_name, [:named_table, ets_table_type, :public])
:timer.send_interval(cleanup_interval_ms, :prune)

_ ->
Expand Down

0 comments on commit f4a6431

Please sign in to comment.