Skip to content

Cache the number of forks of a project

yorickpeterse-staging requested to merge forks-count-cache into master

What does this MR do?

This MR changes Project#forks_count so its result is cached in Redis and refreshed whenever necessary.

Counting the number of projects can take between 30 and 35 milliseconds, and we happen to do this at least twice on the homepage of a project. By caching this we can thus reduce loading times by around 60 seconds (for GitLab CE at least). This is based on the output of the performance bar, which contains the following two entries:

38.319ms	SELECT COUNT(*) FROM "projects" INNER JOIN "forked_project_links" ON "projects"."id" = "forked_project_links"."forked_to_project_id" WHERE "forked_project_links"."forked_from_project_id" = 13083 AND ("projects"."pending_delete" != 't');
31.547ms	SELECT COUNT(*) FROM "projects" INNER JOIN "forked_project_links" ON "projects"."id" = "forked_project_links"."forked_to_project_id" WHERE "forked_project_links"."forked_from_project_id" = 13083 AND ("projects"."pending_delete" != 't');

The cache is refreshed when:

  • A fork is created
  • A fork is unlinked
  • The source project is removed
  • When the cache expires (2 weeks after being created)

Does this MR meet the acceptance criteria?

Edited by yorickpeterse-staging

Merge request reports