Skip to content
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commit b120cfa

Browse files
committed
refactor(cache): workflow for community contributes list/add
1 parent 1756192 commit b120cfa

File tree

4 files changed

+60
-16
lines changed

4 files changed

+60
-16
lines changed

lib/groupher_server/statistics/delegates/contribute.ex

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,42 @@ defmodule GroupherServer.Statistics.Delegate.Contribute do
1313
@user_contribute_months get_config(:general, :user_contribute_months)
1414

1515
@doc """
16-
update community's contributes record
16+
update user's contributes record
1717
"""
18-
def make_contribute(%Community{id: id}) do
18+
def make_contribute(%User{id: id}) do
1919
today = Timex.today() |> Date.to_iso8601()
2020

21-
case ORM.find_by(CommunityContribute, community_id: id, date: today) do
21+
case ORM.find_by(UserContribute, user_id: id, date: today) do
2222
{:ok, contribute} ->
23-
contribute |> inc_contribute_count(:community) |> done
23+
contribute |> inc_contribute_count(:user) |> done
2424

2525
{:error, _} ->
26-
CommunityContribute |> ORM.create(%{community_id: id, date: today, count: 1})
26+
UserContribute |> ORM.create(%{user_id: id, date: today, count: 1})
2727
end
2828
end
2929

3030
@doc """
31-
update user's contributes record
31+
update community's contributes record
3232
"""
33-
def make_contribute(%User{id: id}) do
33+
def make_contribute(%Community{id: id}) do
3434
today = Timex.today() |> Date.to_iso8601()
3535

36-
case ORM.find_by(UserContribute, user_id: id, date: today) do
36+
case ORM.find_by(CommunityContribute, community_id: id, date: today) do
3737
{:ok, contribute} ->
38-
contribute |> inc_contribute_count(:user) |> done
38+
result = contribute |> inc_contribute_count(:community) |> done
39+
40+
# TODO: move to background job
41+
list_contributes_digest(%Community{id: id})
42+
result
3943

4044
{:error, _} ->
41-
UserContribute |> ORM.create(%{user_id: id, date: today, count: 1})
45+
result =
46+
CommunityContribute
47+
|> ORM.create(%{community_id: id, date: today, count: 1})
48+
49+
# TODO: move to background job
50+
list_contributes_digest(%Community{id: id})
51+
result
4252
end
4353
end
4454

@@ -61,7 +71,7 @@ defmodule GroupherServer.Statistics.Delegate.Contribute do
6171
Returns the list of community_contribute by latest 6 days.
6272
"""
6373
def list_contributes_digest(%Community{id: id}) do
64-
scope = "community.contributes.#{id}"
74+
scope = Cache.get_scope(:community_contributes, id)
6575

6676
case Cache.get(scope) do
6777
{:ok, result} ->
@@ -76,12 +86,13 @@ defmodule GroupherServer.Statistics.Delegate.Contribute do
7686
end
7787
end
7888

89+
# TODO: mv to helper/cache, also 规范一下 scope
7990
defp cache_result({:ok, result}, scope) do
80-
IO.inspect scope, label: "put cashe"
8191
Cache.put(scope, result)
8292
{:ok, result}
8393
end
8494

95+
# TODO: mv to helper/cache, also 规范一下 scope
8596
defp cache_result({:error, result}, _scope) do
8697
{:error, result}
8798
end

lib/helper/cache.ex

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@ defmodule Helper.Cache do
99
{:ok, "b"}
1010
"""
1111
def get(cache_key) do
12-
Cachex.get(:site_cache, cache_key)
12+
case Cachex.get(:site_cache, cache_key) do
13+
{:ok, nil} ->
14+
{:error, nil}
15+
16+
{:ok, result} ->
17+
{:ok, result}
18+
end
1319
end
1420

1521
@doc """
@@ -20,4 +26,9 @@ defmodule Helper.Cache do
2026
def put(cache_key, cache_value) do
2127
Cachex.put(:site_cache, cache_key, cache_value)
2228
end
29+
30+
@doc """
31+
cache scope of community contributes digest
32+
"""
33+
def get_scope(:community_contributes, id), do: "community.contributes.#{id}"
2334
end

test/groupher_server/statistics/statistics_test.exs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ defmodule GroupherServer.Test.Statistics do
121121
assert second.count == 2
122122
end
123123

124+
@tag :wip
124125
test "should return recent #{@community_contribute_days} days community contributes by default",
125126
~m(community)a do
126127
seven_days_ago = Timex.shift(Timex.today(), days: -@community_contribute_days)
@@ -146,5 +147,26 @@ defmodule GroupherServer.Test.Statistics do
146147
{:ok, contributes} = Statistics.list_contributes_digest(%Community{id: community.id})
147148
assert length(contributes) == @community_contribute_days + 1
148149
end
150+
151+
alias Helper.Cache
152+
@tag :wip
153+
test "the contributes data should be cached after first query", ~m(community)a do
154+
scope = Cache.get_scope(:community_contributes, community.id)
155+
assert {:error, nil} = Cache.get(scope)
156+
157+
{:ok, contributes} = Statistics.list_contributes_digest(%Community{id: community.id})
158+
159+
assert {:ok, contributes} = Cache.get(scope)
160+
end
161+
162+
@tag :wip
163+
test "cache should be update after make contributes", ~m(community)a do
164+
scope = Cache.get_scope(:community_contributes, community.id)
165+
assert {:error, nil} = Cache.get(scope)
166+
167+
Statistics.make_contribute(%Community{id: community.id})
168+
169+
assert {:ok, _} = Cache.get(scope)
170+
end
149171
end
150172
end

test/helper/cache_test.exs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ defmodule GroupherServer.Test.Helper.Cache do
77
describe "[cache test]" do
88
@tag :wip
99
test "cache get unexsit key should get nil" do
10-
assert {:ok, nil} = Cache.get("no exsit")
11-
assert {:ok, nil} = Cache.get(:no_exsit)
10+
assert {:error, nil} = Cache.get("no exsit")
11+
assert {:error, nil} = Cache.get(:no_exsit)
1212
end
1313

1414
@tag :wip
1515
test "cache put should work" do
16-
assert {:ok, nil} = Cache.get(:data)
16+
assert {:error, nil} = Cache.get(:data)
1717

1818
assert {:ok, true} = Cache.put(:data, "value")
1919
assert {:ok, "value"} = Cache.get(:data)

0 commit comments

Comments
 (0)