Skip to content

Move cache v1 and v2 implementations behind a CacheBackend interface - #2573

Open
diogotorres97 wants to merge 1 commit into
actions:mainfrom
diogotorres97:refactor/cache-backend-interface
Open

diogotorres97 wants to merge 1 commit into
actions:mainfrom
diogotorres97:refactor/cache-backend-interface

Conversation

@diogotorres97

@diogotorres97 diogotorres97 commented Nov 11, 2026 •

Copy link
Copy Markdown

Refactor only, no behaviour change. This is the first step proposed in #2572 (an optional, administrator-configured S3 storage backend for self-hosted runners).

What changes

restoreCache, saveCache and isFeatureAvailable in cache.ts each switched on the cache service version. The switch now lives in one place, getCacheBackend(version), which returns a CacheBackend:

export interface CacheBackend {
  isAvailable(): boolean
  restoreCache(paths, primaryKey, restoreKeys?, options?, enableCrossOsArchive?): Promise<string | undefined>
  saveCache(paths, key, options?, enableCrossOsArchive?): Promise<number>
}
  • internal/backends/v1.ts and internal/backends/v2.ts hold restoreCacheV1/saveCacheV1 and restoreCacheV2/saveCacheV2, moved verbatim (diffed against the original cache.ts), each with its own isAvailable() that holds the same env check as before.
  • cache.ts keeps the public entry points, path and key validation, and the ACTIONS_CACHE_MODE checks, then hands off to the backend.
  • The error classes and the cache write/read denied: prefixes move to internal/errors.ts, and checkPaths/checkKey move to internal/validation.ts. The backends need them, and importing them from cache.ts would create a circular import. cache.ts re-exports every error class and constant, so the public API and instanceof checks are unchanged: the exported names in lib/cache.d.ts match before and after.

Why

A new backend becomes one file implementing CacheBackend plus one case in getCacheBackend, without touching the existing v1/v2 paths. That keeps a follow-up S3 backend PR small and lets it be reviewed in isolation.

Testing

  • npm run format-check and eslint pass on the package, and tsc --noEmit passes.
  • jest packages/cache: all 150 existing tests pass, unchanged. A new cacheBackends.test.ts covers getCacheBackend dispatch (v1, v2, unknown version falling back to v1) and each backend's isAvailable(), for 155 in total.

@diogotorres97
diogotorres97 marked this pull request as ready for review November 11, 2026 18:45
@diogotorres97
diogotorres97 requested a review from a team as a code owner November 11, 2026 18:45

This branch has not been deployed

No deployments
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.

1 participant