Skip to content

Commit

Permalink
add envApi cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Feverqwe committed Sep 20, 2023
1 parent bb299fd commit 067cc14
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 16 deletions.
6 changes: 3 additions & 3 deletions src/services/cache/cacheFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,10 @@ class CacheFile {
return true;

Check warning on line 151 in src/services/cache/cacheFile.ts

View workflow job for this annotation

GitHub Actions / Verify Files

Async method 'checkAsync' expected no return value
}

addFileDep({filename, content}: {filename: string; content: string}) {
const contentHash = CacheService.getHash(content);
addFileDep({filename, content}: {filename: string; content: string | Uint8Array}) {
if (this.data.fileDeps[filename]) return;

Check failure on line 155 in src/services/cache/cacheFile.ts

View workflow job for this annotation

GitHub Actions / Verify Files

Expected { after 'if' condition

this.data.fileDeps[filename] = contentHash;
this.data.fileDeps[filename] = CacheService.getHash(content);
}

addFileExists({filename, state}: {filename: string; state: boolean}) {
Expand Down
67 changes: 54 additions & 13 deletions src/utils/pluginEnvApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,15 @@ class PluginEnvApi {
return new PluginEnvApi(props);
}

readonly root: string;
readonly distRoot: string;
cacheFile: CacheFile | undefined;
public readonly root: string;

Check failure on line 29 in src/utils/pluginEnvApi.ts

View workflow job for this annotation

GitHub Actions / Verify Files

Public accessibility modifier on class property root
public readonly distRoot: string;

Check failure on line 30 in src/utils/pluginEnvApi.ts

View workflow job for this annotation

GitHub Actions / Verify Files

Public accessibility modifier on class property distRoot
public readonly cacheFile: CacheFile | undefined;

Check failure on line 31 in src/utils/pluginEnvApi.ts

View workflow job for this annotation

GitHub Actions / Verify Files

Public accessibility modifier on class property cacheFile

private asyncActionQueue: AsyncAction[] = [];
private readonly fileExistsCache = new Map<string, boolean>();
private readonly readFileCache = new Map<string, string | Uint8Array>();
private readonly mkdirCache = new Set();

private readonly asyncActionQueue: AsyncAction[] = [];

constructor({root, distRoot, cacheFile}: PluginEnvApiProps) {
this.root = root;
Expand All @@ -45,7 +49,7 @@ class PluginEnvApi {
const fullFrom = path.join(this.root, from);
const fullTo = path.join(this.distRoot, to);

fs.mkdirSync(path.dirname(fullTo), {recursive: true});
this.cacheMkdir(path.dirname(fullTo));
fs.copyFileSync(fullFrom, fullTo);
if (this.cacheFile) {
this.cacheFile.addCopyFile({from, to});
Expand All @@ -59,11 +63,11 @@ class PluginEnvApi {
this.asyncActionQueue.push({type: AsyncActionType.Copy, from, to});
}

readFile<T extends BufferEncoding>(rawTarget: string, encoding: T) {
readFile<T extends BufferEncoding>(rawTarget: string, encoding: T | null = null) {
const target = safeRelativePath(rawTarget);
const fullTarget = path.join(this.root, target);

const result = fs.readFileSync(fullTarget, encoding);
const result = this.cacheReadFileSync(fullTarget, encoding);
if (this.cacheFile) {
this.cacheFile.addFileDep({filename: target, content: result});
}
Expand All @@ -74,7 +78,7 @@ class PluginEnvApi {
const target = safeRelativePath(rawTarget);
const fullTarget = path.join(this.root, target);

const result = fs.existsSync(fullTarget);
const result = this.cacheExistsSync(fullTarget);
if (this.cacheFile) {
this.cacheFile.addFileExists({filename: target, state: result});
}
Expand All @@ -85,7 +89,7 @@ class PluginEnvApi {
const to = safeRelativePath(rawTo);
const fullTo = path.join(this.distRoot, to);

fs.mkdirSync(path.dirname(fullTo), {recursive: true});
this.cacheMkdir(path.dirname(fullTo));
fs.writeFileSync(fullTo, data);
if (this.cacheFile) {
this.cacheFile.addWriteFile(to, data);
Expand Down Expand Up @@ -118,7 +122,7 @@ class PluginEnvApi {
const fullFrom = path.join(this.root, from);
const fullTo = path.join(this.distRoot, to);

fs.mkdirSync(path.dirname(fullTo), {recursive: true});
this.cacheMkdir(path.dirname(fullTo));
fs.copyFileSync(fullFrom, fullTo);
if (this.cacheFile) {
this.cacheFile.addCopyFile({from, to});
Expand All @@ -129,7 +133,7 @@ class PluginEnvApi {
const {to, data} = action;
const fullTo = path.join(this.distRoot, to);

fs.mkdirSync(path.dirname(fullTo), {recursive: true});
this.cacheMkdir(path.dirname(fullTo));
fs.writeFileSync(fullTo, data);
if (this.cacheFile) {
this.cacheFile.addWriteFile(to, data);
Expand All @@ -150,7 +154,7 @@ class PluginEnvApi {
const fullFrom = path.join(this.root, from);
const fullTo = path.join(this.distRoot, to);

await fs.promises.mkdir(path.dirname(fullTo), {recursive: true});
await this.cacheMkdirAsync(path.dirname(fullTo));
await fs.promises.copyFile(fullFrom, fullTo);
if (this.cacheFile) {
this.cacheFile.addCopyFile({from, to});
Expand All @@ -161,7 +165,7 @@ class PluginEnvApi {
const {to, data} = action;
const fullTo = path.join(this.distRoot, to);

await fs.promises.mkdir(path.dirname(fullTo), {recursive: true});
await this.cacheMkdirAsync(path.dirname(fullTo));
await fs.promises.writeFile(fullTo, data);
if (this.cacheFile) {
this.cacheFile.addWriteFile(to, data);
Expand All @@ -171,6 +175,43 @@ class PluginEnvApi {
}
}));
}

private cacheMkdir(path: string) {
const {mkdirCache} = this;
if (!mkdirCache.has(path)) {
fs.mkdirSync(path, {recursive: true});
mkdirCache.add(path);
}
}

private async cacheMkdirAsync(path: string) {
const {mkdirCache} = this;
if (!mkdirCache.has(path)) {
await fs.promises.mkdir(path, {recursive: true});
mkdirCache.add(path);
}
}

private cacheReadFileSync(filename: string, encoding: BufferEncoding | null = null) {
const {readFileCache} = this;
const key = `${filename}_${encoding}`;
let result = readFileCache.get(key);
if (result === undefined) {
result = fs.readFileSync(filename, encoding);
readFileCache.set(key, result);
}
return result;
}

private cacheExistsSync(filename: string) {
const {fileExistsCache} = this;
let result = fileExistsCache.get(filename);
if (result === undefined) {
result = fs.existsSync(filename);
fileExistsCache.set(filename, result);
}
return result;
}
}

export default PluginEnvApi;

0 comments on commit 067cc14

Please sign in to comment.