Gathering detailed insights and metrics for limited-cache
Gathering detailed insights and metrics for limited-cache
Gathering detailed insights and metrics for limited-cache
Gathering detailed insights and metrics for limited-cache
safe-memory-cache
Secure and size-limited in-memory cache for Node.js and browsers
array-limited
Simple limited arrays in length.
time-limited-file-cache
size-limited-map
A Map that discards its oldest elements to stay within a fixed size. Implements the same interface as JS's built-in Map class.
npm install limited-cache
Typescript
Module System
Node Version
NPM Version
TypeScript (80.99%)
Shell (16.19%)
JavaScript (2.82%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
9 Stars
118 Commits
1 Watchers
1 Branches
1 Contributors
Updated on May 02, 2025
Latest Version
2.2.0
Package Id
limited-cache@2.2.0
Unpacked Size
167.15 kB
Size
34.59 kB
File Count
82
NPM Version
10.2.4
Node Version
20.11.0
Published on
Feb 12, 2024
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
A minimal JS cache. Like using an object to store keys and values, except it won't grow forever
A plain Javascript object is often good enough for simple key-value caching.
The problem is that a plain object cache can grow forever -- especially if you persist it in local storage. This library
adds a size limit, plus maxCacheTime
and smarter removal of old items.
The plain API provides a standard cache interface:
1const recentResults = LimitedCache({ 2 maxCacheSize: 100, 3}); 4recentResults.set('abc', thingToSave); 5recentResults.get('abc'); 6recentResults.has('abc'); 7recentResults.getAll(); 8recentResults.reset();
Use LimitedCacheObject
for a nicer developer experience, using Proxy:
1const recentResults = LimitedCacheObject(); 2recentResults['abc'] = thingToSave;
Low-level functions using plain objects, if you need to stay serializable or want to store offline:
1const reduxState = { 2 childIdsByParentId: {}, 3 cacheMeta: limitedCacheUtil.init(), 4}; 5 6// cacheMeta is a plain, serializable object that contains the cache's internal state 7cacheMeta = limitedCacheUtil.set(cacheMeta, 'abc', thingToSave); 8 9return { 10 ...reduxState, 11 childIdsByParentId: limitedCacheUtil.getAll(cacheMeta), 12 cacheMeta, 13};
Typescript generics, to define a type for items in the cache:
1const stringCache = LimitedCache<string>(); 2const myClassCache = LimitedCacheObject<SomeClass>(); 3const offlineCacheMeta = lowLevelInit<SomeObjectShape>();
Note: The React hooks were removed in v1.0
The code for useLimitedCache
and useLimitedCacheObject
is here
if you want to implement them yourself. For most cases, a useMemo(() => LimitedCache(), []))
should be enough.
npm install limited-cache
or yarn add limited-cache
1import { LimitedCache, LimitedCacheObject, limitedCacheUtil } from 'limited-cache';
maxCacheSize
(number, default: 100)Number of key/value pairs to keep in the cache. A falsy value will make it limitless.
maxCacheTime
(milliseconds, default: 1 day, max: 1 year)Time after which an item is removed. A falsy value will make it the 1-year maximum.
warnIfItemPurgedBeforeTime
(milliseconds, default: 5000, development only)If an item rotates out of the cache before this time passes, emits a warning to suggest you increase the cache size. Use a falsy value to disable.
Under the hood, everything is tracked inside a single, serializable object (cacheMeta
) which can be persisted to
storage or kept in Redux or any other state.
You can retrieve this object from a LimitedCache or LimitedCacheObject, or create it directly via lowLevelInit
:
1myLimitedCache.getCacheMeta(); 2getCacheMetaFromObject(myLimitedCacheObject);
Do not manipulate cacheMeta directly: a set of low-level functions is available for that. Every action available on the higher-level LimitedCache and LimitedCacheObject is available as a low-level function.
lowLevelInit(options)
lowLevelGetOne(cacheMeta, cacheKey)
lowLevelGetAll(cacheMeta)
- returns the entire cache, excluding expired itemslowLevelHas(cacheMeta, cacheKey)
lowLevelSet(cacheMeta, cacheKey, value)
lowLevelRemove(cacheMeta, cacheKey)
lowLevelReset(cacheMeta)
lowLevelSetOptions(cacheMeta, options)
- you can update options anytimeThese functions are also grouped together as limitedCacheUtil -- but minimization and tree-shaking will be slightly better if you import each individually.
Immutable?
The cache itself is, but the low-level cacheMeta is persistent/mutated.
API for bulk operations?
Only reset
and getAll
. The other actions aren't as optimizable, so they're omitted to keep this small.
When are old items removed?
When new items are added, or if you try to get
an item that has expired.
Is this a least-recently-used cache?
Not by default: For performance it only tracks by set
time.
You can turn it into a least-recently-used cache by calling set
each time you get
an item, though: items will then
expire based on when they were last accessed. This case has been optimized so performance won't suffer.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 0/16 approved changesets -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
13 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-06-30
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More