Skip to content

Commit

Permalink
Merge pull request #810 from sul-dlss/hours-cache
Browse files Browse the repository at this point in the history
Use expires_in for library hours cache
  • Loading branch information
corylown authored Oct 30, 2024
2 parents 8293e5b + 99d6fce commit 036cf04
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 11 deletions.
2 changes: 1 addition & 1 deletion app/controllers/landing_page_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class LandingPageController < ApplicationController
def index
@hours = Settings.hours_locations.map { |hours_config| LibraryHours.from_config(hours_config) }
@collection_count = site_collection_count
@next_half_hour = TimeService.next_half_hour
@seconds_until_next_half_hour = TimeService.seconds_until_next_half_hour
end

private
Expand Down
11 changes: 8 additions & 3 deletions app/services/time_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@
class TimeService
def self.next_half_hour
now = Time.current
next30 = now.at_beginning_of_hour + (now.min < 30 ? 30.minutes : 60.minutes)
now.at_beginning_of_hour + (now.min < 30 ? 30.minutes : 60.minutes)
end

def self.seconds_until_next_half_hour
delta = next_half_hour - Time.current
return 5.seconds if delta < 5.seconds

return next30 + 5.seconds if next30 - now < 5.seconds
return 30.minutes if delta > 30.minutes

next30
delta
end
end
2 changes: 1 addition & 1 deletion app/views/landing_page/_cards.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<%= image_tag 'locations.png', alt: '', class: 'card-img-top' %>
<div class="card-body px-1">
<h3 class="card-title"><%= t('.location.title') %></h3>
<% cache('location_hours', expires_at: @next_half_hour) do %>
<% cache('location_hours', expires_in: @seconds_until_next_half_hour) do %>
<ul>
<% @hours.each do |location_hours| %>
<li><%= render 'landing_page/location_hours', location_hours: %></li>
Expand Down
33 changes: 27 additions & 6 deletions spec/services/time_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@
include ActiveSupport::Testing::TimeHelpers

describe '.next_half_hour' do
it 'pads the next half-hour time with 5 seconds when the current time is less than 5 seconds away' do
travel_to Time.zone.local(2024, 5, 14, 9, 29, 56) do
expect(described_class.next_half_hour).to eq(Time.zone.local(2024, 5, 14, 9, 30, 5))
end
end

it 'returns the next half-hour when the current time is exactly the half-hour' do
travel_to Time.zone.local(2024, 5, 14, 9, 30) do
expect(described_class.next_half_hour).to eq(Time.zone.local(2024, 5, 14, 10, 0))
Expand Down Expand Up @@ -46,4 +40,31 @@
end
end
end

describe '.seconds_until_next_half_hour' do
it 'returns the minimum of 5 seconds when the next half-hour is less than 5 seconds away' do
travel_to Time.zone.local(2024, 5, 14, 9, 29, 56) do
expect(described_class.seconds_until_next_half_hour).to eq(5.seconds)
end
end

it 'returns a maximum of 30 minutes' do
allow(described_class).to receive(:next_half_hour).and_return(45.minutes.from_now)
expect(described_class.seconds_until_next_half_hour).to eq(30.minutes)
end

it 'returns the correct number of seconds if the current time is within the thresholds' do
travel_to Time.zone.local(2024, 5, 14, 9, 30) do
expect(described_class.seconds_until_next_half_hour).to eq(30.minutes)
end

travel_to Time.zone.local(2024, 5, 14, 10, 0) do
expect(described_class.seconds_until_next_half_hour).to eq(30.minutes)
end

travel_to Time.zone.local(2024, 5, 14, 9, 20, 30) do
expect(described_class.seconds_until_next_half_hour).to eq(9.minutes + 30.seconds)
end
end
end
end

0 comments on commit 036cf04

Please sign in to comment.