From 896fa7d68ec8409d5c2b2c96a958c6b5ed72905d Mon Sep 17 00:00:00 2001 From: Miles Zhang Date: Wed, 1 Nov 2023 18:09:50 +0800 Subject: [PATCH] feat: add cache for daily statistic data (#1489) * feat: add cache for daily statistic data Signed-off-by: Miles Zhang * test: fix test Signed-off-by: Miles Zhang --------- Signed-off-by: Miles Zhang --- app/controllers/api/v1/daily_statistics_controller.rb | 4 +++- app/controllers/api/v1/monetary_data_controller.rb | 2 ++ app/models/daily_statistic.rb | 2 +- test/models/daily_statistic_test.rb | 2 +- 4 files changed, 7 insertions(+), 3 deletions(-) diff --git a/app/controllers/api/v1/daily_statistics_controller.rb b/app/controllers/api/v1/daily_statistics_controller.rb index 8a092ba33..4c6294c74 100644 --- a/app/controllers/api/v1/daily_statistics_controller.rb +++ b/app/controllers/api/v1/daily_statistics_controller.rb @@ -6,7 +6,9 @@ class DailyStatisticsController < ApplicationController def show daily_statistics = DailyStatistic.order(created_at_unixtimestamp: :asc).valid_indicators - render json: rendered_json(daily_statistics) + if stale?(daily_statistics, public: true) + render json: rendered_json(daily_statistics) + end end private diff --git a/app/controllers/api/v1/monetary_data_controller.rb b/app/controllers/api/v1/monetary_data_controller.rb index 0a75148ef..cd4e7719a 100644 --- a/app/controllers/api/v1/monetary_data_controller.rb +++ b/app/controllers/api/v1/monetary_data_controller.rb @@ -4,6 +4,8 @@ class MonetaryDataController < ApplicationController before_action :validate_query_params, only: :show def show + expires_in 1.hour, public: true, stale_while_revalidate: 10.minutes, stale_if_error: 1.hour + monetary_data = MonetaryData.new render json: MonetaryDataSerializer.new(monetary_data, params: { indicator: params[:id] }) diff --git a/app/models/daily_statistic.rb b/app/models/daily_statistic.rb index 0087fccc4..d1aceed13 100644 --- a/app/models/daily_statistic.rb +++ b/app/models/daily_statistic.rb @@ -12,7 +12,7 @@ class DailyStatistic < ApplicationRecord attr_accessor :from_scratch - scope :valid_indicators, -> { select(VALID_INDICATORS - %w(burnt liquidity created_at updated_at) + %w(id)) } + scope :valid_indicators, -> { select(VALID_INDICATORS - %w(burnt liquidity created_at) + %w(id updated_at)) } scope :recent, -> { order("created_at_unixtimestamp desc nulls last") } scope :recent_year, -> { where("created_at_unixtimestamp >= ? and created_at_unixtimestamp < ?", Time.current.beginning_of_year.to_i, Time.current.to_i) diff --git a/test/models/daily_statistic_test.rb b/test/models/daily_statistic_test.rb index ba4764f28..d6fdd79cd 100644 --- a/test/models/daily_statistic_test.rb +++ b/test/models/daily_statistic_test.rb @@ -4,6 +4,6 @@ class DailyStatisticTest < ActiveSupport::TestCase test "valid_indicators should only return valid indicators" do create(:daily_statistic) attrs = DailyStatistic.valid_indicators.first.attribute_names + %w(burnt liquidity) - assert_equal (DailyStatistic::VALID_INDICATORS + %w(id)).sort, attrs.sort + assert_equal (DailyStatistic::VALID_INDICATORS + %w(id updated_at)).sort, attrs.sort end end