mentie 0.3.8 → 0.3.9

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.
Files changed (2) hide show
  1. package/modules/cache.js +41 -0
  2. package/package.json +1 -1
package/modules/cache.js CHANGED
@@ -42,6 +42,47 @@ export function cache( key, value, expires_in_ms=Infinity ) {
42
42
  return _cache[key]?.value
43
43
  }
44
44
 
45
+ /**
46
+ *
47
+ * @returns {Object} - A copy of the cache object.
48
+ */
49
+ cache.dump = () => {
50
+
51
+ // Return a copy of the cache object
52
+ return { ..._cache }
53
+
54
+ }
55
+
56
+ /**
57
+ *
58
+ * @returns {Object} stats - An object containing statistics about the cache.
59
+ * @returns {number} stats.keys - The number of keys in the cache.
60
+ * @returns {number} stats.size_bytes - The size of the cache in bytes.
61
+ * @returns {string} stats.size_mib - The size of the cache in MiB, rounded to 2 decimal places.
62
+ * @returns {string} stats.size_gib - The size of the cache in GiB, rounded to 2 decimal places.
63
+ */
64
+ cache.stats = () => {
65
+
66
+ // Get the number of keys in the cache
67
+ const keys = Object.keys( _cache ).length
68
+
69
+ // Get the size in bytes of the cache
70
+ const size_bytes = keys.reduce( ( acc, key ) => acc + JSON.stringify( _cache[key] ).length, 0 )
71
+
72
+ // Calculate side to MiB and GiB, both rounded to 2 decimal places
73
+ const size_mib = ( size_bytes / ( 1024 * 1024 ) ).toFixed( 2 )
74
+ const size_gib = ( size_bytes / ( 1024 * 1024 * 1024 ) ).toFixed( 2 )
75
+
76
+ // Return the stats
77
+ return {
78
+ keys,
79
+ size_bytes,
80
+ size_mib,
81
+ size_gib
82
+ }
83
+
84
+ }
85
+
45
86
  /**
46
87
  * Function to inspect concurrency.
47
88
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mentie",
3
- "version": "0.3.8",
3
+ "version": "0.3.9",
4
4
  "description": "Mentor's toolbelt",
5
5
  "type": "module",
6
6
  "main": "index.js",