<commit_before>/**

  cache.js

  Basic caching for server. Entries consist of:
  • key:   cache key for reference
  • type:  file type (for sending)
  • val:   value

  Middleware creates a cache key based on the route and returns the entry if available.

**/

var get = require("lodash/get");
var Utils = require("./utils");
var Renderers = require("./renderers");
var minimatch = require("minimatch");


var Cache = module.exports = {

  store: require("lru-cache")({

    max: get(global.shipp, "config.cache.sizeLimit"),

    length: function(cached, key) {

      var val = cached.val;

      // Assume vanilla types have byte size 4, calculate all others
      var size = 4;
      if (Buffer.isBuffer(val))
        size = val.length;
      else if ("string" === typeof(val))
        size = Buffer.byteLength(val);

      // Object keys = 7 (string length of type, val)
      return size + 7 + Buffer.byteLength(key) + Buffer.byteLength(cached.type || "");

    }

  }),


  has: function(key) {
    return Cache.store.has(key);
  },


  length: function() {
    return Cache.store.length;
  },

  /**

    Gets an object from the cache.

    @param {String} key The key of the object
    @returns {*} The object

  **/

  get: function(key) {
    return Cache.store.get(key);
  },


  /**

    Sets an object in the cache.

    @param {String} key The key of the object
    @param {Object} value The object to store

  **/

  set: function(key, value) {
    if ("undefined" === typeof value)
      Cache.unset(key);
    else
      Cache.store.set(key, value);
  },


  /**

    Unsets an object in the cache.

    @param {String} key The key of the object

  **/

  unset: function(key) {
    Cache.store.del(key);
  },


  /**

    Warms a route

    @param {String} id Route id
    @param {String} method Compression method (defaults to gzip)
    @param {Object} params query parameters (defaults to {})

    @returns {Promise} Resolves after warming and returns cache key

  **/

  warm: function(id, method, params) {<commit_msg>Cache: adds ability to enable or disable the cache<commit_after>/**

  cache.js

  Basic caching for server. Entries consist of:
  • key:   cache key for reference
  • type:  file type (for sending)
  • val:   value

  Middleware creates a cache key based on the route and returns the entry if available.

**/

var get = require("lodash/get");
var Utils = require("./utils");
var Renderers = require("./renderers");
var minimatch = require("minimatch");


var Cache = module.exports = {

  enabled: false,

  store: require("lru-cache")({

    max: get(global.shipp, "config.cache.sizeLimit"),

    length: function(cached, key) {

      var val = cached.val;

      // Assume vanilla types have byte size 4, calculate all others
      var size = 4;
      if (Buffer.isBuffer(val))
        size = val.length;
      else if ("string" === typeof(val))
        size = Buffer.byteLength(val);

      // Object keys = 7 (string length of type, val)
      return size + 7 + Buffer.byteLength(key) + Buffer.byteLength(cached.type || "");

    }

  }),


  has: function(key) {
    return Cache.store.has(key);
  },


  length: function() {
    return Cache.store.length;
  },

  /**

    Gets an object from the cache.

    @param {String} key The key of the object
    @returns {*} The object

  **/

  get: function(key) {
    return Cache.store.get(key);
  },


  /**

    Sets an object in the cache.

    @param {String} key The key of the object
    @param {Object} value The object to store

  **/

  set: function(key, value) {
    if (!Cache.enabled) return;

    if ("undefined" === typeof value)
      Cache.unset(key);
    else
      Cache.store.set(key, value);
  },


  /**

    Unsets an object in the cache.

    @param {String} key The key of the object

  **/

  unset: function(key) {
    if (!Cache.enabled) return;

    Cache.store.del(key);
  },


  /**

    Warms a route

    @param {String} id Route id
    @param {String} method Compression method (defaults to gzip)
    @param {Object} params query parameters (defaults to {})

    @returns {Promise} Resolves after warming and returns cache key

  **/

  warm: function(id, method, params) {
{'fraction_non_alphanumeric': 0.12922417896239885, 'fraction_numerical': 0.0019038553069966682, 'mean_word_length': 2.598458904109589, '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': '1243943', 'n_tokens_mistral': 1367, 'n_tokens_neox': 1281, 'n_words': 446}