UNPKG

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