<commit_before>'use strict';

const log = require('util').debuglog('monrule');
const co = require('co');
const hash = require('object-hash');
const ObjectStore = require('./object-store');

const NS_SEPARATOR = ':';

module.exports = class FunctionCache {

	constructor(func, options) {
		if (typeof func !== 'function') {
			throw new Error('Found no function to cache');
		}

		options = Object.assign({}, options);
		this.func = func;
		this.funcHash = hash(func.toString());
		this.namespace = String(options.namespace || 'FunctionCache');
		this.resolver = options.resolver || FunctionCache.stdResolver;
		this.store = options.store || new ObjectStore({
			mongoose: options.mongoose || require('mongoose'),
			modelName: options.modelName || this.namespace
		});
	}

	getWrapper() {
		return this.get.bind(this);
	}

	get() {
		const args = arguments;
		const id = this.getId.apply(this, arguments);

		return co(function* () {
			log('get with id %s', id);
			let result = yield this.store.get(id);

			if (result) {
				log('found result for id %s', id);
				return result;
			}

			log('create a new entry for id %s', id);
			result = yield Promise.resolve(this.func.apply(this, args));
			return this.store.set(id, result);
		}.bind(this));
	}

	invalidate(query) {
		log('invalidating cache with query %s', JSON.stringify(query));
		return this.store.model.remove(query);
	}<commit_msg>refactor(debuglog): use distinct namespace for function cache<commit_after>'use strict';

const log = require('util').debuglog('monrule:cache');
const co = require('co');
const hash = require('object-hash');
const ObjectStore = require('./object-store');

const NS_SEPARATOR = ':';

module.exports = class FunctionCache {

	constructor(func, options) {
		if (typeof func !== 'function') {
			throw new Error('Found no function to cache');
		}

		options = Object.assign({}, options);
		this.func = func;
		this.funcHash = hash(func.toString());
		this.namespace = String(options.namespace || 'FunctionCache');
		this.resolver = options.resolver || FunctionCache.stdResolver;
		this.store = options.store || new ObjectStore({
			mongoose: options.mongoose || require('mongoose'),
			modelName: options.modelName || this.namespace
		});
	}

	getWrapper() {
		return this.get.bind(this);
	}

	get() {
		const args = arguments;
		const id = this.getId.apply(this, arguments);

		return co(function* () {
			log('get with id %s', id);
			let result = yield this.store.get(id);

			if (result) {
				log('found result for id %s', id);
				return result;
			}

			log('call wrapped function and create a new entry for id %s', id);
			result = yield Promise.resolve(this.func.apply(this, args));
			return this.store.set(id, result);
		}.bind(this));
	}

	invalidate(query) {
		log('invalidating cache with query %s', JSON.stringify(query));
		return this.store.model.remove(query);
	}
{'fraction_non_alphanumeric': 0.1593597773138483, 'fraction_numerical': 0.0, 'mean_word_length': 4.124777183600713, 'pattern_counts': {'":': 0, '<': 3, '<?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': '7209668', 'n_tokens_mistral': 988, 'n_tokens_neox': 892, 'n_words': 234}