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

Commit

Permalink
refactor(cron): clear cache in midinight & add expire option
Browse files Browse the repository at this point in the history
  • Loading branch information
mydearxym committed Aug 3, 2019
1 parent ed98827 commit 8106067
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 2 deletions.
6 changes: 5 additions & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ config :rihanna,
producer_postgres_connection: {Ecto, GroupherServer.Repo}

# cron-like job scheduler
config :groupher_server, Helper.Scheduler, jobs: []
config :groupher_server, Helper.Scheduler,
jobs: [
# Every midnight
{"@daily", {Helper.Scheduler, :clear_all_cache, []}}
]

import_config "#{Mix.env()}.exs"

Expand Down
2 changes: 1 addition & 1 deletion lib/groupher_server/statistics/delegates/contribute.ex
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ defmodule GroupherServer.Statistics.Delegate.Contribute do
%Community{id: id}
|> do_get_contributes()
|> to_counts_digest(days: @community_contribute_days)
|> done_and_cache(scope)
|> done_and_cache(scope, expire: 60_000)
end

defp update_contribute_record(%UserContribute{} = contribute) do
Expand Down
13 changes: 13 additions & 0 deletions lib/helper/cache.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ defmodule Helper.Cache do
Cachex.put(:site_cache, cache_key, cache_value)
end

def put(cache_key, cache_value, expire: expire_time) do
Cachex.put(:site_cache, cache_key, cache_value)
Cachex.expire(:site_cache, cache_key, expire_time)
end

@doc """
clear all the cache
## Example
iex> Helper.Cache.clear()
{:ok, 1}
"""
def clear_all(), do: Cachex.clear(:site_cache)

@doc """
cache scope of community contributes digest
"""
Expand Down
10 changes: 10 additions & 0 deletions lib/helper/scheduler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,14 @@ defmodule Helper.Scheduler do
cron-like job scheduler
"""
use Quantum.Scheduler, otp_app: :groupher_server

alias Helper.Cache

@doc """
clear all the cache in Cachex
just in case the cache system broken
"""
def clear_all_cache do
Cache.clear_all()
end
end
7 changes: 7 additions & 0 deletions lib/helper/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ defmodule Helper.Utils do
# def done({:error, error}), do: {:error, error}
def done(result), do: {:ok, result}

def done_and_cache(result, scope, expire: expire_time) do
with {:ok, res} <- done(result) do
Cache.put(scope, res, expire: expire_time)
{:ok, res}
end
end

def done_and_cache(result, scope) do
with {:ok, res} <- done(result) do
Cache.put(scope, res)
Expand Down
19 changes: 19 additions & 0 deletions test/helper/cache_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,24 @@ defmodule GroupherServer.Test.Helper.Cache do
assert {:ok, true} = Cache.put("namespace.aaa.bbb", [1, %{a: "2"}])
assert {:ok, [1, %{a: "2"}]} = Cache.get("namespace.aaa.bbb")
end

@tag :wip2
test "cache can be clear" do
assert {:ok, true} = Cache.put(:data, "value")
assert {:ok, "value"} = Cache.get(:data)

assert {:ok, _} = Cache.clear_all()
assert {:error, nil} = Cache.get(:data)
end

@tag :wip2
test "cache expire should work" do
assert {:ok, true} = Cache.put(:data, "value", expire: 1000)
assert {:ok, "value"} = Cache.get(:data)
Process.sleep(900)
assert {:ok, "value"} = Cache.get(:data)
Process.sleep(1200)
assert {:error, nil} = Cache.get(:data)
end
end
end

0 comments on commit 8106067

Please sign in to comment.