Skip to content

fix: skip cache save on exact primary key hit#91

Merged
j178 merged 3 commits intoj178:mainfrom
shaanmajid:fix/cache-hit-skip
Mar 16, 2026
Merged

fix: skip cache save on exact primary key hit#91
j178 merged 3 commits intoj178:mainfrom
shaanmajid:fix/cache-hit-skip

Conversation

@shaanmajid
Copy link
Copy Markdown
Collaborator

@shaanmajid shaanmajid commented Mar 16, 2026

Summary

  • Skip redundant cache uploads when the restore step already matched the primary key, avoiding unnecessary work and API calls on every run.
  • Track the matched restore key via CACHE_MATCHED_KEY_STATE and compare it against the primary key before saving.
  • Add unit tests covering restore hit/miss, save skip, and error paths.

Approach

This follows the same pattern used by the official GitHub Actions that wrap @actions/cache:

All of these save the restored key via core.saveState() during the restore step, then compare it against the primary key in the post-run save step. If they match, the cache already exists and saving is skipped.

cacheId !== -1 check and removal of the "already exists" branch

The previous code caught errors from cache.saveCache() and checked message.includes('already exists') to handle duplicate saves. This branch was dead code in two ways:

  1. @actions/cache catches ReserveCacheError internally (both v1 and v2 code paths) and logs via core.info() before returning -1. The error never propagates to the caller.
  2. The ReserveCacheError message is "Unable to reserve cache with key ..., another job may be creating this cache.", which does not contain the string "already exists".

The new code checks cacheId !== -1 to align with the library's actual contract. Since @actions/cache already logs the reason internally on every path that returns -1, no additional logging is needed from our side. The catch block now only handles genuinely unexpected errors.

@j178
Copy link
Copy Markdown
Owner

j178 commented Mar 16, 2026

Thanks!

@j178 j178 merged commit f90cb0a into j178:main Mar 16, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants