Skip to content

Commit

Permalink
chore: use 8 character hash components (#143)
Browse files Browse the repository at this point in the history
Use 8 character hash components to reduce the key length, making it
more readable.

Fixes #97
  • Loading branch information
stevenh authored May 19, 2023
1 parent ad97570 commit bb80d0f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 2.3.1

- Fix cache key stability.
- Use 8 character hash components to reduce the key length, making it more readable.

## 2.3.0

Expand Down
14 changes: 12 additions & 2 deletions dist/restore/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60028,6 +60028,7 @@ class Workspace {
const HOME = external_os_default().homedir();
const config_CARGO_HOME = process.env.CARGO_HOME || external_path_default().join(HOME, ".cargo");
const STATE_CONFIG = "RUST_CACHE_CONFIG";
const HASH_LENGTH = 8;
class CacheConfig {
constructor() {
/** All the paths we want to cache */
Expand Down Expand Up @@ -60103,7 +60104,7 @@ class CacheConfig {
}
}
self.keyEnvs = keyEnvs;
key += `-${hasher.digest("hex")}`;
key += `-${digest(hasher)}`;
self.restoreKey = key;
// Construct the lockfiles portion of the key:
// This considers all the files found via globbing for various manifests
Expand Down Expand Up @@ -60132,7 +60133,7 @@ class CacheConfig {
hasher.update(chunk);
}
}
let lockHash = hasher.digest("hex");
let lockHash = digest(hasher);
self.keyFiles = keyFiles;
key += `-${lockHash}`;
self.cacheKey = key;
Expand Down Expand Up @@ -60214,6 +60215,15 @@ class CacheConfig {
function isCacheUpToDate() {
return core.getState(STATE_CONFIG) === "";
}
/**
* Returns a hex digest of the given hasher truncated to `HASH_LENGTH`.
*
* @param hasher The hasher to digest.
* @returns The hex digest.
*/
function digest(hasher) {
return hasher.digest("hex").substring(0, HASH_LENGTH);
}
async function getRustVersion() {
const stdout = await getCmdOutput("rustc", ["-vV"]);
let splits = stdout
Expand Down
14 changes: 12 additions & 2 deletions dist/save/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60028,6 +60028,7 @@ class Workspace {
const HOME = external_os_default().homedir();
const CARGO_HOME = process.env.CARGO_HOME || external_path_default().join(HOME, ".cargo");
const STATE_CONFIG = "RUST_CACHE_CONFIG";
const HASH_LENGTH = 8;
class CacheConfig {
constructor() {
/** All the paths we want to cache */
Expand Down Expand Up @@ -60103,7 +60104,7 @@ class CacheConfig {
}
}
self.keyEnvs = keyEnvs;
key += `-${hasher.digest("hex")}`;
key += `-${digest(hasher)}`;
self.restoreKey = key;
// Construct the lockfiles portion of the key:
// This considers all the files found via globbing for various manifests
Expand Down Expand Up @@ -60132,7 +60133,7 @@ class CacheConfig {
hasher.update(chunk);
}
}
let lockHash = hasher.digest("hex");
let lockHash = digest(hasher);
self.keyFiles = keyFiles;
key += `-${lockHash}`;
self.cacheKey = key;
Expand Down Expand Up @@ -60214,6 +60215,15 @@ class CacheConfig {
function isCacheUpToDate() {
return core.getState(STATE_CONFIG) === "";
}
/**
* Returns a hex digest of the given hasher truncated to `HASH_LENGTH`.
*
* @param hasher The hasher to digest.
* @returns The hex digest.
*/
function digest(hasher) {
return hasher.digest("hex").substring(0, HASH_LENGTH);
}
async function getRustVersion() {
const stdout = await getCmdOutput("rustc", ["-vV"]);
let splits = stdout
Expand Down
15 changes: 13 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const HOME = os.homedir();
export const CARGO_HOME = process.env.CARGO_HOME || path.join(HOME, ".cargo");

const STATE_CONFIG = "RUST_CACHE_CONFIG";
const HASH_LENGTH = 8;

export class CacheConfig {
/** All the paths we want to cache */
Expand Down Expand Up @@ -105,7 +106,7 @@ export class CacheConfig {

self.keyEnvs = keyEnvs;

key += `-${hasher.digest("hex")}`;
key += `-${digest(hasher)}`;

self.restoreKey = key;

Expand Down Expand Up @@ -144,7 +145,7 @@ export class CacheConfig {
hasher.update(chunk);
}
}
let lockHash = hasher.digest("hex");
let lockHash = digest(hasher);

self.keyFiles = keyFiles;

Expand Down Expand Up @@ -239,6 +240,16 @@ export function isCacheUpToDate(): boolean {
return core.getState(STATE_CONFIG) === "";
}

/**
* Returns a hex digest of the given hasher truncated to `HASH_LENGTH`.
*
* @param hasher The hasher to digest.
* @returns The hex digest.
*/
function digest(hasher: crypto.Hash): string {
return hasher.digest("hex").substring(0, HASH_LENGTH);
}

interface RustVersion {
host: string;
release: string;
Expand Down

0 comments on commit bb80d0f

Please sign in to comment.