PERF: Cache build_not_found_page

approved

#1

PERF: Cache build_not_found_page

diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index 846ffc2..e625b8e 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -717,12 +717,21 @@ class ApplicationController < ActionController::Base
       layout = 'application' if layout == 'no_ember'
     end
 
-    category_topic_ids = Category.pluck(:topic_id).compact
+    if !SiteSetting.login_required? || current_user
+      key = "page_not_found_topics"
+      if @topics_partial = $redis.get(key)
+        @topics_partial = @topics_partial.html_safe
+      else
+        category_topic_ids = Category.pluck(:topic_id).compact
+        @top_viewed = TopicQuery.new(nil, except_topic_ids: category_topic_ids).list_top_for("monthly").topics.first(10)
+        @recent = Topic.includes(:category).where.not(id: category_topic_ids).recent(10)
+        @topics_partial = render_to_string partial: '/exceptions/not_found_topics'
+        $redis.setex(key, 10.minutes, @topics_partial)
+      end
+    end
+
     @container_class = "wrap not-found-container"
-    @top_viewed = TopicQuery.new(nil, except_topic_ids: category_topic_ids).list_top_for("monthly").topics.first(10)
-    @recent = Topic.includes(:category).where.not(id: category_topic_ids).recent(10)
-    @slug =  params[:slug].class == String ? params[:slug] : ''
-    @slug =  (params[:id].class == String ? params[:id] : '') if @slug.blank?
+    @slug = params[:slug].presence || params[:id].presence || ""
     @slug.tr!('-', ' ')
     @hide_search = true if SiteSetting.login_required
     render_to_string status: status, layout: layout, formats: [:html], template: '/exceptions/not_found'
diff --git a/app/views/exceptions/_not_found_topics.html.erb b/app/views/exceptions/_not_found_topics.html.erb
new file mode 100644
index 0000000..59cf088
--- /dev/null
+++ b/app/views/exceptions/_not_found_topics.html.erb
@@ -0,0 +1,20 @@
+<div class="row page-not-found-topics">
+  <div class="popular-topics">
+    <h2 class="popular-topics-title"><%= t 'page_not_found.popular_topics' %></h2>
+    <% @top_viewed.each do |t| %>
+      <div class='not-found-topic'>
+        <%= link_to t.title, t.relative_url %><%= category_badge(t.category) %>
+      </div>
+    <% end %>
+    <a href="<%= path "/top" %>" class="btn btn-default"><%= t 'page_not_found.see_more' %>&hellip;</a>
+  </div>
+  <div class="recent-topics">
+    <h2 class="recent-topics-title"><%= t 'page_not_found.recent_topics' %></h2>
+    <% @recent.each do |t| %>
+      <div class='not-found-topic'>
+        <%= link_to t.title, t.relative_url %><%= category_badge(t.category) %>
+      </div>
+    <% end %>
+    <a href="<%= path "/latest" %>" class="btn btn-default"><%= t 'page_not_found.see_more' %>&hellip;</a>
+  </div>
+</div>
diff --git a/app/views/exceptions/not_found.html.erb b/app/views/exceptions/not_found.html.erb
index e787e78..a1173a8 100644
--- a/app/views/exceptions/not_found.html.erb
+++ b/app/views/exceptions/not_found.html.erb
@@ -2,28 +2,7 @@
 
 <%= build_plugin_html 'server:not-found-before-topics' %>
 
-<% unless SiteSetting.login_required? && current_user.nil? %>
-  <div class="row page-not-found-topics">
-    <div class="popular-topics">
-      <h2 class="popular-topics-title"><%= t 'page_not_found.popular_topics' %></h2>
-      <% @top_viewed.each do |t| %>
-        <div class='not-found-topic'>
-          <%= link_to t.title, t.relative_url %><%= category_badge(t.category) %>
-        </div>
-      <% end %>
-      <a href="<%= path "/top" %>" class="btn btn-default"><%= t 'page_not_found.see_more' %>&hellip;</a>
-    </div>
-    <div class="recent-topics">
-      <h2 class="recent-topics-title"><%= t 'page_not_found.recent_topics' %></h2>
-      <% @recent.each do |t| %>
-        <div class='not-found-topic'>
-          <%= link_to t.title, t.relative_url %><%= category_badge(t.category) %>
-        </div>
-      <% end %>
-      <a href="<%= path "/latest" %>" class="btn btn-default"><%= t 'page_not_found.see_more' %>&hellip;</a>
-    </div>
-  </div>
-<% end %>
+<%= @topics_partial %>
 
 <%- unless @hide_search%>
   <div class="row">
diff --git a/spec/requests/application_controller_spec.rb b/spec/requests/application_controller_spec.rb
index 0e9f251..d2d63ab 100644
--- a/spec/requests/application_controller_spec.rb
+++ b/spec/requests/application_controller_spec.rb
@@ -143,6 +143,21 @@ RSpec.describe ApplicationController do
         expect(response.status).to eq(404)
         expect(response.body).to_not include('google.com/search')
       end
+
+      it 'should cache results' do
+        $redis.del("page_not_found_topics")
+
+        topic1 = Fabricate(:topic)
+        get '/t/nope-nope/99999999'
+        expect(response.status).to eq(404)
+        expect(response.body).to include(topic1.title)
+
+        topic2 = Fabricate(:topic)
+        get '/t/nope-nope/99999999'
+        expect(response.status).to eq(404)
+        expect(response.body).to include(topic1.title)
+        expect(response.body).to_not include(topic2.title)
+      end
     end
   end

GitHub sha: 90ce4486


#2

Minor… but I would like you to confirm… can you check there is a test for:

if !SiteSetting.login_required? || current_user

In particular one that sets login_required to true and then has no user logged in and tries to get a 404. We got to make sure we never regress on a 404 page that leaks hidden content … I bet we have a test, but still … worth confirming @udan11


Approved #3