refactor: bypass cache when necessary

This commit is contained in:
Viren070
2025-05-06 20:16:41 +01:00
parent 25c0639f98
commit 760fd27a15
2 changed files with 20 additions and 24 deletions
+4 -7
View File
@@ -135,7 +135,7 @@ export class AIOStreams {
Settings.DEFAULT_REGEX_EXCLUDE_PATTERN
: undefined;
const excludeRegex = excludeRegexPattern
? compileRegex(excludeRegexPattern, 'i')
? compileRegex(excludeRegexPattern, 'i', true)
: undefined;
const excludeKeywordsRegex = this.config.excludeFilters
@@ -147,7 +147,7 @@ export class AIOStreams {
Settings.DEFAULT_REGEX_INCLUDE_PATTERN
: undefined;
const requiredRegex = requiredRegexPattern
? compileRegex(requiredRegexPattern, 'i')
? compileRegex(requiredRegexPattern, 'i', true)
: undefined;
const requiredKeywordsRegex = this.config.strictIncludeFilters
@@ -162,7 +162,7 @@ export class AIOStreams {
? sortRegexPatterns
.split(/\s+/)
.filter(Boolean)
.map((pattern) => compileRegex(pattern, 'i'))
.map((pattern) => compileRegex(pattern, 'i', true))
: undefined;
excludeRegex ||
@@ -830,12 +830,9 @@ export class AIOStreams {
if (!b.filename) return direction === 'asc' ? 1 : -1;
// Test patterns in order
for (let i = 0; i < compiledRegexPatterns.length; i++) {
const regex = compiledRegexPatterns[i];
for (const regex of compiledRegexPatterns) {
const aMatch = safeRegexTest(regex, a.filename);
const bMatch = safeRegexTest(regex, b.filename);
// If both match or both don't match, continue to next pattern
if ((aMatch && bMatch) || (!aMatch && !bMatch)) continue;
+16 -17
View File
@@ -5,8 +5,11 @@ import { createLogger } from './logger';
import { Settings } from './settings';
const DEFAULT_TIMEOUT = 1000; // 1 second timeout
const regexCache = Cache.getInstance<string, RegExp>('regexCache');
const resultCache = Cache.getInstance<string, string>('regexResultCache');
const regexCache = Cache.getInstance<string, RegExp>('regexCache', 1_000);
const resultCache = Cache.getInstance<string, boolean>(
'regexResultCache',
1_000_000
);
const logger = createLogger('regex');
@@ -22,41 +25,37 @@ export function safeRegexTest(
str: string,
timeoutMs: number = DEFAULT_TIMEOUT
): boolean {
// const compiledPattern =
// typeof pattern === 'string'
// ? regexCache.wrap(
// (p: string) => new RegExp(p),
// getTextHash(pattern),
// 60,
// pattern
// )
// : pattern;
const compiledPattern =
typeof pattern === 'string' ? compileRegex(pattern) : pattern;
try {
const result = resultCache.wrap(
return resultCache.wrap(
(p: RegExp, s: string) => isMatch(p, s, { timeout: timeoutMs }),
getTextHash(`${compiledPattern.toString()}|${str}`),
60,
100,
compiledPattern,
str
);
return result;
} catch (error) {
logger.error(`Regex test timed out after ${timeoutMs}ms:`, error);
return false;
}
}
export function compileRegex(pattern: string, flags: string = ''): RegExp {
const compiledPattern = regexCache.wrap(
export function compileRegex(
pattern: string,
flags: string = '',
bypassCache: boolean = false
): RegExp {
if (bypassCache) {
return new RegExp(pattern, flags);
}
return regexCache.wrap(
(p: string, f: string) => new RegExp(p, f),
getTextHash(`${pattern}|${flags}`),
60,
pattern,
flags
);
return compiledPattern;
}
export function formRegexFromKeywords(