Sha256: c5cd9725e64d87bced8d7b3882c7dfadc6f0af3185e39b1b2f49f6a5941816a3

Contents?: true

Size: 1.48 KB

Versions: 19

Compression:

Stored size: 1.48 KB

Contents

/** internal
 *  mixin Caching
 *
 *  An internal mixin whose public methods are exposed on the [[Environment]]
 *  and [[Index]] classes.
 **/


////////////////////////////////////////////////////////////////////////////////


'use strict';


// stdlib
var join    = require('path').join;
var crypto  = require('crypto');


// internal
var Asset = require('../assets/asset');


////////////////////////////////////////////////////////////////////////////////


function expandKey(self, key) {
  key = String(key).replace(self.root, '');
  key = crypto.createHash('md5').update(key, 'utf8').digest('hex');
  return join('mincer', key);
}


function cacheSet(self, key, hash) {
  hash._version = self.digest.digest('hex');
  self.cache.set(expandKey(self, key), hash);
}


function cacheGet(self, key) {
  var hash = self.cache.get(expandKey(self, key));
  if (hash && hash._version === self.digest.digest('hex')) {
    return hash;
  }
}


module.exports.cacheAsset = function (path, fn) {
  var asset, hash;

  if (!this.cache) {
    return fn();
  }

  asset = Asset.fromHash(this, cacheGet(this, path));
  if (asset && asset.isFresh(this)) {
    return asset;
  }

  asset = fn();
  if (asset) {
    hash = {};
    asset.encodeWith(hash);

    // Save the asset to its path
    cacheSet(this, path, hash);

    // Since path maybe a logical or full pathname, save the
    // asset its its full path too
    if (path !== asset.pathname) {
      cacheSet(this, asset.pathname, hash);
    }

    return asset;
  }
};

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
ela-4.1.6 node_modules/mincer/lib/mincer/helpers/caching.js
ela-4.1.5 node_modules/mincer/lib/mincer/helpers/caching.js
ela-4.1.4 node_modules/mincer/lib/mincer/helpers/caching.js
ela-4.1.3 node_modules/mincer/lib/mincer/helpers/caching.js
ela-4.1.2 node_modules/mincer/lib/mincer/helpers/caching.js
ela-4.1.1 node_modules/mincer/lib/mincer/helpers/caching.js
ela-4.1.0 node_modules/mincer/lib/mincer/helpers/caching.js
ela-4.0.0 node_modules/mincer/lib/mincer/helpers/caching.js
ela-3.4.3 node_modules/mincer/lib/mincer/helpers/caching.js
ela-3.4.2 node_modules/mincer/lib/mincer/helpers/caching.js
ela-3.4.0 node_modules/mincer/lib/mincer/helpers/caching.js
ela-3.3.1 node_modules/mincer/lib/mincer/helpers/caching.js
ela-3.3.0 node_modules/mincer/lib/mincer/helpers/caching.js
ela-3.2.0 node_modules/mincer/lib/mincer/helpers/caching.js
ela-3.1.1 node_modules/mincer/lib/mincer/helpers/caching.js
ela-3.1.0 node_modules/mincer/lib/mincer/helpers/caching.js
ela-3.0.0 node_modules/mincer/lib/mincer/helpers/caching.js
ela-2.0.0 node_modules/mincer/lib/mincer/helpers/caching.js
ela-1.1.0 node_modules/mincer/lib/mincer/helpers/caching.js