Skip to content

Commit

Permalink
Cache all blocks (#1918)
Browse files Browse the repository at this point in the history
* Cache regions in the warmer as well

* Move cache_key behavior to Region

this should get the cache keys consistent between a facility and
district group and their associated Regions, because they should have
the same data and therefore the same cache keys

* Fix test

* linting

* looser expectation

* delegate cache_version also

* Cache blocks also; bump up batch size

* turn off query trace by default

too noisy when you dont want this

* linting
  • Loading branch information
rsanheim authored Dec 24, 2020
1 parent ba08f5e commit 3f0ab31
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
14 changes: 13 additions & 1 deletion app/services/reports/region_cache_warmer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ def self.call
new.call
end

BATCH_SIZE = 100
BATCH_SIZE = 1000

def initialize(period: RegionService.default_period)
@period = period
Expand All @@ -29,6 +29,11 @@ def call
cache_facility_groups
end

notify "starting block caching"
Statsd.instance.time("region_cache_warmer.blocks") do
cache_blocks
end

notify "starting facility caching"
Statsd.instance.time("region_cache_warmer.facilities") do
cache_facilities
Expand Down Expand Up @@ -58,6 +63,13 @@ def cache_facility_groups
end
end

def cache_blocks
Region.block_regions.find_each(batch_size: BATCH_SIZE).each do |region|
RegionService.call(region: region, period: period)
Statsd.instance.increment("region_cache_warmer.blocks.cache")
end
end

def cache_facilities
Facility.find_each(batch_size: BATCH_SIZE).each do |region|
RegionService.call(region: region, period: period)
Expand Down
2 changes: 1 addition & 1 deletion config/initializers/active_record_query_trace.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
if Rails.env.development?
# See https://github.com/brunofacca/active-record-query-trace for docs
ActiveRecordQueryTrace.enabled = false
ActiveRecordQueryTrace.level = :app
ActiveRecordQueryTrace.enabled = true
ActiveRecordQueryTrace.lines = 15
end
3 changes: 2 additions & 1 deletion spec/services/reports/region_cache_warmer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ def refresh_views
end

it "warms the cache for all regions" do
facilities = FactoryBot.create_list(:facility, 5, facility_group: facility_group_1)
facilities = FactoryBot.create_list(:facility, 5, block: "Block 1", facility_group: facility_group_1)

expect(Reports::RegionService).to receive(:call).with(hash_including(region: instance_of(Region))).exactly(1).times
expect(Reports::RegionService).to receive(:call).with(hash_including(region: instance_of(FacilityGroup))).exactly(1).times
expect(Reports::RegionService).to receive(:call).with(hash_including(region: instance_of(Facility))).exactly(5).times
Reports::RegionCacheWarmer.call
Expand Down

0 comments on commit 3f0ab31

Please sign in to comment.