Skip to content

Commit

Permalink
feat(cache): change key to match actions/cache documentation
Browse files Browse the repository at this point in the history
Signed-off-by: Matthieu MOREL <[email protected]>
  • Loading branch information
mmorel-35 authored and voxpelli committed Feb 27, 2023
1 parent 7c29869 commit 6a898d2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
6 changes: 3 additions & 3 deletions __tests__/cache-restore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,10 @@ describe('cache-restore', () => {
await restoreCache(packageManager);
expect(hashFilesSpy).toHaveBeenCalled();
expect(infoSpy).toHaveBeenCalledWith(
`Cache restored from key: node-cache-${platform}-${packageManager}-${fileHash}`
`Cache restored from key: ${platform}-setup-node-${packageManager}-${fileHash}`
);
expect(infoSpy).not.toHaveBeenCalledWith(
`${packageManager} cache is not found`
`Cache not found for input keys: ${platform}-setup-node-${packageManager}-${fileHash}, ${platform}-setup-node-${packageManager}-, ${platform}-setup-node-`
);
expect(setOutputSpy).toHaveBeenCalledWith('cache-hit', true);
}
Expand Down Expand Up @@ -166,7 +166,7 @@ describe('cache-restore', () => {
await restoreCache(packageManager);
expect(hashFilesSpy).toHaveBeenCalled();
expect(infoSpy).toHaveBeenCalledWith(
`${packageManager} cache is not found`
`Cache not found for input keys: ${platform}-setup-node-${packageManager}-${fileHash}, ${platform}-setup-node-${packageManager}-, ${platform}-setup-node-`
);
expect(setOutputSpy).toHaveBeenCalledWith('cache-hit', false);
}
Expand Down
8 changes: 5 additions & 3 deletions dist/setup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73044,13 +73044,15 @@ const restoreCache = (packageManager, cacheDependencyPath) => __awaiter(void 0,
if (!fileHash) {
throw new Error('Some specified paths were not resolved, unable to cache dependencies.');
}
const primaryKey = `node-cache-${platform}-${packageManager}-${fileHash}`;
const keyPrefix = `${platform}-setup-node-`;
const primaryKey = `${keyPrefix}${packageManager}-${fileHash}`;
const restoreKeys = [`${keyPrefix}${packageManager}-`, keyPrefix];
core.debug(`primary key is ${primaryKey}`);
core.saveState(constants_1.State.CachePrimaryKey, primaryKey);
const cacheKey = yield cache.restoreCache([cachePath], primaryKey);
const cacheKey = yield cache.restoreCache([cachePath], primaryKey, restoreKeys);
core.setOutput('cache-hit', Boolean(cacheKey));
if (!cacheKey) {
core.info(`${packageManager} cache is not found`);
core.info(`Cache not found for input keys: ${[primaryKey, ...restoreKeys].join(', ')}`);
return;
}
core.saveState(constants_1.State.CacheMatchedKey, cacheKey);
Expand Down
10 changes: 5 additions & 5 deletions src/cache-restore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ export const restoreCache = async (
'Some specified paths were not resolved, unable to cache dependencies.'
);
}

const primaryKey = `node-cache-${platform}-${packageManager}-${fileHash}`;
const keyPrefix = `${platform}-setup-node-`;
const primaryKey = `${keyPrefix}${packageManager}-${fileHash}`;
const restoreKeys = [`${keyPrefix}${packageManager}-`, keyPrefix];
core.debug(`primary key is ${primaryKey}`);

core.saveState(State.CachePrimaryKey, primaryKey);

const cacheKey = await cache.restoreCache([cachePath], primaryKey);
const cacheKey = await cache.restoreCache([cachePath], primaryKey, restoreKeys);
core.setOutput('cache-hit', Boolean(cacheKey));

if (!cacheKey) {
core.info(`${packageManager} cache is not found`);
core.info(`Cache not found for input keys: ${[primaryKey, ...restoreKeys].join(', ')}`);
return;
}

Expand Down

0 comments on commit 6a898d2

Please sign in to comment.