-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve video controllers caching and retrieving #5709
Improve video controllers caching and retrieving #5709
Conversation
Writing down some of my discoveries, I'll open a separate issues once I get to the bottom of these: if you notice also the fetchers fetch from page 0 and then page 1 which are the same page |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔥
num = [per_page, 1000].min | ||
|
||
@video_articles = Article.with_video. | ||
includes([:user]). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎉
@@ -30,7 +30,7 @@ | |||
<span class="video-timestamp"><%= video_article.video_duration_in_minutes %></span> | |||
</div> | |||
<p><strong><%= video_article.title %></strong></p> | |||
<p class="video-username"><%= User.find(video_article.user_id).name %></p> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NICE find!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔥
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very nice!
where("score > ?", -4). | ||
page = params[:page] | ||
per_page = (params[:per_page] || 24).to_i | ||
num = [per_page, 1000].min |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is lovely refactoring!
@@ -138,6 +138,8 @@ class Article < ApplicationRecord | |||
|
|||
scope :feed, -> { published.select(:id, :published_at, :processed_html, :user_id, :organization_id, :title, :path) } | |||
|
|||
scope :with_video, -> { published.where.not(video: [nil, ""], video_thumbnail_url: [nil, ""]).where("score > ?", -4) } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
great work, and great catch on fixing some of the funny stuff that was happening here! ✨
@@ -138,6 +138,8 @@ class Article < ApplicationRecord | |||
|
|||
scope :feed, -> { published.select(:id, :published_at, :processed_html, :user_id, :organization_id, :title, :path) } | |||
|
|||
scope :with_video, -> { published.where.not(video: [nil, ""], video_thumbnail_url: [nil, ""]).where("score > ?", -4) } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
@@ -1,21 +1,25 @@ | |||
module Api | |||
module V0 | |||
class VideosController < ApiController | |||
caches_action :index, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lol, I had never seen caches_action
before and had to look up where it was coming from 🙈
TIL about actionpack-action_caching
and the fact that we use caches_action
in podcast_episodes_controller
and tags_controller
. I'm assuming we can move away from that and just use set_cache_control_headers
in place of it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vaidehijoshi this is exactly what i've been doing with these latest PRs :) only a few endpoints left and we can remove that gem :D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rad!! Thanks for doing this 🙌
What type of PR is this? (check all applicable)
Description
This PR has been a little bit of a ride, through it I found out we have some inefficiencies in https://dev.to/videos (and likely other pages) where we fetch the same page (made by 24 items) 3 times (once in the server and twice in the frontend).
This PR though addresses another set of things:
/api/videos
fetches only the fields it needs to render the JSON, doesn't do a N + 1 on the user, has flexible pagination and sets the correct edge caching, removing server side caching/videos
fetches only the fields needed, removes the SQL queries that were issued in the partial resulting in N + 1 and sets the correct edge cachingI did not address the issues with multiple fetchings in
/videos
as it's outside the scope of this PR and it's probably a much bigger refactoring.The PR is essentially done but it incorporates #5673 so I'm tagging this as WIP until that one is merged and then rebase and force push this one