Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement LRU cache for property filters #61

Merged
merged 5 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nodejs 16.20.0
2 changes: 2 additions & 0 deletions packages/property/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ const pvs1valid = PropertyFilter.isValid(pvs1, pf); // false
const pvs2valid = PropertyFilter.isValid(pvs2, pf); // true
```

The library uses an LRU (Least Recently Used) cache to improve performance. The size of this cache is controlled by the `process.env.PROPERTY_FILTER_CACHE_SIZE` environment variable on node. By default the LRU cache can store up to 20000 items.

[version-image]: https://img.shields.io/npm/v/@promaster-sdk/property.svg?style=flat
[version-url]: https://www.npmjs.com/package/@promaster-sdk/property
[prettier-image]: https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat
Expand Down
1 change: 1 addition & 0 deletions packages/property/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"promaster-sdk"
],
"dependencies": {
"lru-cache": "^7.18.3",
"uom": "^6.0.0"
}
}
18 changes: 13 additions & 5 deletions packages/property/src/property-filter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { UnitMap } from "uom";
import LRUCache from "lru-cache";
import * as PropertyValueSet from "./property-value-set";
import * as PropertyValue from "./property-value";
import * as Ast from "./property-filter-ast/index";
Expand All @@ -9,7 +10,14 @@ export interface PropertyFilter {
readonly _evaluate: Ast.CompiledFilterFunction;
}

const _cache: { [key: string]: PropertyFilter } = {}; //eslint-disable-line
const maxLRUCacheSize = globalThis.process?.env?.PROPERTY_FILTER_CACHE_SIZE
? Number(process.env.PROPERTY_FILTER_CACHE_SIZE)
: 20000;

const LRUCacheOptions = {
max: maxLRUCacheSize, // Arbitrary number. Uses on average up to 400mb
};
const _cache = new LRUCache<string, PropertyFilter>(LRUCacheOptions);

export const Empty: PropertyFilter = {
text: "",
Expand All @@ -25,8 +33,7 @@ export function fromString(filter: string, unitLookup: UnitMap.UnitLookup): Prop
if (filter === null || filter === undefined) {
throw new Error("Argument 'filter' must be defined.");
}
// eslint-disable-next-line no-prototype-builtins
if (!_cache.hasOwnProperty(filter)) {
if (!_cache.has(filter)) {
if (filter === "" || filter.trim().length === 0) {
return Empty;
}
Expand All @@ -36,9 +43,10 @@ export function fromString(filter: string, unitLookup: UnitMap.UnitLookup): Prop
console.warn("Invalid property filter syntax: " + filter);
return undefined;
}
_cache[filter] = create(filter, ast);
_cache.set(filter, create(filter, ast));
}
return _cache[filter];

return _cache.get(filter);
}

export function fromStringOrEmpty(filterString: string, unitLookup: UnitMap.UnitLookup): PropertyFilter {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
} from "../../discrete/use-discrete-property-selector";
import { usePropertiesSelector, UsePropertiesSelectorOptions } from "../../properties-selector/use-properties-selector";

const unitLookup: UnitMap.UnitLookup = (unitString) => (BaseUnits as UnitMap.UnitMap)[unitString];

type PropertyValueDef = {
readonly sortNo: number;
readonly value: PropertyValue.PropertyValue | undefined | null;
Expand Down Expand Up @@ -84,8 +86,6 @@ function getExampleProductProperties(): {
}
const propInfo = getExampleProductProperties();

const unitLookup: UnitMap.UnitLookup = (unitString) => (BaseUnits as UnitMap.UnitMap)[unitString];

type UnitLabels = {
readonly [unitName: string]: string;
};
Expand Down
2 changes: 1 addition & 1 deletion packages/tsconfig.settings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
// From the lern-a
"target": "es5",
"target": "es6",
"module": "commonjs",
"declaration": true,
"declarationMap": true,
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10972,6 +10972,11 @@ lru-cache@^6.0.0:
dependencies:
yallist "^4.0.0"

lru-cache@^7.18.3:
version "7.18.3"
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-7.18.3.tgz#f793896e0fd0e954a59dfdd82f0773808df6aa89"
integrity sha512-jumlc0BIUrS3qJGgIkWZsyfAM7NCWiBcCDhnd+3NNM5KbBmLTgHVfWBcg6W+rLUsIpzpERPsvwUP7CckAQSOoA==

lz-string@^1.4.4:
version "1.4.4"
resolved "https://registry.yarnpkg.com/lz-string/-/lz-string-1.4.4.tgz#c0d8eaf36059f705796e1e344811cf4c498d3a26"
Expand Down
Loading