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

refactor: remove cache prefix (options.cacheKey/cacheIdentifier/cacheDirectory) #22

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ module.exports = {

|Name|Type|Default|Description|
|:--:|:--:|:-----:|:----------|
|**`cacheKey`**|`{Function(options, request) -> {String}}`|`undefined`|Allows you to override default cache key generator|
|**`cacheDirectory`**|`{String}`|`path.resolve('.cache-loader')`|Provide a cache directory where cache items should be stored (used for default read/write implementation)|
|**`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 (used for default read/write implementation)|
|**`key`**|`{Function(options, request) -> {String}}`|`undefined`|Allows you to override default cache key generator|
|**`directory`**|`{String}`|`path.resolve('.cache-loader')`|Provide a cache directory where cache items should be stored (used for default read/write implementation)|
|**`identifier`**|`{String}`|`cache-loader:{version} {process.env.NODE_ENV}`|Provide an invalidation cache identifier which is used to generate the hashes. You can use it for extra dependencies of loaders (used for default read/write implementation)|
|**`write`**|`{Function(cacheKey, data, callback) -> {void}}`|`undefined`|Allows you to override default write cache data to file (e.g. Redis, memcached)|
|**`read`**|`{Function(cacheKey, callback) -> {void}}`|`undefined`|Allows you to override default read cache data from file|
|**`read`**|`{Function(key, callback) -> {void}}`|`undefined`|Allows you to override default read cache data from file|

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

Expand Down Expand Up @@ -91,12 +91,10 @@ function digest(str) {
return crypto.createHash('md5').update(str).digest('hex');
}

// Generate own cache key
function cacheKey(options, request) {
function key(options, request) {
return `build:cache:${digest(request)}`;
}


// Read data from database and parse them
function read(key, callback) {
client.get(key, (err, result) => {
Expand All @@ -117,8 +115,7 @@ function read(key, callback) {
});
}


// Write data to database under cacheKey
// Write data to the database under the generated key
function write(key, data, callback) {
client.set(key, JSON.stringify(data), 'EX', BUILD_CACHE_TIMEOUT, callback);
}
Expand All @@ -132,7 +129,7 @@ module.exports = {
{
loader: 'cache-loader',
options: {
cacheKey,
key,
read,
write,
}
Expand Down
38 changes: 25 additions & 13 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,25 @@ const pkgVersion = require('../package.json').version;
const ENV = process.env.NODE_ENV || 'development';

const defaults = {
cacheDirectory: path.resolve('.cache-loader'),
cacheIdentifier: `cache-loader:${pkgVersion} ${ENV}`,
cacheKey,
directory: path.resolve('.cache-loader'),
identifier: `cache-loader:${pkgVersion} ${ENV}`,
key,
read,
write,
};

function loader(...args) {
const { data } = this;

const options = Object.assign({}, defaults, loaderUtils.getOptions(this));
const { write: writeFn } = options;

const callback = this.async();
const { data } = this;
const dependencies = this.getDependencies().concat(this.loaders.map(l => l.path));

const dependencies = this.getDependencies()
.concat(this.loaders.map(loader => loader.path));
const contextDependencies = this.getContextDependencies();

const toDepDetails = (dep, mapCallback) => {
fs.stat(dep, (err, stats) => {
if (err) {
Expand All @@ -45,7 +50,8 @@ function loader(...args) {
return;
}
const [deps, contextDeps] = taskResults;
writeFn(data.cacheKey, {

writeFn(data.key, {
remainingRequest: data.remainingRequest,
dependencies: deps,
contextDependencies: contextDeps,
Expand All @@ -60,12 +66,13 @@ function loader(...args) {
function pitch(remainingRequest, prevRequest, dataInput) {
const options = Object.assign({}, defaults, loaderUtils.getOptions(this));
const callback = this.async();
const { read: readFn, cacheKey: cacheKeyFn } = options;
const { read: readFn, key: keyFn } = options;
const data = dataInput;

data.remainingRequest = remainingRequest;
data.cacheKey = cacheKeyFn(options, remainingRequest);
readFn(data.cacheKey, (readErr, cacheData) => {
data.key = keyFn(options, remainingRequest);

readFn(data.key, (readErr, cacheData) => {
if (readErr) {
callback();
return;
Expand All @@ -79,21 +86,26 @@ function pitch(remainingRequest, prevRequest, dataInput) {
fs.stat(dep.path, (statErr, stats) => {
if (statErr) {
eachCallback(statErr);

return;
}
if (stats.mtime.getTime() !== dep.mtime) {
eachCallback(true);

return;
}
eachCallback();
});
}, (err) => {
if (err) {
callback();

return;
}

cacheData.dependencies.forEach(dep => this.addDependency(dep.path));
cacheData.contextDependencies.forEach(dep => this.addContextDependency(dep.path));

callback(null, ...cacheData.result);
});
});
Expand Down Expand Up @@ -142,11 +154,11 @@ function read(key, callback) {
});
}

function cacheKey(options, request) {
const { cacheIdentifier, cacheDirectory } = options;
const hash = digest(`${cacheIdentifier}\n${request}`);
function key(options, request) {
const { identifier, directory } = options;
const hash = digest(`${identifier}\n${request}`);

return path.join(cacheDirectory, `${hash}.json`);
return path.join(directory, `${hash}.json`);
}

export { loader as default, pitch };