Skip to content

Commit

Permalink
Cache talks page (#173)
Browse files Browse the repository at this point in the history
* update solid cache

* remove rack mini profiler

* cache talks#show page

* cache expensive query for random recommended talks
  • Loading branch information
adrienpoly committed Sep 6, 2024
1 parent d8cd842 commit 62dab86
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ group :development do
gem "web-console"

# Add speed badges [https://github.com/MiniProfiler/rack-mini-profiler]
gem "rack-mini-profiler"
# gem "rack-mini-profiler"

# Speed up commands on slow machines / big apps [https://github.com/rails/spring]
# gem "spring"
Expand Down
3 changes: 0 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,6 @@ GEM
raabro (1.4.0)
racc (1.8.1)
rack (3.1.7)
rack-mini-profiler (3.3.1)
rack (>= 1.2.0)
rack-proxy (0.7.7)
rack
rack-session (2.0.0)
Expand Down Expand Up @@ -545,7 +543,6 @@ DEPENDENCIES
pagy
propshaft
puma
rack-mini-profiler
rails (~> 7.2.0)
rails-controller-testing
rails_autolink (~> 1.1)
Expand Down
1 change: 1 addition & 0 deletions app/controllers/talks/recommendations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class Talks::RecommendationsController < ApplicationController
def index
redirect_to talk_path(@talk) unless turbo_frame_request?
@talks = @talk&.related_talks || []
fresh_when(@talks)
end

private
Expand Down
1 change: 1 addition & 0 deletions app/controllers/talks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def show
@back_path = speaker_slug.present? ? speaker_path(speaker_slug, page: session[:talks_page]) : talks_path(page: session[:talks_page])

set_meta_tags(@talk)
fresh_when(@talk)
end

# GET /talks/1/edit
Expand Down
6 changes: 5 additions & 1 deletion app/models/talk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,11 @@ def thumbnail_xl
end

def related_talks(limit: 6)
Talk.order("RANDOM()").excluding(self).limit(limit)
ids = Rails.cache.fetch(["talk_recommendations", id, limit], expires_in: 1.week) do
Talk.order("RANDOM()").excluding(self).limit(limit).ids
end

Talk.where(id: ids)
end

def transcript
Expand Down
10 changes: 3 additions & 7 deletions app/views/talks/_talk.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<%# locals: (talk:, admin: false) -%>
<%= content_tag :div, id: dom_id(talk),
data: {
controller: "video-player",
Expand All @@ -21,7 +23,7 @@
<div class="py-4 flex flex-col gap-4 mb-4">
<h1><%= talk.title %></h1>

<% if Current.user&.admin? %>
<% if admin %>
<div class="flex flex-wrap gap-2">
<% talk.approved_topics.each do |topic| %>
<%= link_to topic do %>
Expand Down Expand Up @@ -60,12 +62,6 @@
<%= render partial: "talks/transcript", locals: {talk: talk} %>
</div>
</div>

<% if action_name != "show" %>
<%= link_to "Show this talk", talk, class: "rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
<%= link_to "Edit this talk", edit_talk_path(talk), class: "rounded-lg py-3 ml-2 px-5 bg-gray-100 inline-block font-medium" %>
<% end %>

</div>
<% end %>

Expand Down
16 changes: 9 additions & 7 deletions app/views/talks/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
<span>List</span>
</div>
<% end %>
<div class="w-full flex">
<div class="flex-grow">
<%= render @talk %>
<% cache [@talk, Current.user&.admin?] do %>
<div class="w-full flex">
<div class="flex-grow">
<%= render partial: "talks/talk", locals: {talk: @talk, admin: Current.user&.admin?} %>
</div>
<div class="gap-4 w-60 flex-shrink-0 hidden md:flex md:flex-col px-4">
<%= turbo_frame_tag "recommended_talks", target: "_top", src: talk_recommendations_path(@talk) %>
</div>
</div>
<div class="gap-4 w-60 flex-shrink-0 hidden md:flex md:flex-col px-4">
<%= turbo_frame_tag "recommended_talks", target: "_top", src: talk_recommendations_path(@talk) %>
</div>
</div>
<% end %>
</div>

0 comments on commit 62dab86

Please sign in to comment.