Skip to content

Avoid duplicated keys in ProtectedBranches cache

Vasilii Iakliushin requested to merge 387978_remove_cache_key_duplication into master

What does this MR do and why?

Contributes to #387978 (closed)

Problem

Sometimes we provide a ProjectPresenter object to the CacheService. Because we use object.class.name for the Redis key generation, it leads to duplicated keys problem when Project and ProjectPresenter objects generate different keys.

Solution

  • Extract CacheKey generation logic into a separate class
  • Use is_a? (via case statement) to identify object type

MR acceptance checklist

Please evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

How to set up and validate locally

  1. Enable feature flags: group_protected_branches and allow_protected_branches_for_group
  2. Visit a project page that has a protected branch setup
  3. In Rails console, execute the following command
Gitlab::Redis::Cache.with { |r| r }.scan_each.to_a.select { |a| a.include? 'protected_branch' }
  1. You should see a record like "cache:gitlab:protected_branch:project:1" where the last digit is your project id.
Edited by Vasilii Iakliushin

Merge request reports