-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathcache.ex
More file actions
40 lines (34 loc) · 813 Bytes
/
Copy pathcache.ex
File metadata and controls
40 lines (34 loc) · 813 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
defmodule Survey.Cache do
use Survey.Web, :model
alias Survey.Repo
import Ecto.Query
require Ecto.Query
alias Survey.Cache
schema "cache" do
field :blob, Survey.Term
end
def store(blob) do
{:ok, id} = Repo.transaction(fn ->
blobterm = :erlang.term_to_binary(blob)
case from(f in Cache, where: f.blob == ^blobterm, limit: 1) |> Repo.all do
%{id: id} -> id
nil -> insert(blob)
[] -> insert(blob)
end
end)
id
end
defp insert(blob) do
%{id: id} = %Survey.Cache{blob: blob} |> Repo.insert!
id
end
def delete(id) do
Survey.Repo.delete_all(from f in Survey.Cache, where: f.id == ^id)
end
def get(id) do
case Repo.get(Survey.Cache, id) do
%{blob: blob} -> blob
nil -> nil
end
end
end