Skip to content

Instantly share code, notes, and snippets.

@rugyoga
Created September 6, 2022 17:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rugyoga/3fde53ebdf527a937061c1e5b2e93ddf to your computer and use it in GitHub Desktop.
Save rugyoga/3fde53ebdf527a937061c1e5b2e93ddf to your computer and use it in GitHub Desktop.
Agent that performs file cacheing
defmodule Audit.FileCache do
@moduledoc """
Simple File cache Agent
"""
use Agent
def start_link(_args) do
Agent.start_link(fn -> %{} end, name: __MODULE__)
end
def get(filename) do
Agent.get_and_update(
__MODULE__,
fn state ->
if cached = Map.get(state, filename) do
{cached, state}
else
file = File.stream!(filename)
{file, Map.put(state, filename, file)}
end
end
)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment