Skip to content

Commit

Permalink
docs/error clarification re size requirement
Browse files Browse the repository at this point in the history
Fix: #264
  • Loading branch information
isaacs committed Feb 16, 2023
1 parent 88abd0b commit 1d6f638
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
16 changes: 7 additions & 9 deletions README.md
Expand Up @@ -123,8 +123,8 @@ cached, and no other items will be evicted.

Optional, must be a positive integer if provided.

Sets `maxItemSize` to the same value, unless a different value is
provided for `maxItemSize`.
Sets `maxEntrySize` to the same value, unless a different value
is provided for `maxEntrySize`.

At least one of `max`, `maxSize`, or `TTL` is required. This
must be a positive integer if set.
Expand Down Expand Up @@ -157,9 +157,8 @@ This may be overridden by passing an options object to
Requires `maxSize` to be set.

If the `size` (or return value of `sizeCalculation`) for a given
entry is greater than `maxEntrySize`, or if the resulting size of
the entire cache would be greater than `maxSize`, then the item
will not be added to the cache.
entry is greater than `maxEntrySize`, then the item will not be
added to the cache.

Deprecated alias: `length`

Expand All @@ -184,7 +183,7 @@ behavior.

The `fetchMethod` should **only** return `undefined` or a Promise
resolving to `undefined` if the AbortController signaled an
`abort` event. In all other cases, it should return or resolve
`abort` event. In all other cases, it should return or resolve
to a value suitable for adding to the cache.

The `options` object is a union of the options that may be
Expand Down Expand Up @@ -491,9 +490,8 @@ will prevent calling a `dispose` function in the case of
overwrites.

If the `size` (or return value of `sizeCalculation`) for a given
entry is greater than `maxEntrySize`, or if the resulting size of
the entire cache would be greater than `maxSize`, then the item
will not be added to the cache.
entry is greater than `maxEntrySize`, then the item will not be
added to the cache.

Will update the recency of the entry.

Expand Down
23 changes: 21 additions & 2 deletions index.d.ts
Expand Up @@ -345,8 +345,12 @@ declare namespace LRUCache {
/**
* Function to calculate size of items. Useful if storing strings or
* buffers or other items where memory size depends on the object itself.
* Also note that oversized items do NOT immediately get dropped from
* the cache, though they will cause faster turnover in the storage.
*
* Items larger than {@link maxEntrySize} will not be stored in the cache.
*
* Note that when `maxSize` or `maxEntrySize` are set, every item added
* MUST have a size specified, either via a `sizeCalculation` in the
* constructor, or `sizeCalculation` or `size` options to {@link set}.
*/
sizeCalculation?: SizeCalculator<K, V>
}
Expand Down Expand Up @@ -542,8 +546,23 @@ declare namespace LRUCache {
/**
* A value for the size of the entry, prevents calls to
* {@link sizeCalculation}.
*
* Items larger than {@link maxEntrySize} will not be stored in the cache.
*
* Note that when `maxSize` or `maxEntrySize` are set, every item added
* MUST have a size specified, either via a `sizeCalculation` in the
* constructor, or `sizeCalculation` or `size` options to {@link set}.
*/
size?: number
/**
* Overrides the {@link sizeCalculation} method set in the constructor.
*
* Items larger than {@link maxEntrySize} will not be stored in the cache.
*
* Note that when `maxSize` or `maxEntrySize` are set, every item added
* MUST have a size specified, either via a `sizeCalculation` in the
* constructor, or `sizeCalculation` or `size` options to {@link set}.
*/
sizeCalculation?: SizeCalculator<K, V>
ttl?: number
start?: number
Expand Down
4 changes: 3 additions & 1 deletion index.js
Expand Up @@ -399,7 +399,9 @@ class LRUCache {
}
} else {
throw new TypeError(
'invalid size value (must be positive integer)'
'invalid size value (must be positive integer). ' +
'When maxSize or maxEntrySize is used, sizeCalculation or size ' +
'must be set.'
)
}
}
Expand Down

0 comments on commit 1d6f638

Please sign in to comment.