This repository has been archived by the owner on Jan 23, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use cache when listing PRs and issues (#616)
* feat: use cache when listing PRs and issues * fix(deps): pin typescript to fix an unrelated issue * test: add tests * fix: use async-mutex
- Loading branch information
1 parent
a046b29
commit 6572a87
Showing
5 changed files
with
246 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
// Copyright 2022 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import {existsSync} from 'fs'; | ||
import {mkdir, readFile, stat, unlink, writeFile} from 'fs/promises'; | ||
import {tmpdir} from 'os'; | ||
import {join} from 'path'; | ||
import {Mutex} from 'async-mutex'; | ||
import {GitHubRepository, Issue, PullRequest} from './github'; | ||
|
||
const cacheDirectory = join(tmpdir(), 'google-repo-cache'); | ||
const cacheMaxAge = 60 * 60 * 1000; // 1 hour | ||
|
||
export type CachedData = {issues?: Issue[]; prs?: PullRequest[]}; | ||
export type CacheType = 'prs' | 'issues'; | ||
|
||
const mutex = new Mutex(); | ||
|
||
async function initCache() { | ||
if (!existsSync(cacheDirectory)) { | ||
await mkdir(cacheDirectory); | ||
} | ||
} | ||
|
||
function cacheFilename(repo: GitHubRepository, type: CacheType) { | ||
const owner = repo.repository.owner.login; | ||
const name = repo.repository.name; | ||
return join( | ||
cacheDirectory, | ||
`${owner}-${name}`.replace(/\W/g, '-') + `-${type}` | ||
); | ||
} | ||
|
||
export async function readFromCache(repo: GitHubRepository, type: CacheType) { | ||
const release = await mutex.acquire(); | ||
try { | ||
await initCache(); | ||
const cacheFile = cacheFilename(repo, type); | ||
if (!existsSync(cacheFile)) { | ||
return null; | ||
} | ||
const cacheStat = await stat(cacheFile); | ||
const mtime = cacheStat.mtimeMs ?? cacheStat.ctimeMs; | ||
const now = Date.now(); | ||
if (now - mtime >= cacheMaxAge) { | ||
await unlink(cacheFile); | ||
return null; | ||
} | ||
|
||
const content = await readFile(cacheFile); | ||
const json = JSON.parse(content.toString()) as CachedData; | ||
return json; | ||
} finally { | ||
release(); | ||
} | ||
} | ||
|
||
export async function saveToCache( | ||
repo: GitHubRepository, | ||
type: CacheType, | ||
data: CachedData | ||
) { | ||
const release = await mutex.acquire(); | ||
try { | ||
await initCache(); | ||
const cacheFile = cacheFilename(repo, type); | ||
if (!data.issues) { | ||
data.issues = []; | ||
} | ||
if (!data.prs) { | ||
data.prs = []; | ||
} | ||
const content = JSON.stringify(data, null, ' '); | ||
await writeFile(cacheFile, content); | ||
} finally { | ||
release(); | ||
} | ||
} | ||
|
||
export async function deleteCache(repo: GitHubRepository, type: CacheType) { | ||
const release = await mutex.acquire(); | ||
try { | ||
await initCache(); | ||
const cacheFile = cacheFilename(repo, type); | ||
if (existsSync(cacheFile)) { | ||
await unlink(cacheFile); | ||
} | ||
} finally { | ||
release(); | ||
} | ||
} |
Oops, something went wrong.