Skip to content

Commit

Permalink
Merge pull request #2602 from ujh/speed-up-missing-descriptions
Browse files Browse the repository at this point in the history
Add more caching in the controller
  • Loading branch information
ujh authored Jan 6, 2025
2 parents fada69f + f3f3e94 commit aa6b660
Showing 1 changed file with 34 additions and 19 deletions.
53 changes: 34 additions & 19 deletions app/controllers/descriptions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ class DescriptionsController < ApplicationController
before_action :authenticate_user!, only: [:my_missing]

def missing
@missing_inks = sorted_inks(clusters_without_descriptions_ids)
@missing_brands = sorted_brands(brands_without_descriptions_ids)
@missing_inks = sorted_inks(clusters_without_descriptions_ids, cache: true)
@missing_brands =
sorted_brands(brands_without_descriptions_ids, cache: true)
end

def my_missing
Expand All @@ -18,7 +19,7 @@ def brands_without_descriptions_ids
.cache
.fetch(
"DescriptionsController#brands_without_descriptions_ids",
expires_in: 1.hour
expires_in: 6.hours
) { BrandCluster.without_description.pluck(:id) }
end

Expand All @@ -31,7 +32,7 @@ def clusters_without_descriptions_ids
.cache
.fetch(
"DescriptionsController#clusters_without_descriptions_ids",
expires_in: 1.hour
expires_in: 6.hour
) do
MacroCluster
.without_description
Expand All @@ -46,22 +47,36 @@ def my_clusters_without_descriptions_ids
MacroCluster.without_description_of_user(current_user).pluck(:id)
end

def sorted_inks(ids)
MacroCluster
.where(id: ids)
.includes(:brand_cluster)
.order(:brand_name, :line_name, :ink_name)
.page(params[:inks_page])
.per(10)
def sorted_inks(ids, cache: false)
rel =
MacroCluster
.where(id: ids)
.select(:id, :brand_name, :line_name, :ink_name)
.order(:brand_name, :line_name, :ink_name)
rel =
Rails
.cache
.fetch("DescriptionsController#sorted_inks", expires_in: 6.hours) do
rel
end if cache

rel.page(params[:inks_page]).per(10)
end

def sorted_brands(ids)
BrandCluster
.where(id: ids)
.joins(:macro_clusters)
.group("brand_clusters.id")
.order(:name)
.page(params[:brands_page])
.per(10)
def sorted_brands(ids, cache: false)
rel =
BrandCluster
.where(id: ids)
.select(:id, :name)
.group("brand_clusters.id")
.order(:name)
rel =
Rails
.cache
.fetch("DescriptionsController#sorted_brands", expires_in: 6.hours) do
rel
end if cache

rel.page(params[:brands_page]).per(10)
end
end

0 comments on commit aa6b660

Please sign in to comment.