|
10 | 10 | <img width="200" height="200" src="https://cdn.rawgit.com/webpack/media/e7485eb2/logo/icon-square-big.svg">
|
11 | 11 | </a>
|
12 | 12 | <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> |
14 | 14 | </div>
|
15 | 15 |
|
16 | 16 | <h2 align="center">Install</h2>
|
@@ -47,8 +47,11 @@ module.exports = {
|
47 | 47 |
|
48 | 48 | |Name|Type|Default|Description|
|
49 | 49 | |:--:|:--:|:-----:|:----------|
|
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.| |
52 | 55 |
|
53 | 56 | <h2 align="center">Examples</h2>
|
54 | 57 |
|
@@ -95,6 +98,82 @@ module.exports = {
|
95 | 98 | }
|
96 | 99 | ```
|
97 | 100 |
|
| 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 | + |
98 | 177 | <h2 align="center">Maintainers</h2>
|
99 | 178 |
|
100 | 179 | <table>
|
|
0 commit comments