Functions necessary for caching dependencies and build outputs to improve workflow execution time
npx @tessl/cli install tessl/npm-actions--cache@4.0.0Actions Cache provides essential caching functionality for GitHub Actions workflows, enabling developers to cache dependencies and build outputs to significantly improve workflow execution time. It offers functions for saving and restoring caches using keys and restore keys, with automatic compression using zstandard or gzip algorithms.
npm install @actions/cacheimport {
restoreCache,
saveCache,
isFeatureAvailable,
getUploadOptions,
getDownloadOptions,
ValidationError,
ReserveCacheError,
type UploadOptions,
type DownloadOptions
} from '@actions/cache';For CommonJS:
const {
restoreCache,
saveCache,
isFeatureAvailable,
getUploadOptions,
getDownloadOptions,
ValidationError,
ReserveCacheError
} = require('@actions/cache');import { restoreCache, saveCache } from '@actions/cache';
// Define paths and cache key
const paths = [
'node_modules',
'packages/*/node_modules/'
];
const key = 'npm-deps-' + hashFiles('**/package-lock.json');
const restoreKeys = [
'npm-deps-',
'npm-'
];
// Restore cache if it exists
const cacheKey = await restoreCache(paths, key, restoreKeys);
if (cacheKey) {
console.log(`Cache restored from key: ${cacheKey}`);
} else {
console.log('No cache found');
}
// Install dependencies if cache miss
if (!cacheKey) {
// ... run npm install or similar
// Save cache for future runs
const cacheId = await saveCache(paths, key);
console.log(`Cache saved with ID: ${cacheId}`);
}Actions Cache is built around several key components:
Core cache functionality for saving and restoring files using GitHub's caching service.
function restoreCache(
paths: string[],
primaryKey: string,
restoreKeys?: string[],
options?: DownloadOptions,
enableCrossOsArchive?: boolean = false
): Promise<string | undefined>;
function saveCache(
paths: string[],
key: string,
options?: UploadOptions,
enableCrossOsArchive?: boolean = false
): Promise<number>;
function isFeatureAvailable(): boolean;Upload and download options for controlling cache behavior, compression, timeouts, and Azure SDK usage.
interface UploadOptions {
useAzureSdk?: boolean;
uploadConcurrency?: number;
uploadChunkSize?: number;
archiveSizeBytes?: number;
}
interface DownloadOptions {
useAzureSdk?: boolean;
downloadConcurrency?: number;
concurrentBlobDownloads?: boolean;
timeoutInMs?: number;
segmentTimeoutInMs?: number;
lookupOnly?: boolean;
}
function getUploadOptions(copy?: UploadOptions): UploadOptions;
function getDownloadOptions(copy?: DownloadOptions): DownloadOptions;Custom error types for validation failures and cache reservation conflicts.
class ValidationError extends Error {
constructor(message: string);
}
class ReserveCacheError extends Error {
constructor(message: string);
}enum CompressionMethod {
Gzip = 'gzip',
ZstdWithoutLong = 'zstd-without-long',
Zstd = 'zstd'
}
enum CacheFilename {
Gzip = 'cache.tgz',
Zstd = 'cache.tzst'
}
enum ArchiveToolType {
GNU = 'gnu',
BSD = 'bsd'
}
const CacheFileSizeLimit = 10737418240; // 10 * Math.pow(1024, 3) - 10GiB per repository
const DefaultRetryAttempts = 2;
const DefaultRetryDelay = 5000; // 5000ms
const SocketTimeout = 5000; // 5000ms
const TarFilename = 'cache.tar';
const ManifestFilename = 'manifest.txt';