-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsubscription_storage_cache.ex
More file actions
79 lines (62 loc) · 2.18 KB
/
Copy pathsubscription_storage_cache.ex
File metadata and controls
79 lines (62 loc) · 2.18 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
defmodule DeerCache.SubscriptionStorageCache do
use GenServer
alias Phoenix.PubSub
def start_link(opts \\ []),
do:
GenServer.start_link(
__MODULE__,
[{:ets_table_name, :deer_files_disk_usage_by_subscription_id}],
opts
)
def fetch_data(subscription_id) do
case GenServer.call(__MODULE__, {:get, subscription_id}) do
[] -> {0, 0}
[{_subscription_id, data}] -> data
end
end
def handle_call({:get, subscription_id}, _from, state) do
{:reply, get(subscription_id, state), state}
end
def handle_cast({:uploaded_file, subscription_id, file_size_in_kilobytes}, state) do
new_data =
case get(subscription_id, state) do
[] ->
{1, file_size_in_kilobytes}
[{^subscription_id, {files, kilobytes}}] ->
{files + 1, kilobytes + file_size_in_kilobytes}
end
set(subscription_id, new_data, state)
PubSub.broadcast(
DeerStorage.PubSub,
"subscription_deer_storage:#{subscription_id}",
{:cached_deer_storage_changed, new_data}
)
{:noreply, state}
end
def handle_cast({:removed_files, subscription_id, count, file_size_in_kilobytes}, state) do
[{^subscription_id, {files, kilobytes}}] = get(subscription_id, state)
new_data = {files - count, kilobytes - file_size_in_kilobytes}
set(subscription_id, new_data, state)
PubSub.broadcast(
DeerStorage.PubSub,
"subscription_deer_storage:#{subscription_id}",
{:cached_deer_storage_changed, new_data}
)
{:noreply, state}
end
def init(args) do
[{:ets_table_name, ets_table_name}] = args
:ets.new(ets_table_name, [:named_table, :set, :private])
grouped_data = DeerStorage.Services.CalculateDeerStorage.run!()
state = %{ets_table_name: ets_table_name}
for {subscription_id, {files, kilobytes}} <- grouped_data,
do: set(subscription_id, {files, kilobytes}, state)
{:ok, state}
end
defp set(subscription_id, data, %{ets_table_name: ets_table_name}) do
true = :ets.insert(ets_table_name, {subscription_id, data})
end
defp get(subscription_id, %{ets_table_name: ets_table_name}) do
:ets.lookup(ets_table_name, subscription_id)
end
end