1
0
Fork 0
mirror of https://github.com/rharkor/caching-for-turbo.git synced 2025-06-09 09:35:17 +09:00

fix: cache

This commit is contained in:
rharkor 2024-06-25 18:11:47 +02:00
parent f68c7847c0
commit fda8aa2d8a
3 changed files with 11 additions and 4 deletions

View file

@ -8,7 +8,7 @@ import {
statSync
} from 'node:fs'
import { getCacheClient } from './utils'
import { cacheVersion, getCacheKey } from '../constants'
import { cacheVersion, getCacheKey, getFsCachePath } from '../constants'
type RequestContext = {
log: {
@ -27,7 +27,7 @@ export async function saveCache(
ctx.log.info(
`Using filesystem cache because cache API env vars are not set`
)
await pipeline(stream, createWriteStream(`/tmp/${hash}.tg.bin`))
await pipeline(stream, createWriteStream(getFsCachePath(hash)))
return
}
const client = getCacheClient()
@ -60,12 +60,14 @@ export async function getCache(
): Promise<
[number | undefined, Readable | ReadableStream, string | undefined] | null
> {
//* Get cache from filesystem if cache API env vars are not set
if (!env.valid) {
const path = `/tmp/${hash}.tg.bin`
const path = getFsCachePath(hash)
if (!existsSync(path)) return null
const size = statSync(path).size
return [size, createReadStream(path), undefined]
}
//* Get cache from cache API
const client = getCacheClient()
const cacheKey = getCacheKey(hash)
const { data } = await client.query(cacheKey, cacheVersion)

View file

@ -5,6 +5,7 @@ import * as cacheHttpClient from '@actions/cache/lib/internal/cacheHttpClient'
import streamToPromise from 'stream-to-promise'
import { createWriteStream } from 'node:fs'
import { unlink } from 'node:fs/promises'
import { getTempCachePath } from '../constants'
class HandledError extends Error {
status: number
@ -69,7 +70,7 @@ export function getCacheClient() {
const save = async (id: number, stream: Readable): Promise<void> => {
try {
//* Create a temporary file to store the cache
const tempFile = `/tmp/cache-${id}.tg.bin`
const tempFile = getTempCachePath(id)
const writeStream = createWriteStream(tempFile)
await streamToPromise(stream.pipe(writeStream))
core.info(`Saved cache to ${tempFile}`)

View file

@ -10,3 +10,7 @@ export const getCacheKey = (hash: string, tag?: string): string =>
export const serverLogFile = env.RUNNER_TEMP
? join(env.RUNNER_TEMP, 'turbogha.log')
: '/tmp/turbogha.log'
export const getFsCachePath = (hash: string): string =>
join(env.RUNNER_TEMP || '/tmp', `${hash}.tg.bin`)
export const getTempCachePath = (id: number): string =>
join(env.RUNNER_TEMP || '/tmp', `cache-${id}.tg.bin`)