Skip to main content

xlucene-parser / cached-parser / CachedParser

Class: CachedParser

Defined in: packages/xlucene-parser/src/cached-parser.ts:27

A caching wrapper around the Parser class for improved performance.

CachedParser maintains an internal cache of parsed queries to avoid re-parsing identical query strings. This is particularly useful when the same queries are parsed repeatedly.

Example

const cache = new CachedParser();

// First parse - creates and caches the parser
const parser1 = cache.make('name:John');

// Second parse - returns cached parser
const parser2 = cache.make('name:John');

console.log(parser1 === parser2); // true

Constructors

Constructor

new CachedParser(): CachedParser

Defined in: packages/xlucene-parser/src/cached-parser.ts:28

Returns

CachedParser

Methods

make()

make(query, options?, _overrideParsedQuery?): Parser

Defined in: packages/xlucene-parser/src/cached-parser.ts:59

Create or retrieve a cached Parser instance for the given query.

The cache key is based on the query string and type configuration. If a parser for the same query and configuration exists in the cache, it will be returned instead of creating a new one.

Parameters

ParameterTypeDescription
querystringThe xLucene query string to parse
options?ParserOptionsOptional configuration for parsing behavior
_overrideParsedQuery?NodeInternal parameter for providing pre-parsed AST

Returns

Parser

A Parser instance (cached or newly created)

Example

const cache = new CachedParser();

// Create parser with type configuration
const parser = cache.make('age:25', {
type_config: { age: 'integer' }
});

// Same query and config returns cached parser
const cachedParser = cache.make('age:25', {
type_config: { age: 'integer' }
});

reset()

reset(): void

Defined in: packages/xlucene-parser/src/cached-parser.ts:88

Clear all cached parsers.

This method removes all entries from the internal cache, forcing subsequent calls to make() to create new Parser instances.

Returns

void

Example

const cache = new CachedParser();
cache.make('name:John'); // Creates and caches parser

cache.reset(); // Clear cache

cache.make('name:John'); // Creates new parser (not cached)