<commit_before>import fs from 'fs';

const CACHE = {};

export default function loadLuaScript(filename) {
    if (CACHE.hasOwnProperty(filename)) {
        return CACHE[filename];
    }

    CACHE[filename] = fs.readFileSync(filename).toString('utf8');
    return CACHE[filename];
}<commit_msg>Add EVALSHA support to lualoader<commit_after>import fs from 'fs';
import logger from '../logger';

const CACHE = {};
const EVALSHA_CACHE = {};

export function loadLuaScript(filename) {
    if (CACHE.hasOwnProperty(filename)) {
        return CACHE[filename];
    }

    CACHE[filename] = fs.readFileSync(filename).toString('utf8');
    return CACHE[filename];
}

export function runLuaScript(redisClient, filename, args) {
    if (EVALSHA_CACHE.hasOwnProperty(filename)) {
        args.unshift(EVALSHA_CACHE[filename]);
        return redisClient.evalshaAsync.apply(redisClient, args);
    } else {
        return redisClient.scriptAsync('load', loadLuaScript(filename))
                .then(sha => {
            logger.debug(`Cached ${filename} as ${sha}`);
            EVALSHA_CACHE[filename] = sha;
            args.unshift(sha);
            return redisClient.evalshaAsync.apply(redisClient, args);
        });
    }
}
{'fraction_non_alphanumeric': 0.1287940935192781, 'fraction_numerical': 0.0016406890894175555, 'mean_word_length': 3.3884892086330933, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '775118', 'n_tokens_mistral': 369, 'n_tokens_neox': 368, 'n_words': 71}