Skip to content

Commit

Permalink
refactor: Standardize cache key for controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
nanafox committed Sep 18, 2024
1 parent e5f165d commit 53c6ded
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
4 changes: 3 additions & 1 deletion app/controllers/api/v1/categories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ class CategoriesController < ApplicationController
# GET /api/v1/categories.json
# Retrieves a paginated list of categories filtered by developer token
def index
page = params[:page] || 1

categories = Category.by_developer(developer_id)
.by_name(params[:name])
.by_search(params[:search])
.page(params[:page])
.page(page)
.per(page_size)

# implement caching for the collection of categories
Expand Down
5 changes: 3 additions & 2 deletions app/controllers/concerns/cacheable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ module Cacheable
end

def base_key
self.class.name.underscore
Rails.logger.debug "#{self.class.name.underscore}_dev_id_#{developer_id}"
"#{self.class.name.underscore}_dev_id_#{developer_id}"
end

# rubocop:disable Metrics/ParameterLists
Expand Down Expand Up @@ -66,6 +67,6 @@ def invalidate_single_resource_cache(cache_key)

# Returns the current cache key based on the controller's context.
def current_cache_key
"#{base_key}_#{params[:id]}_#{developer_id}"
"#{base_key}_#{params[:id]}"
end
end
4 changes: 3 additions & 1 deletion spec/requests/api/v1/products_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,9 @@
allow(Product).to receive(:where).and_call_original
end

let(:base_key) { 'api/v1/products_controller' }
let(:base_key) do
"api/v1/products_controller_dev_id_#{developers.dig(:first, :id)}"
end
let(:page) { 1 }
let(:page_size) { 100 }

Expand Down

0 comments on commit 53c6ded

Please sign in to comment.