Skip to content

Fix/cache wrap function fixup#3169

Open
danoswaltCL wants to merge 3 commits into
devfrom
fix/cache-wrap-function-fixup
Open

Fix/cache wrap function fixup#3169
danoswaltCL wants to merge 3 commits into
devfrom
fix/cache-wrap-function-fixup

Conversation

@danoswaltCL

Copy link
Copy Markdown
Collaborator

this is 4b from #3144.

this is a lower-order thing and can go in 6.6, just putting it up now cuz i have it

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR is part of the Feature Flag optimizations work (issue #3144, item “4b”) and refactors the backend cache batching logic so callers can fetch only missing items on partial cache hits.

Changes:

  • Replaces CacheService.wrapFunction with CacheService.wrapMany, allowing DB fetch callbacks to receive only the missing keys and then merge cached + fetched results.
  • Updates SegmentService.getSegmentByIds to query only missing segment IDs and return results in missing-key order for alignment.
  • Updates unit tests to cover partial-hit behavior and ordering guarantees for wrapMany.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.

File Description
packages/backend/src/api/services/CacheService.ts Introduces wrapMany to batch cache reads/writes and fetch only cache misses.
packages/backend/src/api/services/SegmentService.ts Switches segment batching to wrapMany and queries only missing segment IDs.
packages/backend/test/unit/services/CacheService.test.ts Renames/extends tests to validate wrapMany partial-hit and ordering behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 105 to 112
if (!keys.length) {
return [];
}

// find all cached values for the keys, which will return the value, or undefined if the key is missing.
const keysWithPrefix = keys.map((key) => prefix + key);
const cachedData = (await this.cache.store.mget(...keysWithPrefix)) as (T | undefined)[];

Comment on lines +113 to 120
// determine which keys are missing from the cache
const missingKeys = keys.filter((_, i) => !cachedData[i]);

// if none are missing, we're done, return the cached data
const allCachedFound = cachedData.length > 0 && cachedData.every((cached) => !!cached);
if (allCachedFound) {
return cachedData as T[];
}
Comment on lines +100 to +104
public async wrapMany<T>(
prefix: CACHE_PREFIX,
keys: string[],
dbFetchCallback: (missingKeys: string[]) => Promise<T[]>
): Promise<T[]> {
Comment on lines +169 to +170
// sort to match missingIds order so wrapMany can align with the full keys array
return missingIds.map((id) => result.find((data) => data.id === id));

it('returns null and skips set when value is undefined', async () => {
const result = await service.setCache('key', undefined);
const result = await service.setCache('key', undefined as unknown as string);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants