3
0
Fork 0
mirror of https://github.com/Swatinem/rust-cache synced 2026-07-18 12:25:47 +00:00

Add source-keyed target cache

This commit is contained in:
Gary Sassano 2026-06-14 00:45:34 +02:00
parent c106961fee
commit c020f6afef
No known key found for this signature in database
GPG key ID: F8FE82821AD22F8F
12 changed files with 420 additions and 141 deletions

View file

@ -2,7 +2,7 @@ import * as core from "@actions/core";
import { cleanTargetDir } from "./cleanup";
import { CacheConfig } from "./config";
import { getCacheProvider, reportError } from "./utils";
import { CacheProvider, getCacheProvider, reportError } from "./utils";
process.on("uncaughtException", (e) => {
core.error(e.message);
@ -20,11 +20,11 @@ async function run() {
}
try {
var cacheOnFailure = core.getInput("cache-on-failure").toLowerCase();
let cacheOnFailure = core.getInput("cache-on-failure").toLowerCase();
if (cacheOnFailure !== "true") {
cacheOnFailure = "false";
}
var lookupOnly = core.getInput("lookup-only").toLowerCase() === "true";
const lookupOnly = core.getInput("lookup-only").toLowerCase() === "true";
core.exportVariable("CACHE_ON_FAILURE", cacheOnFailure);
core.exportVariable("CARGO_INCREMENTAL", 0);
@ -34,36 +34,42 @@ async function run() {
core.info("");
core.info(`... ${lookupOnly ? "Checking" : "Restoring"} cache ...`);
const key = config.cacheKey;
// Pass a copy of cachePaths to avoid mutating the original array as reported by:
// https://github.com/actions/toolkit/pull/1378
// TODO: remove this once the underlying bug is fixed.
const restoreKey = await cacheProvider.cache.restoreCache(config.cachePaths.slice(), key, [config.restoreKey], {
lookupOnly,
});
if (restoreKey) {
const match =
restoreKey.localeCompare(key, undefined, {
sensitivity: "accent",
}) === 0;
core.info(`${lookupOnly ? "Found" : "Restored from"} cache key "${restoreKey}" full match: ${match}.`);
if (!match) {
// pre-clean the target directory on cache mismatch
for (const workspace of config.workspaces) {
try {
await cleanTargetDir(workspace.target, [], true);
} catch {}
}
const cacheResult = await restoreCache(cacheProvider, config.cachePaths, config.cacheKey, [config.restoreKey], lookupOnly);
config.cacheNeedsSave = !cacheResult.match;
if (config.targetCacheEnabled) {
if (cacheResult.found && !cacheResult.match) {
// pre-clean the target directory on cargo cache mismatch before restoring target cache
await cleanTargets(config);
}
// We restored the cache but it is not a full match.
const targetResult = await restoreCache(
cacheProvider,
config.targetCachePaths,
config.targetCacheKey,
config.targetRestoreKeys,
lookupOnly,
"target",
);
config.targetCacheNeedsSave = !targetResult.match;
if (targetResult.found && !targetResult.match) {
// pre-clean the target directory on target cache mismatch
await cleanTargets(config);
}
if (!cacheResult.match || !targetResult.match) {
config.saveState();
}
setCacheHitOutput(match);
setCacheHitOutput(cacheResult.match && targetResult.match);
} else if (cacheResult.match) {
setCacheHitOutput(true);
} else {
core.info("No cache found.");
config.saveState();
if (cacheResult.found) {
// pre-clean the target directory on cache mismatch
await cleanTargets(config);
}
config.saveState();
setCacheHitOutput(false);
}
} catch (e) {
@ -78,4 +84,45 @@ function setCacheHitOutput(cacheHit: boolean): void {
core.setOutput("cache-hit", cacheHit.toString());
}
async function restoreCache(
cacheProvider: CacheProvider,
paths: string[],
key: string,
restoreKeys: string[],
lookupOnly: boolean,
name = "",
): Promise<RestoreResult> {
const label = name ? `${name} cache` : "cache";
// Pass a copy of cachePaths to avoid mutating the original array as reported by:
// https://github.com/actions/toolkit/pull/1378
// TODO: remove this once the underlying bug is fixed.
const restoreKey = await cacheProvider.cache.restoreCache(paths.slice(), key, restoreKeys, {
lookupOnly,
});
if (!restoreKey) {
core.info(`No ${label} found.`);
return { found: false, match: false };
}
const match =
restoreKey.localeCompare(key, undefined, {
sensitivity: "accent",
}) === 0;
core.info(`${lookupOnly ? "Found" : "Restored from"} ${label} key "${restoreKey}" full match: ${match}.`);
return { found: true, match };
}
interface RestoreResult {
found: boolean;
match: boolean;
}
async function cleanTargets(config: CacheConfig) {
for (const workspace of config.workspaces) {
try {
await cleanTargetDir(workspace.target, [], true);
} catch {}
}
}
run();