Created
April 14, 2025 08:37
-
-
Save Wigny/6941caf0727f566b68f382e0e8e7758e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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