Skip to content
This repository has been archived by the owner on May 22, 2024. It is now read-only.

Commit

Permalink
chore: move cache into class
Browse files Browse the repository at this point in the history
  • Loading branch information
danez committed Nov 3, 2022
1 parent 9cc7e59 commit 531e3b8
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 27 deletions.
8 changes: 4 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { FunctionSource } from './function.js'
import { getFunctionFromPath, getFunctionsFromPaths } from './runtimes/index.js'
import { findISCDeclarationsInPath, ISCValues } from './runtimes/node/in_source_config/index.js'
import { GetSrcFilesFunction, RuntimeType } from './runtimes/runtime.js'
import { createNewCache } from './utils/cache.js'
import { RuntimeCache } from './utils/cache.js'
import { listFunctionsDirectories, resolveFunctionsDirectories } from './utils/fs.js'

export { zipFunction, zipFunctions } from './zip.js'
Expand Down Expand Up @@ -61,7 +61,7 @@ export const listFunctions = async function (
const featureFlags = getFlags(inputFeatureFlags)
const srcFolders = resolveFunctionsDirectories(relativeSrcFolders)
const paths = await listFunctionsDirectories(srcFolders)
const cache = createNewCache()
const cache = new RuntimeCache()
const functionsMap = await getFunctionsFromPaths(paths, { cache, config, featureFlags })
const functions = [...functionsMap.values()]
const augmentedFunctions = parseISC ? await Promise.all(functions.map(augmentWithISC)) : functions
Expand All @@ -79,7 +79,7 @@ export const listFunction = async function (
}: { featureFlags?: FeatureFlags; config?: Config; parseISC?: boolean } = {},
) {
const featureFlags = getFlags(inputFeatureFlags)
const cache = createNewCache()
const cache = new RuntimeCache()
const func = await getFunctionFromPath(path, { cache, config, featureFlags })

if (!func) {
Expand All @@ -99,7 +99,7 @@ export const listFunctionsFiles = async function (
const featureFlags = getFlags(inputFeatureFlags)
const srcFolders = resolveFunctionsDirectories(relativeSrcFolders)
const paths = await listFunctionsDirectories(srcFolders)
const cache = createNewCache()
const cache = new RuntimeCache()
const functionsMap = await getFunctionsFromPaths(paths, { cache, config, featureFlags })
const functions = [...functionsMap.values()]
const augmentedFunctions = parseISC ? await Promise.all(functions.map(augmentWithISC)) : functions
Expand Down
2 changes: 1 addition & 1 deletion src/runtimes/go/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const detectGoFunction = async ({ cache, path }: { cache: RuntimeCache; path: st

const directoryName = basename(path)

const files = await cachedReaddir(cache.readDirCache, path)
const files = await cachedReaddir(cache.readdirCache, path)
const mainFileName = [`${directoryName}.go`, 'main.go'].find((name) => files.includes(name))

if (mainFileName === undefined) {
Expand Down
2 changes: 1 addition & 1 deletion src/runtimes/rust/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const detectRustFunction = async ({ cache, path }: { cache: RuntimeCache; path:
return
}

const files = await cachedReaddir(cache.readDirCache, path)
const files = await cachedReaddir(cache.readdirCache, path)
const hasCargoManifest = files.includes(MANIFEST_NAME)

if (!hasCargoManifest) {
Expand Down
33 changes: 15 additions & 18 deletions src/utils/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,27 @@ export type ReaddirCache = Map<string, Promise<string[]>>

interface NFTCache {
fileCache: FileCache
statCache: Map<string, unknown>
symlinkCache: Map<string, unknown>
analysisCache: Map<string, unknown>
// nft actually sets even more properties on this object, but
// they do not have any relevance for use here
}

export interface RuntimeCache {
// file content
export class RuntimeCache {
// Cache for fs.readFile() calls
fileCache: FileCache
// Cache for fs.lstat() calls
lstatCache: LstatCache
readDirCache: ReaddirCache
// Cache fs.readdir calls
readdirCache: ReaddirCache
// NFT cache, which should not be used in zisi and only supplied to NFT
// this cache shares the file cache with zisi
nftCache: Partial<NFTCache>
}

export const createNewCache = (): RuntimeCache => {
const cache: RuntimeCache = Object.create(null)

cache.fileCache = new Map()
cache.lstatCache = new Map()
cache.readDirCache = new Map()
nftCache: NFTCache

cache.nftCache = Object.create(null)
cache.nftCache.fileCache = cache.fileCache
constructor() {
this.fileCache = new Map()
this.lstatCache = new Map()
this.readdirCache = new Map()

return cache
this.nftCache = Object.create(null)
this.nftCache.fileCache = this.fileCache
}
}
6 changes: 3 additions & 3 deletions src/zip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { createManifest } from './manifest.js'
import { getFunctionsFromPaths } from './runtimes/index.js'
import { ModuleFormat } from './runtimes/node/utils/module_format.js'
import { addArchiveSize } from './utils/archive_size.js'
import { createNewCache } from './utils/cache.js'
import { RuntimeCache } from './utils/cache.js'
import { formatZipResult } from './utils/format_result.js'
import { listFunctionsDirectories, resolveFunctionsDirectories } from './utils/fs.js'
import { nonNullable } from './utils/non_nullable.js'
Expand Down Expand Up @@ -58,7 +58,7 @@ export const zipFunctions = async function (
) {
validateArchiveFormat(archiveFormat)

const cache = createNewCache()
const cache = new RuntimeCache()
const featureFlags = getFlags(inputFeatureFlags)
const srcFolders = resolveFunctionsDirectories(relativeSrcFolders)
const [paths] = await Promise.all([listFunctionsDirectories(srcFolders), fs.mkdir(destFolder, { recursive: true })])
Expand Down Expand Up @@ -134,7 +134,7 @@ export const zipFunction = async function (

const featureFlags = getFlags(inputFeatureFlags)
const srcPath = resolve(relativeSrcPath)
const cache = createNewCache()
const cache = new RuntimeCache()
const functions = await getFunctionsFromPaths([srcPath], { cache, config: inputConfig, dedupe: true, featureFlags })

if (functions.size === 0) {
Expand Down

0 comments on commit 531e3b8

Please sign in to comment.