Skip to content

Commit 54c225f

Browse files
feat(releases): replace slow cron jobs by cache TTL
1 parent 664bb26 commit 54c225f

File tree

3 files changed

+5
-87
lines changed

3 files changed

+5
-87
lines changed

src/lib/server/github-cache.ts

Lines changed: 5 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ export type GitHubRelease = Awaited<
2020
* @see {@link https://docs.github.com/en/rest/releases/releases#list-releases|GitHub Docs}
2121
*/
2222
const per_page = 100;
23+
/**
24+
* The TTL of the cached values, in seconds.
25+
*/
26+
const cacheTTL = 60 * 15;
2327

2428
/**
2529
* A fetch layer to reach the GitHub API
@@ -80,65 +84,7 @@ export class GitHubCache {
8084
const releases = await this.#fetchReleases(repository);
8185

8286
await this.#redis.json.set(cacheKey, "$", releases);
83-
84-
return releases;
85-
}
86-
87-
/**
88-
* Add the given releases to the repository cache
89-
*
90-
* @param owner the owner of the cached GitHub repository to add the
91-
* new releases into
92-
* @param repo the name of the cached GitHub repository to add the
93-
* new releases into
94-
* @param newReleases the new releases to add to the cache; they will then be
95-
* de-duped from the already existing ones, and sorted from most recent to oldest
96-
* @returns all the cached releases after the new releases have been applied
97-
*/
98-
async addReleases(owner: string, repo: string, newReleases: GitHubRelease[]) {
99-
const cacheKey = this.#getRepoKey(owner, repo);
100-
101-
// Get existing releases
102-
const existingReleases = (await this.#redis.json.get<GitHubRelease[]>(cacheKey)) ?? [];
103-
104-
// Dedupe them by ID
105-
const existingIds = new Set(existingReleases.map(({ id }) => id));
106-
const uniqueNewReleases = newReleases.filter(({ id }) => !existingIds.has(id));
107-
108-
// Merge them all
109-
const updatedReleases = [...existingReleases, ...uniqueNewReleases];
110-
111-
// Sort them by most recent
112-
updatedReleases.sort(
113-
(a, b) =>
114-
new Date(b.published_at ?? b.created_at).getTime() -
115-
new Date(a.published_at ?? a.created_at).getTime()
116-
);
117-
118-
await this.#redis.json.set(cacheKey, "$", updatedReleases);
119-
120-
return updatedReleases;
121-
}
122-
123-
/**
124-
* Fetch the latest releases for the given repository and add them
125-
* to the cache via {@link addReleases}
126-
*
127-
* @param owner the owner of the GitHub repository to fetch and cache
128-
* the releases for
129-
* @param repo the name of the GitHub repository to fetch and cache
130-
* the releases for
131-
* @returns the fetched releases
132-
*/
133-
async fetchAndCacheReleases(owner: string, repo: string) {
134-
const { data: releases } = await this.#octokit.rest.repos.listReleases({
135-
owner,
136-
repo,
137-
per_page
138-
});
139-
140-
// Ajouter au cache pour le repo
141-
await this.addReleases(owner, repo, releases);
87+
await this.#redis.expire(cacheKey, cacheTTL);
14288

14389
return releases;
14490
}

src/routes/cron/+server.ts

Lines changed: 0 additions & 20 deletions
This file was deleted.

vercel.json

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)