Finish fs cache changes / tracer optimization

This commit is contained in:
SukkaW
2024-02-02 11:21:16 +08:00
parent 8428b3da42
commit efa1ab254e
14 changed files with 77 additions and 65 deletions

View File

@@ -25,19 +25,17 @@ export interface CacheOptions<S = string> {
type?: S extends string ? 'string' : 'buffer'
}
interface CacheApplyNonStringOption<T, S = string> {
interface CacheApplyRawOption {
ttl?: number | null,
temporaryBypass?: boolean
}
interface CacheApplyNonRawOption<T, S> extends CacheApplyRawOption {
serializer: (value: T) => S,
deserializer: (cached: S) => T,
temporaryBypass?: boolean
deserializer: (cached: S) => T
}
interface CacheApplyStringOption {
ttl?: number | null,
temporaryBypass?: boolean
}
type CacheApplyOption<T, S = string> = T extends string ? CacheApplyStringOption : CacheApplyNonStringOption<T, S>;
type CacheApplyOption<T, S> = T extends S ? CacheApplyRawOption : CacheApplyNonRawOption<T, S>;
const randomInt = (min: number, max: number) => Math.floor(Math.random() * (max - min + 1)) + min;
@@ -127,8 +125,8 @@ export class Cache<S = string> {
});
}
get(key: string, defaultValue?: string): string | undefined {
const rv = this.db.prepare<{ value: string }, string>(
get(key: string, defaultValue?: S): S | undefined {
const rv = this.db.prepare<{ value: S }, string>(
`SELECT value FROM ${this.tableName} WHERE key = ? LIMIT 1`
).get(key);
@@ -150,7 +148,7 @@ export class Cache<S = string> {
async apply<T>(
key: string,
fn: () => Promise<T>,
opt: CacheApplyOption<T>
opt: CacheApplyOption<T, S>
): Promise<T> {
const { ttl, temporaryBypass } = opt;

View File

@@ -18,7 +18,7 @@ export async function compareAndWriteFile(span: Span, linesA: string[], filePath
console.log(`Nothing to write to ${filePath}...`);
isEqual = false;
} else {
isEqual = await span.traceChild(`comparing ${filePath}`).traceAsyncFn(async () => {
isEqual = await span.traceChildAsync(`comparing ${filePath}`, async () => {
let index = 0;
for await (const lineB of readFileByLine(file)) {
@@ -63,7 +63,7 @@ export async function compareAndWriteFile(span: Span, linesA: string[], filePath
return;
}
await span.traceChild(`writing ${filePath}`).traceAsyncFn(async () => {
await span.traceChildAsync(`writing ${filePath}`, async () => {
if (linesALen < 10000) {
return Bun.write(file, `${linesA.join('\n')}\n`);
}
@@ -98,7 +98,7 @@ export const createRuleset = (
type: 'ruleset' | 'domainset', surgePath: string, clashPath: string
) => parentSpan.traceChild(`create ruleset: ${path.basename(surgePath, path.extname(surgePath))}`).traceAsyncFn((childSpan) => {
const surgeContent = withBannerArray(title, description, date, content);
const clashContent = childSpan.traceChild('convert incoming ruleset to clash').traceSyncFn(() => {
const clashContent = childSpan.traceChildSync('convert incoming ruleset to clash', () => {
let _clashContent;
switch (type) {
case 'domainset':

View File

@@ -98,8 +98,8 @@ export const getPhishingDomains = (parentSpan: Span) => parentSpan.traceChild('g
SetAdd(domainSet, domainSet2);
}
span.traceChild('whitelisting phishing domains').traceSyncFn((parentSpan) => {
const trieForRemovingWhiteListed = parentSpan.traceChild('create trie for whitelisting').traceSyncFn(() => createTrie(domainSet));
span.traceChildSync('whitelisting phishing domains', (parentSpan) => {
const trieForRemovingWhiteListed = parentSpan.traceChildSync('create trie for whitelisting', () => createTrie(domainSet));
return parentSpan.traceChild('delete whitelisted from domainset').traceSyncFn(() => {
for (let i = 0, len = WHITELIST_DOMAIN.length; i < len; i++) {
@@ -115,7 +115,7 @@ export const getPhishingDomains = (parentSpan: Span) => parentSpan.traceChild('g
const domainCountMap: Record<string, number> = {};
const getDomain = createCachedGorhillGetDomain(gorhill);
span.traceChild('process phishing domain set').traceSyncFn(() => {
span.traceChildSync('process phishing domain set', () => {
const domainArr = Array.from(domainSet);
for (let i = 0, len = domainArr.length; i < len; i++) {
@@ -177,14 +177,14 @@ export const getPhishingDomains = (parentSpan: Span) => parentSpan.traceChild('g
}
});
const results = span.traceChild('get final phishing results').traceSyncFn(() => {
const results: string[] = [];
const results = span.traceChildSync('get final phishing results', () => {
const res: string[] = [];
for (const domain in domainCountMap) {
if (domainCountMap[domain] >= 5) {
results.push(`.${domain}`);
res.push(`.${domain}`);
}
}
return results;
return res;
});
return [results, domainSet] as const;