-
Notifications
You must be signed in to change notification settings - Fork 3k
Expand file tree
/
Copy pathdockerhub-cache.ts
More file actions
114 lines (88 loc) · 2.7 KB
/
dockerhub-cache.ts
File metadata and controls
114 lines (88 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import { dequal } from 'dequal';
import { DateTime } from 'luxon';
import * as packageCache from '../../../util/cache/package/index.ts';
import type { DockerHubTag } from './schema.ts';
export interface DockerHubCacheData {
items: Record<number, DockerHubTag>;
updatedAt: string | null;
}
const cacheNamespace = 'datasource-docker-hub-cache';
export class DockerHubCache {
private isChanged = false;
private reconciledIds = new Set<number>();
private dockerRepository: string;
private cache: DockerHubCacheData;
private constructor(dockerRepository: string, cache: DockerHubCacheData) {
this.dockerRepository = dockerRepository;
this.cache = cache;
}
static async init(dockerRepository: string): Promise<DockerHubCache> {
let repoCache = await packageCache.get<DockerHubCacheData>(
cacheNamespace,
dockerRepository,
);
repoCache ??= {
items: {},
updatedAt: null,
};
return new DockerHubCache(dockerRepository, repoCache);
}
reconcile(items: DockerHubTag[], expectedCount: number): boolean {
let needNextPage = true;
let earliestDate = null;
let { updatedAt } = this.cache;
let latestDate = updatedAt ? DateTime.fromISO(updatedAt) : null;
for (const newItem of items) {
const id = newItem.id;
this.reconciledIds.add(id);
const oldItem = this.cache.items[id];
const itemDate = DateTime.fromISO(newItem.last_updated);
if (!earliestDate || earliestDate > itemDate) {
earliestDate = itemDate;
}
if (!latestDate || latestDate < itemDate) {
latestDate = itemDate;
updatedAt = newItem.last_updated;
}
if (dequal(oldItem, newItem)) {
needNextPage = false;
continue;
}
this.cache.items[newItem.id] = newItem;
this.isChanged = true;
}
this.cache.updatedAt = updatedAt;
if (earliestDate && latestDate) {
for (const [key, item] of Object.entries(this.cache.items)) {
const id = parseInt(key, 10);
const itemDate = DateTime.fromISO(item.last_updated);
if (
itemDate < earliestDate ||
itemDate > latestDate ||
this.reconciledIds.has(id)
) {
continue;
}
delete this.cache.items[id];
this.isChanged = true;
}
if (Object.keys(this.cache.items).length > expectedCount) {
return true;
}
}
return needNextPage;
}
async save(): Promise<void> {
if (this.isChanged) {
await packageCache.set(
cacheNamespace,
this.dockerRepository,
this.cache,
3 * 60 * 24 * 30,
);
}
}
getItems(): DockerHubTag[] {
return Object.values(this.cache.items);
}
}