<commit_before>'use strict';

var request = require('request');
var sf = require('sf');
var url = require('url');
var CircuitBreaker = require('./CircuitBreaker');

module.exports = getThenCache;

function getThenCache(options, config, cache, eventHandler, stream, onError) {

    var start = new Date();

    if(!options.explicitNoCache && options.cacheTTL > 0) {

        cache.get(options.cacheKey, function(err, content, oldContent) {
            if (err) return onError(err, oldContent);
            if (content) {
                var timing = (new Date() - start);
                eventHandler.logger('info', 'CACHE ' + options.cacheKey,{tracer:options.tracer, responseTime: timing, pcType:options.type});
                eventHandler.logger('debug', 'Cache HIT for key: ' + options.cacheKey,{tracer:options.tracer,pcType:options.type});
                eventHandler.stats('increment', options.statsdKey + '.cacheHit');
                stream.end(content);
                return;
            } else {
                eventHandler.logger('debug', 'Cache MISS for key: ' + options.cacheKey,{tracer:options.tracer,pcType:options.type});
                eventHandler.stats('increment', options.statsdKey + '.cacheMiss');
                if(options.url == 'cache') {
                    stream.end("");
                    return;
                }
            }

            CircuitBreaker(options, config, eventHandler, pipeAndCacheContent, function(err, content) {
                if (err) return onError(err, oldContent);
                stream.end(content);
                cache.set(options.cacheKey, content, options.cacheTTL, function(err) {
                    eventHandler.logger('debug', 'Cache SET for key: ' + options.cacheKey + ' @ TTL: ' + options.cacheTTL,{tracer:options.tracer,pcType:options.type});
                });<commit_msg>Slight clarification on flow control<commit_after>'use strict';

var request = require('request');
var sf = require('sf');
var url = require('url');
var CircuitBreaker = require('./CircuitBreaker');

module.exports = getThenCache;

function getThenCache(options, config, cache, eventHandler, stream, onError) {

    var start = new Date();

    if(!options.explicitNoCache && options.cacheTTL > 0) {

        cache.get(options.cacheKey, function(err, content, oldContent) {
            if (err) return onError(err, oldContent);
            if (content) {
                var timing = (new Date() - start);
                eventHandler.logger('info', 'CACHE ' + options.cacheKey,{tracer:options.tracer, responseTime: timing, pcType:options.type});
                eventHandler.logger('debug', 'Cache HIT for key: ' + options.cacheKey,{tracer:options.tracer,pcType:options.type});
                eventHandler.stats('increment', options.statsdKey + '.cacheHit');
                stream.end(content);
                return;
            }

            eventHandler.logger('debug', 'Cache MISS for key: ' + options.cacheKey,{tracer:options.tracer,pcType:options.type});
            eventHandler.stats('increment', options.statsdKey + '.cacheMiss');

            if(options.url == 'cache') {
                stream.end("");
                return;
            }

            CircuitBreaker(options, config, eventHandler, pipeAndCacheContent, function(err, content) {
                if (err) return onError(err, oldContent);
                stream.end(content);
                cache.set(options.cacheKey, content, options.cacheTTL, function(err) {
                    eventHandler.logger('debug', 'Cache SET for key: ' + options.cacheKey + ' @ TTL: ' + options.cacheTTL,{tracer:options.tracer,pcType:options.type});
                });
{'fraction_non_alphanumeric': 0.1347093970668115, 'fraction_numerical': 0.0005431830526887561, 'mean_word_length': 2.4943074003795065, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '4583728', 'n_tokens_mistral': 1034, 'n_tokens_neox': 980, 'n_words': 205}