Skip to content

Commit

Permalink
coordinate post and main hashing method
Browse files Browse the repository at this point in the history
  • Loading branch information
rarmatei committed Jan 10, 2025
1 parent 6845bfb commit 63593de
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 19 deletions.
2 changes: 1 addition & 1 deletion workflow-steps/cache/hashing-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export function buildCachePaths(
inputPaths: string,
warnInvalidPaths: boolean = true,
) {
const directories = Array.from(
const directories: string[] = Array.from(
new Set(
inputPaths
.split('\n')
Expand Down
19 changes: 14 additions & 5 deletions workflow-steps/cache/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,14 @@ if (!inputKey || !inputPaths) {

const paths = buildCachePaths(inputPaths, false);
const stringifiedPaths = paths.join(',');
const key = hashKey(`${inputKey}|"${stringifiedPaths}"`);
const currentBranchKeys = [key].map((k) => `${currentBranch}-${k}`);
const baseBranchKeys = baseBranch ? [key].map((k) => `${baseBranch}-${k}`) : [];
const key = `${inputKey} | "${stringifiedPaths}"`;
const hashedKey = hashKey(key);
const currentBranchKeys = [hashedKey].map((k) => `${currentBranch}-${k}`);
const baseBranchKeys = baseBranch
? [hashedKey].map((k) => `${baseBranch}-${k}`)
: [];

console.log(`Expanded unhashed key is: ${key}\n`);

cacheClient
.restore(
Expand All @@ -38,10 +43,14 @@ cacheClient
)
.then((resp: RestoreResponse) => {
if (resp.success) {
console.log('Found cache entry on hashed key: ' + resp.key);
console.log('Found cache entry on: ' + resp.key);
rememberCacheRestorationForPostStep();
} else {
console.log('Cache miss on hashed key: ' + key);
console.log(`Cache miss on:`);
console.log(`- ${currentBranch}-${hashedKey}`);
if (baseBranch && currentBranch !== baseBranch) {
console.log(`- ${baseBranch}-${hashedKey}`);
}
}
});

Expand Down
17 changes: 12 additions & 5 deletions workflow-steps/cache/output/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6041,19 +6041,26 @@ if (!inputKey || !inputPaths) {
}
var paths = buildCachePaths(inputPaths, false);
var stringifiedPaths = paths.join(",");
var key = hashKey(`${inputKey}|"${stringifiedPaths}"`);
var currentBranchKeys = [key].map((k) => `${currentBranch}-${k}`);
var baseBranchKeys = baseBranch ? [key].map((k) => `${baseBranch}-${k}`) : [];
var key = `${inputKey} | "${stringifiedPaths}"`;
var hashedKey = hashKey(key);
var currentBranchKeys = [hashedKey].map((k) => `${currentBranch}-${k}`);
var baseBranchKeys = baseBranch ? [hashedKey].map((k) => `${baseBranch}-${k}`) : [];
console.log(`Expanded unhashed key is: ${key}
`);
cacheClient.restore(
new RestoreRequest({
keys: [...currentBranchKeys, ...baseBranchKeys]
})
).then((resp) => {
if (resp.success) {
console.log("Found cache entry on hashed key: " + resp.key);
console.log("Found cache entry on: " + resp.key);
rememberCacheRestorationForPostStep();
} else {
console.log("Cache miss on hashed key: " + key);
console.log(`Cache miss on:`);
console.log(`- ${currentBranch}-${hashedKey}`);
if (baseBranch && currentBranch !== baseBranch) {
console.log(`- ${baseBranch}-${hashedKey}`);
}
}
});
function rememberCacheRestorationForPostStep() {
Expand Down
10 changes: 6 additions & 4 deletions workflow-steps/cache/output/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -6028,13 +6028,15 @@ if (!!cacheWasHit) {
);
const paths = buildCachePaths(inputPaths);
const stringifiedPaths = paths.join(",");
const key = hashKey(`${inputKey}|${stringifiedPaths}`);
console.log("Storing the following directories..\n" + paths.join("\n"));
const key = `${inputKey} | "${stringifiedPaths}"`;
const hashedKey = hashKey(key);
console.log(`Expanded unhashed cache key is: ${key}`);
console.log("Storing the following directories:\n" + paths.join("\n"));
console.log(`
Using key..${key}`);
under hash: ${process.env.NX_BRANCH}-${hashedKey}`);
cacheClient.storeV2(
new StoreRequest({
key,
key: hashedKey,
paths
})
).then((r) => {
Expand Down
10 changes: 6 additions & 4 deletions workflow-steps/cache/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,17 @@ if (!!cacheWasHit) {

const paths = buildCachePaths(inputPaths);
const stringifiedPaths = paths.join(',');
const key = hashKey(`${inputKey}|${stringifiedPaths}`);
const key = `${inputKey} | "${stringifiedPaths}"`;
const hashedKey = hashKey(key);

console.log('Storing the following directories..\n' + paths.join('\n'));
console.log(`\nUsing key..${key}`);
console.log(`Expanded unhashed cache key is: ${key}`);
console.log('Storing the following directories:\n' + paths.join('\n'));
console.log(`\nunder hash: ${process.env.NX_BRANCH}-${hashedKey}`);

cacheClient
.storeV2(
new StoreRequest({
key,
key: hashedKey,
paths,
}),
)
Expand Down

0 comments on commit 63593de

Please sign in to comment.