Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commit115e70d

Browse files
authored
chore: merge pull request#169 from coderplanets/cron-job
add cron job and expire option to cache
2 parents0d28f9d +062506e commit115e70d

File tree

10 files changed

+74
-7
lines changed

10 files changed

+74
-7
lines changed

‎config/config.exs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@ config :rihanna,
8080
jobs_table_name:"background_jobs",
8181
producer_postgres_connection:{Ecto,GroupherServer.Repo}
8282

83+
# cron-like job scheduler
84+
config:groupher_server,Helper.Scheduler,
85+
jobs:[
86+
# Every midnight
87+
{"@daily",{Helper.Scheduler,:clear_all_cache,[]}}
88+
]
89+
8390
import_config"#{Mix.env()}.exs"
8491

8592
ifFile.exists?("config/#{Mix.env()}.secret.exs")do

‎lib/groupher_server/application.ex‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ defmodule GroupherServer.Application do
3232
)
3333
]
3434
]),
35+
worker(Helper.Scheduler,[]),
3536
{Rihanna.Supervisor,[postgrex:GroupherServer.Repo.config()]}
3637
]
3738

‎lib/groupher_server/statistics/delegates/contribute.ex‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ defmodule GroupherServer.Statistics.Delegate.Contribute do
8585
%Community{id:id}
8686
|>do_get_contributes()
8787
|>to_counts_digest(days:@community_contribute_days)
88-
|>done_and_cache(scope)
88+
|>done_and_cache(scope,expire:60_000)
8989
end
9090

9191
defpupdate_contribute_record(%UserContribute{}=contribute)do

‎lib/helper/cache.ex‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@ defmodule Helper.Cache do
2727
Cachex.put(:site_cache,cache_key,cache_value)
2828
end
2929

30+
defput(cache_key,cache_value,expire:expire_time)do
31+
Cachex.put(:site_cache,cache_key,cache_value)
32+
Cachex.expire(:site_cache,cache_key,expire_time)
33+
end
34+
35+
@doc"""
36+
clear all the cache
37+
## Example
38+
iex> Helper.Cache.clear()
39+
{:ok, 1}
40+
"""
41+
defclear_all(),do:Cachex.clear(:site_cache)
42+
3043
@doc"""
3144
cache scope of community contributes digest
3245
"""

‎lib/helper/scheduler.ex‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
defmoduleHelper.Schedulerdo
2+
@moduledoc"""
3+
cron-like job scheduler
4+
"""
5+
useQuantum.Scheduler,otp_app::groupher_server
6+
7+
aliasHelper.Cache
8+
9+
@doc"""
10+
clear all the cache in Cachex
11+
just in case the cache system broken
12+
"""
13+
defclear_all_cachedo
14+
Cache.clear_all()
15+
end
16+
end

‎lib/helper/utils.ex‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ defmodule Helper.Utils do
5252
# def done({:error, error}), do: {:error, error}
5353
defdone(result),do:{:ok,result}
5454

55+
defdone_and_cache(result,scope,expire:expire_time)do
56+
with{:ok,res}<-done(result)do
57+
Cache.put(scope,res,expire:expire_time)
58+
{:ok,res}
59+
end
60+
end
61+
5562
defdone_and_cache(result,scope)do
5663
with{:ok,res}<-done(result)do
5764
Cache.put(scope,res)

‎mix.exs‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,14 @@ defmodule GroupherServer.Mixfile do
9393
{:sentry,"~> 7.1"},
9494
{:recase,"~> 0.4.0"},
9595
{:nanoid,"~> 2.0.0"},
96+
# mailer
9697
{:bamboo,"1.2.0"},
98+
# mem cache
9799
{:cachex,"3.1.3"},
98-
{:rihanna,"1.3.5"}
100+
# postgres-backed job queue
101+
{:rihanna,"1.3.5"},
102+
# cron-like scheduler job
103+
{:quantum,"~> 2.3"}
99104
]
100105
end
101106

‎mix.lock‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"cowboy":{:hex,:cowboy,"2.6.3","99aa50e94e685557cad82e704457336a453d4abcb77839ad22dbe71f311fcc06",[:rebar3],[{:cowlib,"~> 2.7.3",[hex::cowlib,repo:"hexpm",optional:false]},{:ranch,"~> 1.7.1",[hex::ranch,repo:"hexpm",optional:false]}],"hexpm"},
1818
"cowlib":{:hex,:cowlib,"2.7.3","a7ffcd0917e6d50b4d5fb28e9e2085a0ceb3c97dea310505f7460ff5ed764ce9",[:rebar3],[],"hexpm"},
1919
"credo":{:hex,:credo,"1.1.0","e0c07b2fd7e2109495f582430a1bc96b2c71b7d94c59dfad120529f65f19872f",[:mix],[{:bunt,"~> 0.2.0",[hex::bunt,repo:"hexpm",optional:false]},{:jason,"~> 1.0",[hex::jason,repo:"hexpm",optional:false]}],"hexpm"},
20+
"crontab":{:hex,:crontab,"1.1.7","b9219f0bdc8678b94143655a8f229716c5810c0636a4489f98c0956137e53985",[:mix],[{:ecto,"~> 1.0 or ~> 2.0 or ~> 3.0",[hex::ecto,repo:"hexpm",optional:true]}],"hexpm"},
2021
"dataloader":{:hex,:dataloader,"1.0.6","fb724d6d3fb6acb87d27e3b32dea3a307936ad2d245faf9cf5221d1323d6a4ba",[:mix],[{:ecto,">= 0.0.0",[hex::ecto,repo:"hexpm",optional:true]}],"hexpm"},
2122
"db_connection":{:hex,:db_connection,"2.1.0","122e2f62c4906bf2e49554f1e64db5030c19229aa40935f33088e7d543aa79d0",[:mix],[{:connection,"~> 1.0.2",[hex::connection,repo:"hexpm",optional:false]}],"hexpm"},
2223
"decimal":{:hex,:decimal,"1.8.0","ca462e0d885f09a1c5a342dbd7c1dcf27ea63548c65a65e67334f4b61803822e",[:mix],[],"hexpm"},
@@ -31,6 +32,8 @@
3132
"faker":{:hex,:faker,"0.10.0","367c2ae47e7b4ac6410e1eaa880c07b5fe4194476f697d37ac1ce25f3058aae2",[:mix],[],"hexpm"},
3233
"file_system":{:hex,:file_system,"0.2.6","fd4dc3af89b9ab1dc8ccbcc214a0e60c41f34be251d9307920748a14bf41f1d3",[:mix],[],"hexpm"},
3334
"fs":{:hex,:fs,"0.9.2","ed17036c26c3f70ac49781ed9220a50c36775c6ca2cf8182d123b6566e49ec59",[:rebar],[],"hexpm"},
35+
"gen_stage":{:hex,:gen_stage,"0.14.2","6a2a578a510c5bfca8a45e6b27552f613b41cf584b58210f017088d3d17d0b14",[:mix],[],"hexpm"},
36+
"gen_state_machine":{:hex,:gen_state_machine,"2.0.5","9ac15ec6e66acac994cc442dcc2c6f9796cf380ec4b08267223014be1c728a95",[:mix],[],"hexpm"},
3437
"gettext":{:hex,:gettext,"0.17.0","abe21542c831887a2b16f4c94556db9c421ab301aee417b7c4fbde7fbdbe01ec",[:mix],[],"hexpm"},
3538
"guardian":{:hex,:guardian,"1.2.1","bdc8dd3dbf0fb7216cb6f91c11831faa1a64d39cdaed9a611e37f2413e584983",[:mix],[{:jose,"~> 1.8",[hex::jose,repo:"hexpm",optional:false]},{:phoenix,"~> 1.3",[hex::phoenix,repo:"hexpm",optional:true]},{:plug,"~> 1.3.3 or ~> 1.4",[hex::plug,repo:"hexpm",optional:true]}],"hexpm"},
3639
"hackney":{:hex,:hackney,"1.15.1","9f8f471c844b8ce395f7b6d8398139e26ddca9ebc171a8b91342ee15a19963f4",[:rebar3],[{:certifi,"2.5.1",[hex::certifi,repo:"hexpm",optional:false]},{:idna,"6.0.0",[hex::idna,repo:"hexpm",optional:false]},{:metrics,"1.0.1",[hex::metrics,repo:"hexpm",optional:false]},{:mimerl,"~>1.1",[hex::mimerl,repo:"hexpm",optional:false]},{:ssl_verify_fun,"1.1.4",[hex::ssl_verify_fun,repo:"hexpm",optional:false]}],"hexpm"},
@@ -39,6 +42,7 @@
3942
"jason":{:hex,:jason,"1.1.2","b03dedea67a99223a2eaf9f1264ce37154564de899fd3d8b9a21b1a6fd64afe7",[:mix],[{:decimal,"~> 1.0",[hex::decimal,repo:"hexpm",optional:true]}],"hexpm"},
4043
"jose":{:hex,:jose,"1.9.0","4167c5f6d06ffaebffd15cdb8da61a108445ef5e85ab8f5a7ad926fdf3ada154",[:mix,:rebar3],[{:base64url,"~> 0.0.1",[hex::base64url,repo:"hexpm",optional:false]}],"hexpm"},
4144
"jumper":{:hex,:jumper,"1.0.1","3c00542ef1a83532b72269fab9f0f0c82bf23a35e27d278bfd9ed0865cecabff",[:mix],[],"hexpm"},
45+
"libring":{:hex,:libring,"1.4.0","41246ba2f3fbc76b3971f6bce83119dfec1eee17e977a48d8a9cfaaf58c2a8d6",[:mix],[],"hexpm"},
4246
"metrics":{:hex,:metrics,"1.0.1","25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486",[:rebar3],[],"hexpm"},
4347
"mime":{:hex,:mime,"1.3.1","30ce04ab3175b6ad0bdce0035cba77bba68b813d523d1aac73d9781b4d193cf8",[:mix],[],"hexpm"},
4448
"mimerl":{:hex,:mimerl,"1.2.0","67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3",[:rebar3],[],"hexpm"},
@@ -57,6 +61,7 @@
5761
"poolboy":{:hex,:poolboy,"1.5.1","6b46163901cfd0a1b43d692657ed9d7e599853b3b21b95ae5ae0a777cf9b6ca8",[:rebar],[],"hexpm"},
5862
"postgrex":{:hex,:postgrex,"0.14.3","5754dee2fdf6e9e508cbf49ab138df964278700b764177e8f3871e658b345a1e",[:mix],[{:connection,"~> 1.0",[hex::connection,repo:"hexpm",optional:false]},{:db_connection,"~> 2.0",[hex::db_connection,repo:"hexpm",optional:false]},{:decimal,"~> 1.5",[hex::decimal,repo:"hexpm",optional:false]},{:jason,"~> 1.0",[hex::jason,repo:"hexpm",optional:true]}],"hexpm"},
5963
"pre_commit":{:hex,:pre_commit,"0.3.4","e2850f80be8090d50ad8019ef2426039307ff5dfbe70c736ad0d4d401facf304",[:mix],[],"hexpm"},
64+
"quantum":{:hex,:quantum,"2.3.4","72a0e8855e2adc101459eac8454787cb74ab4169de6ca50f670e72142d4960e9",[:mix],[{:calendar,"~> 0.17",[hex::calendar,repo:"hexpm",optional:true]},{:crontab,"~> 1.1",[hex::crontab,repo:"hexpm",optional:false]},{:gen_stage,"~> 0.12",[hex::gen_stage,repo:"hexpm",optional:false]},{:swarm,"~> 3.3",[hex::swarm,repo:"hexpm",optional:false]},{:timex,"~> 3.1",[hex::timex,repo:"hexpm",optional:true]}],"hexpm"},
6065
"ranch":{:hex,:ranch,"1.7.1","6b1fab51b49196860b733a49c07604465a47bdb78aa10c1c16a3d199f7f8c881",[:rebar3],[],"hexpm"},
6166
"recase":{:hex,:recase,"0.4.0","8fb52846f75948156385af2dfdc12f69e5ce27b022a9d1682c70a2fb3ed149c7",[:mix],[],"hexpm"},
6267
"rihanna":{:hex,:rihanna,"1.3.5","5f5e6c5b1e514978a29a6791f338f4bb963401959fc212bd18d4a2c92d79a7a4",[:mix],[{:ecto,">= 2.0.0",[hex::ecto,repo:"hexpm",optional:true]},{:ecto_sql,">= 3.0.0",[hex::ecto_sql,repo:"hexpm",optional:true]},{:postgrex,">= 0.13.3",[hex::postgrex,repo:"hexpm",optional:false]}],"hexpm"},
@@ -66,6 +71,7 @@
6671
"short_maps":{:hex,:short_maps,"0.1.2","a7c2bfd91179cdbdfe90e74a023992335d116982fa672612c74776b2e9257a7b",[:mix],[],"hexpm"},
6772
"sleeplocks":{:hex,:sleeplocks,"1.1.1","3d462a0639a6ef36cc75d6038b7393ae537ab394641beb59830a1b8271faeed3",[:rebar3],[],"hexpm"},
6873
"ssl_verify_fun":{:hex,:ssl_verify_fun,"1.1.4","f0eafff810d2041e93f915ef59899c923f4568f4585904d010387ed74988e77b",[:make,:mix,:rebar3],[],"hexpm"},
74+
"swarm":{:hex,:swarm,"3.4.0","64f8b30055d74640d2186c66354b33b999438692a91be275bb89cdc7e401f448",[:mix],[{:gen_state_machine,"~> 2.0",[hex::gen_state_machine,repo:"hexpm",optional:false]},{:libring,"~> 1.0",[hex::libring,repo:"hexpm",optional:false]}],"hexpm"},
6975
"telemetry":{:hex,:telemetry,"0.4.0","8339bee3fa8b91cb84d14c2935f8ecf399ccd87301ad6da6b71c09553834b2ab",[:rebar3],[],"hexpm"},
7076
"tesla":{:hex,:tesla,"0.10.0","e588c7e7f1c0866c81eeed5c38f02a4a94d6309eede336c1e6ca08b0a95abd3f",[:mix],[{:exjsx,">= 0.1.0",[hex::exjsx,repo:"hexpm",optional:true]},{:fuse,"~> 2.4",[hex::fuse,repo:"hexpm",optional:true]},{:hackney,"~> 1.6",[hex::hackney,repo:"hexpm",optional:true]},{:ibrowse,"~> 4.2",[hex::ibrowse,repo:"hexpm",optional:true]},{:mime,"~> 1.0",[hex::mime,repo:"hexpm",optional:false]},{:poison,">= 1.0.0",[hex::poison,repo:"hexpm",optional:true]}],"hexpm"},
7177
"timex":{:hex,:timex,"3.6.1","efdf56d0e67a6b956cc57774353b0329c8ab7726766a11547e529357ffdc1d56",[:mix],[{:combine,"~> 0.10",[hex::combine,repo:"hexpm",optional:false]},{:gettext,"~> 0.10",[hex::gettext,repo:"hexpm",optional:false]},{:tzdata,"~> 0.1.8 or ~> 0.5 or ~> 1.0.0",[hex::tzdata,repo:"hexpm",optional:false]}],"hexpm"},

‎test/groupher_server/statistics/statistics_test.exs‎

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

124-
@tag:wip
125124
test"should return recent#{@community_contribute_days} days community contributes by default",
126125
~m(community)ado
127126
days_ago=Timex.shift(Timex.today(),days:-@community_contribute_days)
@@ -148,7 +147,6 @@ defmodule GroupherServer.Test.Statistics do
148147
assertlength(contributes)==@community_contribute_days+1
149148
end
150149

151-
@tag:wip
152150
test"the contributes data should be cached after first query",~m(community)ado
153151
scope=Cache.get_scope(:community_contributes,community.id)
154152
assert{:error,nil}=Cache.get(scope)
@@ -158,7 +156,6 @@ defmodule GroupherServer.Test.Statistics do
158156
assert{:ok,contributes}=Cache.get(scope)
159157
end
160158

161-
@tag:wip2
162159
test"Rihanna should work in test sandbox",~m(community)ado
163160
res=Rihanna.enqueue({IO,:puts,["Work, work, work, work, work."]})
164161
Process.sleep(1000)

‎test/helper/cache_test.exs‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@ defmodule GroupherServer.Test.Helper.Cache do
55
aliasHelper.Cache
66

77
describe"[cache test]"do
8-
@tag:wip
98
test"cache get unexsit key should get nil"do
109
assert{:error,nil}=Cache.get("no exsit")
1110
assert{:error,nil}=Cache.get(:no_exsit)
1211
end
1312

14-
@tag:wip
1513
test"cache put should work"do
1614
assert{:error,nil}=Cache.get(:data)
1715

@@ -26,5 +24,22 @@ defmodule GroupherServer.Test.Helper.Cache do
2624
assert{:ok,true}=Cache.put("namespace.aaa.bbb",[1,%{a:"2"}])
2725
assert{:ok,[1,%{a:"2"}]}=Cache.get("namespace.aaa.bbb")
2826
end
27+
28+
test"cache can be clear"do
29+
assert{:ok,true}=Cache.put(:data,"value")
30+
assert{:ok,"value"}=Cache.get(:data)
31+
32+
assert{:ok,_}=Cache.clear_all()
33+
assert{:error,nil}=Cache.get(:data)
34+
end
35+
36+
test"cache expire should work"do
37+
assert{:ok,true}=Cache.put(:data,"value",expire:1000)
38+
assert{:ok,"value"}=Cache.get(:data)
39+
Process.sleep(900)
40+
assert{:ok,"value"}=Cache.get(:data)
41+
Process.sleep(1200)
42+
assert{:error,nil}=Cache.get(:data)
43+
end
2944
end
3045
end

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp