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

Commit 060796b

Browse files
Mordredmichael-ciniawsky
authored andcommitted
feat: add support for custom cache stores (options.read/options.write) (#19)
1 parent 88dfc00 commit 060796b

File tree

3 files changed

+222
-213
lines changed

3 files changed

+222
-213
lines changed

README.md

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<img width="200" height="200" src="https://cdn.rawgit.com/webpack/media/e7485eb2/logo/icon-square-big.svg">
1111
</a>
1212
<h1>Cache Loader</h1>
13-
<p>Caches the result of following loaders on disk</p>
13+
<p>Caches the result of following loaders on disk (default) or in the database</p>
1414
</div>
1515

1616
<h2 align="center">Install</h2>
@@ -47,8 +47,11 @@ module.exports = {
4747

4848
|Name|Type|Default|Description|
4949
|:--:|:--:|:-----:|:----------|
50-
|**`cacheDirectory`**|`{String}`|`path.resolve('.cache-loader')`|Provide a cache directory where cache items should be stored|
51-
|**`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.|
50+
|**`cacheKey`**|`{Function(options, request) -> {String}}`|`undefined`|Allows you to override default cache key generator.|
51+
|**`cacheDirectory`**|`{String}`|`path.resolve('.cache-loader')`|Provide a cache directory where cache items should be stored (used for default read/write implementation)|
52+
|**`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)|
53+
|**`write`**|`{Function(cacheKey, data, callback) -> {void}}`|`undefined`|Allows you to override default write cache data to file (e.g. Redis, memcached).|
54+
|**`read`**|`{Function(cacheKey, callback) -> {void}}`|`undefined`|Allows you to override default read cache data from file.|
5255

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

@@ -95,6 +98,82 @@ module.exports = {
9598
}
9699
```
97100

101+
### Database Intergration
102+
103+
**webpack.config.js**
104+
```js
105+
const redis = require('redis'); // Or different database client - memcached, mongodb, ...
106+
const crypto = require('crypto');
107+
108+
// ...
109+
// ... connect to client
110+
// ...
111+
112+
const BUILD_CACHE_TIMEOUT = 24 * 3600; // 1 day
113+
114+
function digest(str) {
115+
return crypto.createHash('md5').update(str).digest('hex');
116+
}
117+
118+
/**
119+
* Generate own cache key
120+
*/
121+
function cacheKey(options, request) {
122+
return `build:cache:${digest(request)}`;
123+
}
124+
125+
/**
126+
* Read data from database and parse them
127+
*/
128+
function read(key, callback) {
129+
client.get(key, (err, result) => {
130+
if (err) {
131+
return callback(err);
132+
}
133+
134+
if (!result) {
135+
return callback(new Error(`Key ${key} not found`));
136+
}
137+
138+
try {
139+
let data = JSON.parse(result);
140+
callback(null, data);
141+
} catch (e) {
142+
callback(e);
143+
}
144+
});
145+
}
146+
147+
/**
148+
* Write data to database under cacheKey
149+
*/
150+
function write(key, data, callback) {
151+
client.set(key, JSON.stringify(data), 'EX', BUILD_CACHE_TIMEOUT, callback);
152+
}
153+
154+
module.exports = {
155+
module: {
156+
rules: [
157+
{
158+
test: /\.js$/,
159+
use: [
160+
{
161+
loader: 'cache-loader',
162+
options: {
163+
cacheKey,
164+
read,
165+
write,
166+
}
167+
},
168+
'babel-loader'
169+
],
170+
include: path.resolve('src')
171+
}
172+
]
173+
}
174+
}
175+
```
176+
98177
<h2 align="center">Maintainers</h2>
99178

100179
<table>

package-lock.json

Lines changed: 66 additions & 162 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)