<commit_before>import lrucache from 'lru-cache';

const namespace = new WeakMap();

class PatternCache {
	static defaults = {
		max: 50000
	};

	constructor(options = {}) {
		const settings = {...PatternCache.defaults, ...options.options};
		this.config = settings;
		const cache = lrucache(settings);
		namespace.set(this, {settings, cache});
	}

	set(key, mtime, value, meta) {
		const {cache} = namespace.get(this);
		return cache.set(key, {mtime, value, meta});
	}

	get(key, mtime) {
		const {cache} = namespace.get(this);
		const stored = cache.get(key);

		if (typeof stored === 'undefined') {
			return null;
		}

		const {'mtime': storedMtime, value} = stored;

		if (mtime === false) {
			return value;
		}

		if (new Date(storedMtime) < new Date(mtime)) {
			cache.del(key);
			return null;
		}

		return value;
	}

	peek(key) {
		const {cache} = namespace.get(this);
		return cache.peek(key);
	}

	get length() {
		const {cache} = namespace.get(this);
		return cache.length;
	}

	get itemCount() {
		const {cache} = namespace.get(this);
		return cache.itemCount;
	}
}

function patternCacheFactory(...args) {
	return new PatternCache(...args);
}

export default patternCacheFactory;
export {PatternCache as PatternCache};<commit_msg>fix: avoid reference errors in cache<commit_after>import lrucache from 'lru-cache';
import {get} from 'lodash';

const namespace = new WeakMap();

function select(instance, keyPath) {
	const context = namespace.get(instance) || {};
	return get(context, keyPath);
}

class PatternCache {
	static defaults = {
		max: 50000
	};

	constructor(options = {}) {
		const settings = {...PatternCache.defaults, ...options.options};
		this.config = settings;
		const cache = lrucache(settings);
		namespace.set(this, {settings, cache});
	}

	set(key, mtime, value, meta) {
		const cache = select(this, 'cache');

		if (!cache) {
			return;
		}

		return cache.set(key, {mtime, value, meta});
	}

	get(key, mtime) {
		const cache = select(this, 'cache');

		if (!cache) {
			return null;
		}

		const stored = cache.get(key);

		if (typeof stored === 'undefined') {
			return null;
		}

		const {'mtime': storedMtime, value} = stored;

		if (mtime === false) {
			return value;
		}

		if (new Date(storedMtime) < new Date(mtime)) {
			cache.del(key);
			return null;
		}

		return value;
	}

	peek(key) {
		const cache = select(this, 'cache');
		return cache ? cache.peek(key) : false;
	}

	get length() {
		const cache = select(this, 'cache');
		return cache ? cache.length : 0;
	}

	get itemCount() {
		const cache = select(this, 'cache');
		return cache ? cache.itemCount : 0;
	}
}

function patternCacheFactory(...args) {
	return new PatternCache(...args);
}

export default patternCacheFactory;
export {PatternCache as PatternCache};
{'fraction_non_alphanumeric': 0.1537351136773728, 'fraction_numerical': 0.004330566582461205, 'mean_word_length': 3.6903553299492384, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7123947', 'n_tokens_mistral': 1045, 'n_tokens_neox': 937, 'n_words': 245}