Skip to content

Instantly share code, notes, and snippets.

@Wigny
Created April 14, 2025 08:37
Show Gist options
  • Save Wigny/6941caf0727f566b68f382e0e8e7758e to your computer and use it in GitHub Desktop.
Save Wigny/6941caf0727f566b68f382e0e8e7758e to your computer and use it in GitHub Desktop.
defmodule TermStorage do
use Task
def start_link(args \\ []) do
Task.start_link(fn ->
table_name = Keyword.get(args, :table_name, __MODULE__)
{:ok, _table} = :dets.open_file(table_name, auto_save: to_timeout(second: 1))
Process.hibernate(Function, :identity, [nil])
end)
end
def get(table_name \\ __MODULE__, key) do
case :dets.lookup(table_name, key) do
[{^key, value}] -> {:ok, value}
[] -> :empty
end
end
def put(table_name \\ __MODULE__, key, value) do
:dets.insert(table_name, {key, value})
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment