Skip to content
This repository has been archived by the owner on Oct 27, 2020. It is now read-only.

Commit

Permalink
feat: Allow to change how cache is stored
Browse files Browse the repository at this point in the history
  • Loading branch information
Mordred committed Sep 27, 2017
1 parent 9a18ba9 commit fe4bcfd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ module.exports = {
|:--:|:--:|:-----:|:----------|
|**`cacheDirectory`**|`{String}`|`path.resolve('.cache-loader')`|Provide a cache directory where cache items should be stored|
|**`cacheIdentifier`**|`{String}`|`cache-loader:{version} {process.env.NODE_ENV}`|Provide an invalidation identifier which is used to generate the hashes. You can use it for extra dependencies of loaders.|
|**`writeCache`**|`{Function(cacheFilename, content, callback) -> {void}}`|`undefined`|Allows you to override default write cache data to file (e.g. Redis, memcached).|
|**`readCache`**|`{Function(cacheFilename, callback) -> {void}}`|`undefined`|Allows you to override default read cache data from file.|

<h2 align="center">Examples</h2>

Expand Down
30 changes: 22 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,17 @@ const pkgVersion = require('../package.json').version;
const defaultCacheDirectory = path.resolve('.cache-loader');
const ENV = process.env.NODE_ENV || 'development';

const defaultOptions = {
cacheDirectory: defaultCacheDirectory,
cacheIdentifier: `cache-loader:${pkgVersion} ${ENV}`,
readCache,
writeCache,
};

function loader(...args) {
const loaderOptions = loaderUtils.getOptions(this) || {};
const options = Object.assign({}, defaultOptions, loaderOptions);
const { writeCache: writeCacheFn } = options;
const callback = this.async();
const { data } = this;
const dependencies = this.getDependencies().concat(this.loaders.map(l => l.path));
Expand All @@ -36,13 +46,13 @@ function loader(...args) {
}
const [deps, contextDeps] = taskResults;
const writeCacheFile = () => {
fs.writeFile(data.cacheFile, JSON.stringify({
writeCacheFn(data.cacheFile, JSON.stringify({
remainingRequest: data.remainingRequest,
cacheIdentifier: data.cacheIdentifier,
dependencies: deps,
contextDependencies: contextDeps,
result: args,
}), 'utf-8', () => {
}), () => {
// ignore errors here
callback(null, ...args);
});
Expand All @@ -64,20 +74,16 @@ function loader(...args) {

function pitch(remainingRequest, prevRequest, dataInput) {
const loaderOptions = loaderUtils.getOptions(this) || {};
const defaultOptions = {
cacheDirectory: defaultCacheDirectory,
cacheIdentifier: `cache-loader:${pkgVersion} ${ENV}`,
};
const options = Object.assign({}, defaultOptions, loaderOptions);
const { cacheIdentifier, cacheDirectory } = options;
const { cacheIdentifier, cacheDirectory, readCache: readCacheFn } = options;
const data = dataInput;
const callback = this.async();
const hash = digest(`${cacheIdentifier}\n${remainingRequest}`);
const cacheFile = path.join(cacheDirectory, `${hash}.json`);
data.remainingRequest = remainingRequest;
data.cacheIdentifier = cacheIdentifier;
data.cacheFile = cacheFile;
fs.readFile(cacheFile, 'utf-8', (readFileErr, content) => {
readCacheFn(cacheFile, (readFileErr, content) => {
if (readFileErr) {
callback();
return;
Expand Down Expand Up @@ -123,4 +129,12 @@ function digest(str) {
return crypto.createHash('md5').update(str).digest('hex');
}

function writeCache(cacheFile, content, callback) {
fs.writeFile(cacheFile, content, 'utf-8', callback);
}

function readCache(cacheFile, callback) {
fs.readFile(cacheFile, 'utf-8', callback);
}

export { loader as default, pitch };

0 comments on commit fe4bcfd

Please sign in to comment.