Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions lib/util/cache/package/impl/file.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ describe('util/cache/package/impl/file', () => {

it('continues on cleanup errors', async () => {
await cache.set('_test-namespace', 'valid', 1234, 5);
await cacache.put(cacheFileName, 'cold-entry', 'some data');
const cacacheGet = vi
.spyOn(cacache, 'get')
.mockRejectedValue(new Error('error'));
Expand All @@ -170,5 +171,35 @@ describe('util/cache/package/impl/file', () => {

expect(cacacheGet).toHaveBeenCalled();
});

it('skips disk read for entry written this run', async () => {
await cache.set('_test-namespace', 'in-memory', 'value', 5);
const cacacheGet = vi.spyOn(cacache, 'get');

await cache.destroy();

const calledForKey = cacacheGet.mock.calls.some(
(args) => args[1] === '_test-namespace-in-memory',
);
expect(calledForKey).toBe(false);
const entries = await cacache.ls(cacheFileName);
expect(Object.keys(entries)).toContain('_test-namespace-in-memory');
});

it('skips disk read for expired entry written this run', async () => {
await cache.set('_test-namespace', 'expired-in-memory', 'value', -5);
const cacacheGet = vi.spyOn(cacache, 'get');

await cache.destroy();

const calledForKey = cacacheGet.mock.calls.some(
(args) => args[1] === '_test-namespace-expired-in-memory',
);
expect(calledForKey).toBe(false);
const entries = await cacache.ls(cacheFileName);
expect(Object.keys(entries)).not.toContain(
'_test-namespace-expired-in-memory',
);
});
});
});
20 changes: 20 additions & 0 deletions lib/util/cache/package/impl/file.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import cacache from 'cacache';
import { LRUCache } from 'lru-cache';
import { DateTime } from 'luxon';
import upath from 'upath';
import { logger } from '../../../../logger/index.ts';
Expand All @@ -15,6 +16,12 @@ export class PackageCacheFile extends PackageCacheBase {

private readonly cacheFileName: string;

private readonly expiryMap = new LRUCache<string, DateTime>({
// Assuming 50 bytes per entry, this limits the memory footprint of this
// to around 5MB.
max: 100000,
});

private constructor(cacheFileName: string) {
super();
this.cacheFileName = cacheFileName;
Expand Down Expand Up @@ -47,6 +54,7 @@ export class PackageCacheFile extends PackageCacheBase {
}

logger.trace({ namespace, key }, 'Returning cached value');
this.expiryMap.set(this.getKey(namespace, key), expiry);

if (!cached.compress) {
return cached.value;
Expand Down Expand Up @@ -76,6 +84,7 @@ export class PackageCacheFile extends PackageCacheBase {
expiry,
});
await cacache.put(this.cacheFileName, this.getKey(namespace, key), payload);
this.expiryMap.set(this.getKey(namespace, key), expiry);
}

override async destroy(): Promise<void> {
Expand All @@ -88,6 +97,17 @@ export class PackageCacheFile extends PackageCacheBase {
try {
totalCount += 1;
const cacheEntry = item as unknown as cacache.CacheObject;
const cachedExpiry = this.expiryMap.get(cacheEntry.key);
if (cachedExpiry !== undefined) {
if (DateTime.local() <= cachedExpiry) {
continue;
}
await cacache.rm.entry(this.cacheFileName, cacheEntry.key);
await cacache.rm.content(this.cacheFileName, cacheEntry.integrity);
this.expiryMap.delete(cacheEntry.key);
deletedCount += 1;
continue;
}
const entry = await cacache.get(this.cacheFileName, cacheEntry.key);
let cached: { expiry?: string } | undefined;
try {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@
"jsonata": "2.1.0",
"jsonc-weaver": "0.2.4",
"klona": "2.0.6",
"lru-cache": "11.3.5",
"luxon": "3.7.2",
"markdown-it": "14.1.1",
"markdown-table": "3.0.4",
Expand Down
17 changes: 10 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading