-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add market history cache #2182
Merged
Merged
add market history cache #2182
Changes from 16 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
25cc11b
add market history cache
ayrat555 1815fe6
Merge branch 'master' into ab-cache-for-chart-data
ayrat555 e3f2a64
add market history cache to supervision tree
ayrat555 4fd053b
use cached data
ayrat555 18e6cc8
fix tests
ayrat555 741ba61
fix failing test
ayrat555 6c6b2d1
Merge branch 'master' into ab-cache-for-chart-data
ayrat555 9156f2d
fix ChannelCase
ayrat555 b6a2789
fix tests
ayrat555 583e869
Merge branch 'master' into ab-cache-for-chart-data
ayrat555 f034ad1
fix tests
ayrat555 6ea73f5
use milliseconds in cache
ayrat555 ef5a015
mix format
ayrat555 b90f36a
add CHANGELOG entry
ayrat555 5d3a226
fix tests
ayrat555 b3b3714
fix test
ayrat555 5d1ae20
Update CHANGELOG.md
ayrat555 b12f74f
Merge branch 'master' into ab-cache-for-chart-data
ayrat555 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
defmodule Explorer.Market.MarketHistoryCache do | ||
@moduledoc """ | ||
Caches recent market history. | ||
""" | ||
|
||
import Ecto.Query, only: [from: 2] | ||
|
||
alias Explorer.Market.MarketHistory | ||
alias Explorer.Repo | ||
|
||
@cache_name :market_history | ||
@last_update_key :last_update | ||
@history_key :history | ||
# 6 hours | ||
@cache_period 1_000 * 60 * 60 * 6 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could use erlang timer library here |
||
@recent_days 30 | ||
|
||
def fetch do | ||
if cache_expired?() do | ||
update_cache() | ||
else | ||
fetch_from_cache(@history_key) | ||
end | ||
end | ||
|
||
def cache_name, do: @cache_name | ||
|
||
def data_key, do: @history_key | ||
|
||
def updated_at_key, do: @last_update_key | ||
|
||
def recent_days_count, do: @recent_days | ||
|
||
defp cache_expired? do | ||
updated_at = fetch_from_cache(@last_update_key) | ||
|
||
cond do | ||
is_nil(updated_at) -> true | ||
current_time() - updated_at > @cache_period -> true | ||
true -> false | ||
end | ||
end | ||
|
||
defp update_cache do | ||
new_data = fetch_from_db() | ||
|
||
put_into_cache(@last_update_key, current_time()) | ||
put_into_cache(@history_key, new_data) | ||
|
||
new_data | ||
end | ||
|
||
defp fetch_from_db do | ||
day_diff = @recent_days * -1 | ||
|
||
query = | ||
from( | ||
mh in MarketHistory, | ||
where: mh.date > date_add(^Date.utc_today(), ^day_diff, "day"), | ||
order_by: [desc: mh.date] | ||
) | ||
|
||
Repo.all(query) | ||
end | ||
|
||
defp fetch_from_cache(key) do | ||
ConCache.get(@cache_name, key) | ||
end | ||
|
||
defp put_into_cache(key, value) do | ||
ConCache.put(@cache_name, key, value) | ||
end | ||
|
||
defp current_time do | ||
utc_now = DateTime.utc_now() | ||
|
||
DateTime.to_unix(utc_now, :millisecond) | ||
end | ||
end |
90 changes: 90 additions & 0 deletions
90
apps/explorer/test/explorer/market/market_history_cache_test.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
defmodule Explorer.Market.MarketHistoryCacheTest do | ||
use Explorer.DataCase | ||
|
||
alias Explorer.Market | ||
alias Explorer.Market.MarketHistoryCache | ||
|
||
setup do | ||
Supervisor.terminate_child(Explorer.Supervisor, {ConCache, MarketHistoryCache.cache_name()}) | ||
Supervisor.restart_child(Explorer.Supervisor, {ConCache, MarketHistoryCache.cache_name()}) | ||
|
||
on_exit(fn -> | ||
Supervisor.terminate_child(Explorer.Supervisor, {ConCache, Explorer.Chain.BlocksCache.cache_name()}) | ||
Supervisor.restart_child(Explorer.Supervisor, {ConCache, Explorer.Chain.BlocksCache.cache_name()}) | ||
end) | ||
|
||
:ok | ||
end | ||
|
||
describe "fetch/1" do | ||
test "caches data on the first call" do | ||
today = Date.utc_today() | ||
|
||
records = | ||
for i <- 0..29 do | ||
%{ | ||
date: Timex.shift(today, days: i * -1), | ||
closing_price: Decimal.new(1), | ||
opening_price: Decimal.new(1) | ||
} | ||
end | ||
|
||
Market.bulk_insert_history(records) | ||
|
||
refute fetch_data() | ||
|
||
assert Enum.count(MarketHistoryCache.fetch()) == 30 | ||
|
||
assert fetch_data() == records | ||
end | ||
|
||
test "updates cache if cache is stale" do | ||
today = Date.utc_today() | ||
|
||
stale_records = | ||
for i <- 0..29 do | ||
%{ | ||
date: Timex.shift(today, days: i * -1), | ||
closing_price: Decimal.new(1), | ||
opening_price: Decimal.new(1) | ||
} | ||
end | ||
|
||
Market.bulk_insert_history(stale_records) | ||
|
||
MarketHistoryCache.fetch() | ||
|
||
stale_updated_at = fetch_updated_at() | ||
|
||
assert fetch_data() == stale_records | ||
|
||
ConCache.put(MarketHistoryCache.cache_name(), MarketHistoryCache.updated_at_key(), 1) | ||
|
||
fetch_data() | ||
|
||
assert stale_updated_at != fetch_updated_at() | ||
end | ||
end | ||
|
||
defp fetch_updated_at do | ||
ConCache.get(MarketHistoryCache.cache_name(), MarketHistoryCache.updated_at_key()) | ||
end | ||
|
||
defp fetch_data do | ||
MarketHistoryCache.cache_name() | ||
|> ConCache.get(MarketHistoryCache.data_key()) | ||
|> case do | ||
nil -> | ||
nil | ||
|
||
records -> | ||
Enum.map(records, fn record -> | ||
%{ | ||
date: record.date, | ||
closing_price: record.closing_price, | ||
opening_price: record.opening_price | ||
} | ||
end) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ayrat555 there is a doubled
Features
sectionThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed