Avoid duplicated keys in ProtectedBranches cache
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
- Enable feature flags:
group_protected_branches
andallow_protected_branches_for_group
- Visit a project page that has a protected branch setup
- In Rails console, execute the following command
Gitlab::Redis::Cache.with { |r| r }.scan_each.to_a.select { |a| a.include? 'protected_branch' }
- You should see a record like
"cache:gitlab:protected_branch:project:1"
where the last digit is your project id.
Edited by Vasilii Iakliushin