1 |
|
2 |
|
3 | const mm = require('micromatch')
|
4 | const cache = new Map()
|
5 |
|
6 | /**
|
7 | * Gets an object from the cache.
|
8 | * @public
|
9 | * @param {String} key - Identifier of the cached object.
|
10 | * @returns {?Object} obj - Cached object.
|
11 | */
|
12 | const _get = function(key) {
|
13 |
|
14 | return cache.get(key)
|
15 |
|
16 | }
|
17 |
|
18 | /**
|
19 | * Saves an object in the cache.
|
20 | * @public
|
21 | * @param {String} key - Identifier for the cached object.
|
22 | * @param {Object} obj - Object which should be cached.
|
23 | */
|
24 | const _set = function(key, obj) {
|
25 |
|
26 | obj = Object.assign({}, obj)
|
27 |
|
28 | cache.set(key, obj)
|
29 |
|
30 | }
|
31 |
|
32 | /**
|
33 | * Deletes matching data when a file path is given or all data when no caching information given.
|
34 | * @public
|
35 | * @param {String} filePath - File that triggers a cache flush (relative).
|
36 | */
|
37 | const _flush = function(filePath) {
|
38 |
|
39 | cache.forEach((value, key) => {
|
40 |
|
41 | // Delete entry directly when no caching information available
|
42 | if (value.cache==null) return cache.delete(key)
|
43 |
|
44 | // Look if the current entry is affected by the file path
|
45 | const isAffected = mm.any(filePath, value.cache)
|
46 |
|
47 | if (isAffected===true) cache.delete(key)
|
48 |
|
49 | })
|
50 |
|
51 | }
|
52 |
|
53 | /**
|
54 | * @public
|
55 | */
|
56 | module.exports = {
|
57 | get: _get,
|
58 | set: _set,
|
59 | flush: _flush
|
60 | } |
\ | No newline at end of file |