feat(persistence): add snapshot save and load
This commit is contained in:
1 parent
316413b90e
commit
c7caeba60d
1 file changed
+22
@@ -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
|
||||
Reference in new issue
Block a user