From c7caeba60dd8cd2679d7cd832abc35c48090276b Mon Sep 17 00:00:00 2001 From: DualFroz Date: Thu, 27 Aug 2026 13:33:00 +0200 Subject: [PATCH] feat(persistence): add snapshot save and load --- lib/excache/persistence.ex | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 lib/excache/persistence.ex diff --git a/lib/excache/persistence.ex b/lib/excache/persistence.ex new file mode 100644 index 0000000..f4d81d7 --- /dev/null +++ b/lib/excache/persistence.ex @@ -0,0 +1,22 @@ +defmodule Excache.Persistence do + @moduledoc false + + def snapshot_path do + Application.get_env(:excache, :snapshot_path) || + Path.join(to_string(:code.priv_dir(:excache)), "excache.snapshot") + end + + def save do + path = snapshot_path() + File.mkdir_p!(Path.dirname(path)) + File.write!(path, :erlang.term_to_binary(Excache.Store.dump())) + :ok + end + + def load_entries(path) do + case File.read(path) do + {:ok, binary} -> :erlang.binary_to_term(binary) + {:error, _reason} -> [] + end + end +end