<commit_before>// After starting this example load http://localhost:8080 and hit refresh, you will notice that it loads the response from
// cache for the first 5 seconds and then reloads the cache

// Load modules

var Catbox = require('catbox');
var Http = require('http');


// Declare internals

var internals = {};


internals.handler = function (req, res) {

    internals.getResponse(function (err, item) {

        if (err) {
            res.writeHead(500);
            res.end();
        }
        else {
            res.writeHead(200, { 'Content-Type': 'text/plain' });
            res.end(item);
        }
    });
};


internals.getResponse = function (callback) {

    var key = {
        segment: 'example',
        id: 'myExample'
    };

    var cacheValue = 'my example';
    var ttl = 10000;                         // How long item will be cached in milliseconds

    internals.client.get(key, function (err, cached) {

        if (err) {
            return callback(err);
        }
        else if (cached) {
            return callback(null, 'From cache: ' + cached.item);
        }
        internals.client.set(key, cacheValue, ttl, function (error) {

            callback(error, cacheValue);
        });
    });
};


internals.startCache = function (callback) {

    var options = {
        partition: 'examples',               // For redis this will store items under keys that start with examples:
        location: '127.0.0.1:11212'         // If you don't supply, 127.0.0.1:11212 is the default
    };

    internals.client = new Catbox.Client(require('../'), options);  // Replace require('../') with 'catbox-mongodb' in your own code
    internals.client.start(callback);
};


internals.startServer = function (err) {

    if (err) {
        console.log(err);
        console.log('Could not connect to memcache. Ending process.');
        process.exit();
    }
    else {
        var server = Http.createServer(internals.handler);
        server.listen(8080);
        console.log('Server started at http://localhost:8080/');
    }
};


internals.startCache(internals.startServer);
<commit_msg>Refactoring example [WIP]<commit_after>'use strict';

// After starting this example load http://localhost:8080 and hit refresh.
// You will notice that it loads the response from cache
// for the first 5 seconds and then reloads the cache

// Load modules
const Catbox = require('catbox');
const Http = require('http');


// Declare internals
const internals = {};

internals.handler = async (req, res) => {

    try {
        const item = await internals.getResponse();
        res.writeHead(200, { 'Content-Type': 'text/plain' });
        res.end(item);
    }
    catch (ignoreErr) {
        res.writeHead(500);
        res.end();
    }
};


internals.getResponse = async () => {
    const key = {
        segment: 'example',
        id: 'myExample'
    };

    const cacheValue = 'my example';
    const ttl = 10000; // How long item will be cached in milliseconds

    const cached = await internals.client.get(key);
    console.log(cached);

    if (cached) {
        return `From cache: ${cached.item}`;
    }

    const value = await internals.client.set(key, cacheValue, ttl);
    console.log(value);

    return cacheValue;
};

internals.startCache = async () => {
    const options = {
        partition: 'examples',
        location: '127.0.0.1:11211'
    };
    try {
        internals.client = new Catbox.Client(require('../'), options);  // Replace require('../') with 'catbox-memcached' in your own code
        await internals.client.start();
    } catch (error) {
        console.log('error');
        console.log(error);
        process.exit();
    }
};

internals.startServer = async () => {
    const server = Http.createServer(internals.handler);
    server.listen(8080);
};

(async() => {
    await internals.startCache();
    await internals.startServer();
    console.log('Server started at http://localhost:8080/');
})();

// Useful to debug unhandled rejection errors
process.on('unhandledRejection', error => {
    console.log(error);
    process.exit();
});

{'fraction_non_alphanumeric': 0.13798977853492334, 'fraction_numerical': 0.019712825504989047, 'mean_word_length': 2.413621262458472, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 3, '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': '196497', 'n_tokens_mistral': 1272, 'n_tokens_neox': 1188, 'n_words': 329}