diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index 18c54ab..7c30ca8 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -2,7 +2,7 @@ class HomeController < ApplicationController allow_unauthenticated_access def index - @random_podcast = Podcast.offset(rand(Podcast.count)).first # TODO: use cron + @random_podcast = Podcast.random_by_day @last_podcast = Podcast.last end end diff --git a/app/models/podcast.rb b/app/models/podcast.rb index 789856d..92cd1ff 100644 --- a/app/models/podcast.rb +++ b/app/models/podcast.rb @@ -4,4 +4,13 @@ class Podcast < ApplicationRecord validates :title, presence: true validates :title, length: { minimum: 2 } + + def self.random_by_day + return unless exists? + + cached_random_podcast_id = Rails.cache.fetch("random_podcast_id", expires_in: 1.days) do + offset(rand(Podcast.count)).first.id + end + find_by(id: cached_random_podcast_id) + end end diff --git a/app/views/home/_list.html.erb b/app/views/home/_list.html.erb index 29ca0f6..02a58c5 100644 --- a/app/views/home/_list.html.erb +++ b/app/views/home/_list.html.erb @@ -1,7 +1,11 @@ <% if Podcast.any? %>