Shopify / Shopify/identity_cache

Improve API for "fetch" methods

Open
#510 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ruby
Stars
2k
Forks
174
Avg merge
13m
Merged PRs (30d)
3

Description

The fetch methods (e.g. fetch_#{association_name}) for loading or accessing an already loaded cached record(s) seems inappropriate because

  1. It doesn't make it clear to those unfamiliar with this library that the data could come from the cache.
  2. It makes it easy for those using this library (naturally for performance reasons) to introduce uncached N+1 queries through lazy loading

Problem 1 seems like it came from the fetch method name being taken from Rails.cache.fetch, but the cache context has since been lost. This problem could be avoided by prefixing fetch methods with cache_ (e.g. cache_fetch_#{association_name}), where alias_method could be used for backwards compatibility.

Problem 2 would benefit from having an alternative method that raises instead of lazily loading when the data isn't already loaded (e.g. cacheable_#{association_name}). This alternative method should be used if the data is expected to already be loaded so that it can be used in a loop. Note that the accessed value may not have come from the cache or a cache_fetch_* method, given the Active Record association fallback (hence the suggested cacheable_ prefix).

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the existing fetch methods and their association-loading paths, then confirm how the current names are exposed and where backwards compatibility would apply. Define acceptance criteria for cache-prefixed aliases and an alternative that raises rather than lazy-loads, including coverage for naming, compatibility, and lazy-loading behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
backend-api-design
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.