@cacheable/memory 2.0.3 → 2.0.5

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
@@ -130,7 +130,7 @@ console.log(raw.expires); // e.g. 1677628495000 or null
130
130
 
131
131
  ## CacheableMemory Store Hashing
132
132
 
133
- `CacheableMemory` uses `Map` objects to store the keys and values. To make this scale past the `16,777,216 (2^24) keys` limit of a single `Map` we use a hash to balance the data across multiple `Map` objects. This is done by hashing the key and using the hash to determine which `Map` object to use. The default hashing algorithm is `djb2Hash` but you can change it by setting the `storeHashAlgorithm` property in the options. By default we set the amount of `Map` objects to `16`.
133
+ `CacheableMemory` uses `Map` objects to store the keys and values. To make this scale past the `16,777,216 (2^24) keys` limit of a single `Map` we use a hash to balance the data across multiple `Map` objects. This is done by hashing the key and using the hash to determine which `Map` object to use. The default hashing algorithm is `djb2` but you can change it by setting the `storeHashAlgorithm` property in the options. Supported algorithms include DJB2, FNV1, MURMER, and CRC32. By default we set the amount of `Map` objects to `16`.
134
134
 
135
135
  NOTE: if you are using the LRU cache feature the `lruSize` no matter how many `Map` objects you have it will be limited to the `16,777,216 (2^24) keys` limit of a single `Map` object. This is because we use a double linked list to manage the LRU cache and it is not possible to have more than `16,777,216 (2^24) keys` in a single `Map` object.
136
136
 
@@ -145,24 +145,25 @@ cache.set('key', 'value');
145
145
  const value = cache.get('key'); // value
146
146
  ```
147
147
 
148
- Here is an example of how to use the `storeHashAlgorithm` property:
148
+ Here is an example of how to use the `storeHashAlgorithm` property with supported algorithms:
149
149
 
150
150
  ```javascript
151
- import { CacheableMemory } from '@cacheable/memory';
152
- const cache = new CacheableMemory({ storeHashAlgorithm: 'sha256' });
153
- cache.set('key', 'value');
154
- const value = cache.get('key'); // value
155
- ```
151
+ import { CacheableMemory, HashAlgorithm } from '@cacheable/memory';
156
152
 
157
- If you want to provide your own hashing function you can set the `storeHashAlgorithm` property to a function that takes an object and returns a `number` that is in the range of the amount of `Map` stores you have.
153
+ // Using DJB2 (default)
154
+ const cache = new CacheableMemory({ storeHashAlgorithm: HashAlgorithm.DJB2 });
155
+
156
+ // Or other non-cryptographic algorithms for better performance
157
+ const cache2 = new CacheableMemory({ storeHashAlgorithm: HashAlgorithm.FNV1 });
158
+ const cache3 = new CacheableMemory({ storeHashAlgorithm: HashAlgorithm.MURMER });
159
+ const cache4 = new CacheableMemory({ storeHashAlgorithm: HashAlgorithm.CRC32 });
158
160
 
159
- ```javascript
160
- import { CacheableMemory } from '@cacheable/memory';
161
- const cache = new CacheableMemory({ storeHashAlgorithm: HashAlgorithm.SHA256 });
162
161
  cache.set('key', 'value');
163
162
  const value = cache.get('key'); // value
164
163
  ```
165
164
 
165
+ **Available algorithms:** DJB2 (default), FNV1, MURMER, CRC32. Note: Cryptographic algorithms (SHA-256, SHA-384, SHA-512) are not recommended for store hashing due to performance overhead.
166
+
166
167
  If you want to provide your own hashing function you can set the `storeHashAlgorithm` property to a function that takes an object and returns a `number` that is in the range of the amount of `Map` stores you have.
167
168
 
168
169
  ```javascript
@@ -232,7 +233,7 @@ As you can see from the benchmarks `CacheableMemory` is on par with other cachin
232
233
  * `lruSize`: The size of the LRU cache. Default is `0` which is unlimited.
233
234
  * `checkInterval`: The interval to check for expired keys in milliseconds. Default is `0` which is disabled.
234
235
  * `storeHashSize`: The number of `Map` objects to use for the cache. Default is `16`.
235
- * `storeHashAlgorithm`: The hashing algorithm to use for the cache. Default is `djb2Hash`.
236
+ * `storeHashAlgorithm`: The hashing algorithm to use for the cache. Default is `djb2`. Supported: DJB2, FNV1, MURMER, CRC32.
236
237
 
237
238
  ## CacheableMemory - API
238
239
 
@@ -256,7 +257,7 @@ As you can see from the benchmarks `CacheableMemory` is on par with other cachin
256
257
  * `size`: The number of keys in the cache.
257
258
  * `checkInterval`: The interval to check for expired keys in milliseconds. Default is `0` which is disabled.
258
259
  * `storeHashSize`: The number of `Map` objects to use for the cache. Default is `16`.
259
- * `storeHashAlgorithm`: The hashing algorithm to use for the cache. Default is `djb2Hash`.
260
+ * `storeHashAlgorithm`: The hashing algorithm to use for the cache. Default is `djb2`. Supported: DJB2, FNV1, MURMER, CRC32.
260
261
  * `keys`: Get the keys in the cache. Not able to be set.
261
262
  * `items`: Get the items in the cache as `CacheableStoreItem` example `{ key, value, expires? }`.
262
263
  * `store`: The hash store for the cache which is an array of `Map` objects.