mentie 0.2.38 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -2,3 +2,8 @@
2
2
 
3
3
  Mentor's favorite helpers.
4
4
 
5
+ ## Logging
6
+
7
+ Cross environment logging depends on the `LOGLEVEL` variable. In web it is set with `?loglevel=`, in others with an environment variable `LOGLEVEL`.
8
+
9
+ Valid values: `info`, `warn`, `error`.
package/modules/cache.js CHANGED
@@ -13,10 +13,11 @@ const _cache = {}
13
13
  * If no value is provided, it retrieves the value from the cache.
14
14
  *
15
15
  * @param {string} key - The key to cache the value.
16
- * @param {*} [value] - The value to be cached (optional).
16
+ * @param {*} [value] - The value to be cached (optional). Use null to unset, undefined is never saved when provided as a parameter
17
+ * @param {number} [expires_in_ms=Infinity] - The expiration time in milliseconds (optional).
17
18
  * @returns {*} The cached value.
18
19
  */
19
- export function cache( key, value ) {
20
+ export function cache( key, value, expires_in_ms=Infinity ) {
20
21
 
21
22
  // If the key is undefined, log a warning
22
23
  if( key === undefined ) {
@@ -28,8 +29,17 @@ export function cache( key, value ) {
28
29
  log.warn( `The cache key ${ key } contains 'undefined', this may indicate a bug in your cache logic` )
29
30
  }
30
31
 
31
- if( value ) _cache[key] = value
32
- return _cache[key]
32
+ // If value is provided, save value and expiration
33
+ if( value !== undefined ) _cache[key] = { value, expires: Date.now() + expires_in_ms }
34
+
35
+ // If cache has value, but it expired, remove it
36
+ if( _cache[key] && _cache[key].expires < Date.now() ) {
37
+ log.info( `Cache key ${ key } expired, removing from cache` )
38
+ delete _cache[key]
39
+ }
40
+
41
+ // Return the value of the cache key, which may be undefined
42
+ return _cache[key]?.value
33
43
  }
34
44
 
35
45
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mentie",
3
- "version": "0.2.38",
3
+ "version": "0.3.1",
4
4
  "description": "Mentor's toolbelt",
5
5
  "type": "module",
6
6
  "main": "index.js",