Skip to content

Commit

Permalink
Cache the course count in the api
Browse files Browse the repository at this point in the history
  • Loading branch information
inulty-dfe committed Oct 29, 2024
1 parent 4b4d88d commit 858bfe6
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion app/controllers/api/public/v1/courses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,24 @@ class CoursesController < API::Public::V1::ApplicationController
def index
render jsonapi: paginate(courses),
include: include_param,
meta: { count: courses.count('course.id') },
meta: { count: cached_course_count },
class: API::Public::V1::SerializerService.call
rescue ActiveRecord::StatementInvalid
render json: { status: 400, message: 'Invalid changed_since value, the format should be an ISO8601 UTC timestamp, for example: `2019-01-01T12:01:00Z`' }.to_json, status: :bad_request
end

private

def cached_course_count
if params[:no_cache]
courses.count('course.id')
else
Rails.cache.fetch('api_course_count', expires_in: 5.minutes) do
courses.count('course.id')
end
end
end

def courses
@courses ||= APICourseSearchService.call(
filter: params[:filter],
Expand Down

0 comments on commit 858bfe6

Please sign in to comment.