<commit_before>/**
 * jQuery.ajax-manual-cache.js
 * version 0.0.0
 *
 * Another cache layer for GET ajax requests.
 *
 * Copyright <NAME> <<EMAIL>>
 * Released under the MIT license
 */

(function($, undefined){

    var _fp_ajax = $.ajax;  // remembers the original $.ajax

    // main method
    $.ajax_mc = function(url, options){
        var s, 
            deferred,
            cached_result,
            context = {},
            update_cache = false;

        // same as jQuery.ajax
        if ( typeof url === "object" ) {
            options = url;
            url = undefined;
        } 

        // prepare options and s object as is done in jQuery.ajax
        options = $.extend({}, $.ajax_mc.defaults, options);
        s = jQuery.ajaxSetup( {}, options );

        // Russian roulette for cache maintenance
        if (Math.random() < options.amc_maint_prob) {
            _cache_maint();
        }

        // Cache GET requests only
        if ( (s.method || s.type).toLowerCase() === 'get') {
            // convert data object to string
            if ( s.data && s.processData && typeof s.data !== "string" ) {
                s.data = jQuery.param( s.data, s.traditional );
            }

            url = url || s.url;
            options.amc_key = options.amc_key || (url + (url.indexOf('?') !== -1 ? '&':'?') + s.data);
            cached_result = _cache_get(options.amc_group, options.amc_key);
            
            if (cached_result) {
                // cache hit, fire 'done' callback(s) in options
                if (options.success) {
                    if (typeof options.success == "function") {
                        options.success = [options.success];
                    }

                    $.each(options.success, function(idx, cb){
                        cb.call(s.context, cached_result);
                    })
                }
                deferred = $.Deferred();

                window.setTimeout(function(){
                    deferred.resolveWith(context, [cached_result]);
                }, 1);

                // job done, another ajax request saved.
                return deferred.promise();
            }
            update_cache = true;
        }

        // let jQuery.ajax finish the job
        deferred = _fp_ajax(url, options);
        
        if (update_cache) {
            deferred.done(function(data){
                _cache_set(options.amc_group, options.amc_key, data, options.amc_expires);
            })
        }

        return deferred;
    }<commit_msg>invoke options.success via deferred object, fix context on cache hit<commit_after>/**
 * jQuery.ajax-manual-cache.js
 * version 0.0.0
 *
 * Another cache layer for GET ajax requests.
 *
 * Copyright <NAME> <<EMAIL>>
 * Released under the MIT license
 */

(function($, undefined){

    var _fp_ajax = $.ajax;  // remembers the original $.ajax

    // main method
    $.ajax_mc = function(url, options){
        var s, 
            deferred,
            cached_result,
            update_cache = false;

        // same as jQuery.ajax
        if ( typeof url === "object" ) {
            options = url;
            url = undefined;
        } 

        // prepare options and s object as is done in jQuery.ajax
        options = $.extend({}, $.ajax_mc.defaults, options);
        s = jQuery.ajaxSetup( {}, options );

        // Russian roulette for cache maintenance
        if (Math.random() < options.amc_maint_prob) {
            _cache_maint();
        }

        // Cache GET requests only
        if ( (s.method || s.type).toLowerCase() === 'get') {
            // convert data object to string
            if ( s.data && s.processData && typeof s.data !== "string" ) {
                s.data = jQuery.param( s.data, s.traditional );
            }

            url = url || s.url;
            options.amc_key = options.amc_key || (url + (url.indexOf('?') !== -1 ? '&':'?') + s.data);
            cached_result = _cache_get(options.amc_group, options.amc_key);

            if (cached_result) {
                // cache hit, fire 'done' callback(s) in options
                deferred = $.Deferred();

                if (options.success) {
                    if (typeof options.success == "function") {
                        options.success = [options.success];
                    }

                    $.each(options.success, function(idx, cb){
                        deferred.done(cb);
                    })
                }
                
                window.setTimeout(function(){
                    deferred.resolveWith(options.context, [cached_result]);
                }, 1);

                // job done, another ajax request saved.
                return deferred.promise();
            }
            update_cache = true;
        }

        // let jQuery.ajax finish the job
        deferred = _fp_ajax(url, options);
        
        if (update_cache) {
            deferred.done(function(data){
                _cache_set(options.amc_group, options.amc_key, data, options.amc_expires);
            })
        }

        return deferred;
    }
{'fraction_non_alphanumeric': 0.12216575449569976, 'fraction_numerical': 0.001954652071931196, 'mean_word_length': 1.4366666666666668, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, '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': '2945078', 'n_tokens_mistral': 1427, 'n_tokens_neox': 1339, 'n_words': 341}