1 | /**
|
2 | * @file cache simple value cache
|
3 | * @author Proteus team
|
4 | */
|
5 |
|
6 | ;
|
7 |
|
8 | const _ = require('lodash');
|
9 |
|
10 | /**
|
11 | * This callback type is called `requestCallback` and is displayed as a global symbol.
|
12 | *
|
13 | * @callback cacheFunction
|
14 | * @param {*} name Key under which to save
|
15 | * @param {*} value Value to save
|
16 | */
|
17 |
|
18 | /**
|
19 | * Simple value cache
|
20 | * @return {cacheFuction} caching function
|
21 | */
|
22 | module.exports = function () {
|
23 | var cache = {};
|
24 |
|
25 | return (name, value) => {
|
26 | if (cache[name]) {
|
27 | return cache[name];
|
28 | }
|
29 |
|
30 | if (_.isFunction(value)) {
|
31 | cache[name] = value();
|
32 | return cache[name];
|
33 | }
|
34 |
|
35 | cache[name] = value;
|
36 | return cache[name];
|
37 | };
|
38 | };
|