Skip to content

Restore keys by using the prefix#3

Merged
danySam merged 3 commits into
danySam:mainfrom
breezewish:wenxuan/find_by_prefix
May 31, 2026
Merged

Restore keys by using the prefix#3
danySam merged 3 commits into
danySam:mainfrom
breezewish:wenxuan/find_by_prefix

Conversation

@breezewish
Copy link
Copy Markdown

Description

Hi, thanks for the cool action! This PR attempts to keep behavior of restoring keys by "prefix" instead of exact match (which is exactly the behavior of actions/cache). It could be useful in fallback scenarios.

Motivation and Context

For example:

- uses: actions/cache@v4
  with:
    path: |
      ~/.cache/go-build
      ~/go/pkg/mod
    key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
    restore-keys: |
      ${{ runner.os }}-go-

Suppose that our hash was updated from aaaaaa to bbbbbb, with cache of aaaaaa is available.

Previously actions/cache would first try to restore with keys Linux-go-bbbbbb and then fallback to latest cache with key Linux-go-<any>, which becomes a successful lookup of Linux-go-aaaaaa. So the cache is hit, and try to utilized as much as possible.

In gcs-cache, however, cache will be missed totally because there is no key Linux-go-bbbbbb and Linux-go-.

How Has This Been Tested?

I'm also wondering how this could be tested automatically! Could you give me some advice? Currently I'm testing it manually by applying it in my own workflow.

Screenshots (if appropriate):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
    Considered as a bug fix because it now follows the actions/cache behavior.
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation (add or update README or docs)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

Signed-off-by: Wish <breezewish@outlook.com>
@breezewish breezewish requested a review from danySam as a code owner May 27, 2025 03:58
restoreCache now returns { cacheKey, gcsPath } instead of just the GCS
path, so isExactKeyMatch in restoreImpl correctly compares against the
original cache key. This fixes cache-hit always being "false" on GCS.

Also adds a GCS mock and test suite covering exact match, prefix match,
latest-file selection, cache miss, and lookup-only scenarios.

Co-authored-by: Harald Wellmann <hwellmann@northdata.de>
Copy link
Copy Markdown
Owner

@danySam danySam left a comment

Choose a reason for hiding this comment

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

Hey @breezewish, thank you so much for this PR! The prefix-based lookup using getFiles is exactly the right approach to match actions/cache behaviour. Really appreciate you taking the time to implement this.

Sorry for taking so long to get around to merging this; it's been sitting way too long, and your fix is solid.

I've added a couple of commits on top of your work:

  • Fixed the cache-hit output (it was always returning false because restoreFromGCS was returning the GCS path instead of the cache key). Now uses a { cacheKey, gcsPath } return type so the key comparison works correctly.
  • Added a GCS mock and test suite covering exact match, prefix match, latest-file selection, and lookup-only scenarios.
  • Rebuilt the dist/ bundles.

Tested this in production and everything works as expected — prefix matching, cache-hit: true on exact match, and save correctly skipped on cache hit.

Thanks again for the contribution! 🎉

@danySam danySam merged commit cbaef79 into danySam:main May 31, 2026
18 of 26 checks passed
@breezewish breezewish deleted the wenxuan/find_by_prefix branch June 1, 2026 09:01
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