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 29, 2017
1 parent 9a18ba9 commit d1cb832
Show file tree
Hide file tree
Showing 4 changed files with 2,294 additions and 31 deletions.
58 changes: 58 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.|
|**`writer`**|`{Function(cacheFilename, content, callback) -> {void}}`|`undefined`|Allows you to override default write cache data to file (e.g. Redis, memcached).|
|**`reader`**|`{Function(cacheFilename, callback) -> {void}}`|`undefined`|Allows you to override default read cache data from file.|

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

Expand Down Expand Up @@ -95,6 +97,62 @@ module.exports = {
}
```

### `Write cache data to redis`

**webpack.config.js**
```js
const redis = require('redis');

// ...
// ... connect to client
// ...

const BUILD_CACHE_TIMEOUT = 24 * 3600; // 1 day

function redisKey(filename) {
return `build:cache:${filename}`;
}

function reader(filename, callback) {
client.get(redisKey(filename), (err, result) => {
if (err) {
return callback(err);
}

if (!result) {
return callback(new Error(`Key ${redisKey(filename)} not found`));
}

callback(null, result);
});
}

function writer(filename, content, callback) {
client.set(redisKey(filename), content, 'EX', BUILD_CACHE_TIMEOUT, callback);
}

module.exports = {
module: {
rules: [
{
test: /\.js$/,
use: [
{
loader: 'cache-loader',
options: {
reader,
writer
}
},
'babel-loader'
],
include: path.resolve('src')
}
]
}
}
```

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

<table>
Expand Down
Loading

0 comments on commit d1cb832

Please sign in to comment.