<commit_before>var fs = require("fs");
var path = require("path");
var mkdirp = require("mkdirp");
var EventEmitter = require("events").EventEmitter;

// TODO: Implement an in-memory cache for this (using ./memory.js?)

var DiskCache = module.exports = function (options) {
  this.folder = options.buildDir;
};

DiskCache.prototype = new EventEmitter();

DiskCache.prototype.add = function (key, version, contents, callback) {
  var file = path.normalize(this.folder + path.sep + key);
  var folder = path.dirname(file);

  mkdirp(folder, function (err) {
    if (err) return callback(err);
    fs.writeFile(file, contents, "utf-8", callback);
  });
};

DiskCache.prototype.get = function (key, callback) {
  var file = path.normalize(this.folder + path.sep + key);
  fs.readFile(file, "utf-8", callback);
};

DiskCache.prototype.contains = function (key, version, callback) {
  var file = path.normalize(this.folder + path.sep + key);

  fs.stat(file, function (err, stats) {
    if (err) {
      if (err.code == "ENOENT") return callback(null, false);
      else return callback(err);
    }

    return callback(null, stats.mtime.getTime() >= version);
  });
};<commit_msg>Added TODO to disk cache--that thing really needs to be tested a bit more.<commit_after>var fs = require("fs");
var path = require("path");
var mkdirp = require("mkdirp");
var EventEmitter = require("events").EventEmitter;

// TODO: Implement an in-memory cache for this (using ./memory.js?)
// TODO: What happens when running on a read-only filesystem? 

var DiskCache = module.exports = function (options) {
  this.folder = options.buildDir;
};

DiskCache.prototype = new EventEmitter();

DiskCache.prototype.add = function (key, version, contents, callback) {
  var file = path.normalize(this.folder + path.sep + key);
  var folder = path.dirname(file);

  mkdirp(folder, function (err) {
    if (err) return callback(err);
    fs.writeFile(file, contents, "utf-8", callback);
  });
};

DiskCache.prototype.get = function (key, callback) {
  var file = path.normalize(this.folder + path.sep + key);
  fs.readFile(file, "utf-8", callback);
};

DiskCache.prototype.contains = function (key, version, callback) {
  var file = path.normalize(this.folder + path.sep + key);

  fs.stat(file, function (err, stats) {
    if (err) {
      if (err.code == "ENOENT") return callback(null, false);
      else return callback(err);
    }

    return callback(null, stats.mtime.getTime() >= version);
  });
};
{'fraction_non_alphanumeric': 0.1544058205335489, 'fraction_numerical': 0.0016168148746968471, 'mean_word_length': 4.040733197556008, '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': '3579424', 'n_tokens_mistral': 783, 'n_tokens_neox': 776, 'n_words': 216}