Skip to content

Commit cbe3ec3

Browse files
committed
Simplifies cache fallback + add timed invalidation
1 parent 6a898d2 commit cbe3ec3

File tree

5 files changed

+21
-9
lines changed

5 files changed

+21
-9
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ The action defaults to search for the dependency file (`package-lock.json`, `npm
6363

6464
**Note:** The action does not cache `node_modules`
6565

66+
Use `cache-invalidate-after-days` to change the default fallback cache invalidation of every 120 days. Set to 0 to deactivate.
67+
6668
See the examples of using cache for `yarn`/`pnpm` and `cache-dependency-path` input in the [Advanced usage](docs/advanced-usage.md#caching-packages-data) guide.
6769

6870
**Caching npm dependencies:**

__tests__/cache-restore.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,13 @@ describe('cache-restore', () => {
132132
}
133133
});
134134

135-
await restoreCache(packageManager);
135+
await restoreCache(packageManager, undefined, '0');
136136
expect(hashFilesSpy).toHaveBeenCalled();
137137
expect(infoSpy).toHaveBeenCalledWith(
138-
`Cache restored from key: ${platform}-setup-node-${packageManager}-${fileHash}`
138+
`Cache restored from key: ${platform}-0-setup-node-${packageManager}-${fileHash}`
139139
);
140140
expect(infoSpy).not.toHaveBeenCalledWith(
141-
`Cache not found for input keys: ${platform}-setup-node-${packageManager}-${fileHash}, ${platform}-setup-node-${packageManager}-, ${platform}-setup-node-`
141+
`Cache not found for input keys: ${platform}-0-setup-node-${packageManager}-${fileHash}, ${platform}-0-setup-node-${packageManager}-`
142142
);
143143
expect(setOutputSpy).toHaveBeenCalledWith('cache-hit', true);
144144
}
@@ -163,10 +163,10 @@ describe('cache-restore', () => {
163163
});
164164

165165
restoreCacheSpy.mockImplementationOnce(() => undefined);
166-
await restoreCache(packageManager);
166+
await restoreCache(packageManager, undefined, '0');
167167
expect(hashFilesSpy).toHaveBeenCalled();
168168
expect(infoSpy).toHaveBeenCalledWith(
169-
`Cache not found for input keys: ${platform}-setup-node-${packageManager}-${fileHash}, ${platform}-setup-node-${packageManager}-, ${platform}-setup-node-`
169+
`Cache not found for input keys: ${platform}-0-setup-node-${packageManager}-${fileHash}, ${platform}-0-setup-node-${packageManager}-`
170170
);
171171
expect(setOutputSpy).toHaveBeenCalledWith('cache-hit', false);
172172
}

action.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ inputs:
2525
description: 'Used to specify a package manager for caching in the default directory. Supported values: npm, yarn, pnpm.'
2626
cache-dependency-path:
2727
description: 'Used to specify the path to a dependency file: package-lock.json, yarn.lock, etc. Supports wildcards or a list of file names for caching multiple dependencies.'
28+
cache-invalidate-after-days:
29+
description: 'Used to control how often the fallback cache is invalidated automatically.'
2830
# TODO: add input to control forcing to pull from cloud or dist.
2931
# escape valve for someone having issues or needing the absolute latest which isn't cached yet
3032
outputs:

src/cache-restore.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import {
1313

1414
export const restoreCache = async (
1515
packageManager: string,
16-
cacheDependencyPath?: string
16+
cacheDependencyPath?: string,
17+
cacheInvalidateAfterDays?: string
1718
) => {
1819
const packageManagerInfo = await getPackageManagerInfo(packageManager);
1920
if (!packageManagerInfo) {
@@ -35,9 +36,15 @@ export const restoreCache = async (
3536
'Some specified paths were not resolved, unable to cache dependencies.'
3637
);
3738
}
38-
const keyPrefix = `${platform}-setup-node-`;
39+
const numericCacheInvalidateAfterDays = cacheInvalidateAfterDays && cacheInvalidateAfterDays === '0'
40+
? 0
41+
: (parseInt(cacheInvalidateAfterDays || '', 10) || 120)
42+
const timedInvalidationPrefix = numericCacheInvalidateAfterDays
43+
? Math.floor(Date.now() / (1000 * 60 * 60 * 24 * numericCacheInvalidateAfterDays)) % 1000 // % 1000 to get a rolling prefix between 0 and 999 rather than a possibly infinitely large
44+
: 0;
45+
const keyPrefix = `${platform}-${timedInvalidationPrefix}-setup-node-`;
3946
const primaryKey = `${keyPrefix}${packageManager}-${fileHash}`;
40-
const restoreKeys = [`${keyPrefix}${packageManager}-`, keyPrefix];
47+
const restoreKeys = [`${keyPrefix}${packageManager}-`];
4148
core.debug(`primary key is ${primaryKey}`);
4249
core.saveState(State.CachePrimaryKey, primaryKey);
4350

src/main.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ export async function run() {
6161

6262
if (cache && isCacheFeatureAvailable()) {
6363
const cacheDependencyPath = core.getInput('cache-dependency-path');
64-
await restoreCache(cache, cacheDependencyPath);
64+
const cacheInvalidateAfterDays = core.getInput('cache-invalidate-after-days');
65+
await restoreCache(cache, cacheDependencyPath, cacheInvalidateAfterDays);
6566
}
6667

6768
const matchersPath = path.join(__dirname, '../..', '.github');

0 commit comments

Comments
 (0)