diff --git a/app/controllers/api/public/v1/courses_controller.rb b/app/controllers/api/public/v1/courses_controller.rb index a9da837d0b..42e2cab42d 100644 --- a/app/controllers/api/public/v1/courses_controller.rb +++ b/app/controllers/api/public/v1/courses_controller.rb @@ -7,7 +7,7 @@ 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 @@ -15,6 +15,16 @@ def index 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],