Gathering detailed insights and metrics for toad-cache
Gathering detailed insights and metrics for toad-cache
Gathering detailed insights and metrics for toad-cache
Gathering detailed insights and metrics for toad-cache
npm install toad-cache
Typescript
Module System
Min. Node Version
Node Version
NPM Version
99.7
Supply Chain
100
Quality
76.2
Maintenance
100
Vulnerability
100
License
JavaScript (94.42%)
TypeScript (5.58%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
20 Stars
80 Commits
5 Forks
4 Watchers
1 Branches
5 Contributors
Updated on May 11, 2025
Minified
Minified + Gzipped
Latest Version
3.7.0
Package Id
toad-cache@3.7.0
Unpacked Size
48.39 kB
Size
6.24 kB
File Count
7
NPM Version
10.2.3
Node Version
20.10.0
Published on
Jan 09, 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
Least-Recently-Used and First-In-First-Out caches for Client or Server.
1import { Lru, Fifo } from 'toad-cache' 2const lruCache = new Lru(max, ttl = 0) 3const fifoCache = new Fifo(max, ttl = 0)
Clears the contents of the cache
Example
1cache.clear()
Removes item from cache
param {String} key Item key
Example
1cache.delete('myKey')
Removes items from cache
param {String[]} keys Item keys
Example
1cache.deleteMany(['myKey', 'myKey2'])
Evicts the least recently used item from cache
Example
1cache.evict()
Gets expiration time for cached item
param {String} key Item key
return {Mixed} Undefined or number (epoch time)
Example
1const item = cache.expiresAt('myKey')
Item in "first" or "bottom" position
Example
1const cache = new Lru() 2 3cache.first // null - it's a new cache!
Gets cached item and marks it as recently used (pushes to the back of the list of the candidates for the eviction)
param {String} key Item key
return {Mixed} Undefined or Item value
Example
1const item = cache.get('myKey')
Gets multiple cached items and marks them as recently used (pushes to the back of the list of the candidates for the eviction)
param {String[]} keys Item keys
return {Mixed[]} Undefined or Item values
Example
1const item = cache.getMany(['myKey', 'myKey2'])
Returns an Array
of cache item keys.
return {Array} Array of keys
Example
1console.log(cache.keys())
Max items to hold in cache (1000)
Example
1const cache = new Lru(500) 2 3cache.max // 500
Item in "last" or "top" position
Example
1const cache = new Lru() 2 3cache.last // null - it's a new cache!
Sets item in cache as first
param {String} key Item key
param {Mixed} value Item value
Example
1cache.set('myKey', { prop: true })
Number of items in cache
Example
1const cache = new Lru() 2 3cache.size // 0 - it's a new cache!
Milliseconds an item will remain in cache; lazy expiration upon next get()
of an item
Example
1const cache = new Lru() 2 3cache.ttl = 3e4
In case you want to gather information on cache hit/miss/expiration ratio, as well as cache size and eviction statistics, you can use LruHitStatistics class:
1const sharedRecord = new HitStatisticsRecord() // if you want to use single record object for all of caches, create it manually and pass to each cache
2
3const cache = new LruHitStatistics({
4 cacheId: 'some-cache-id',
5 globalStatisticsRecord: sharedRecord,
6 statisticTtlInHours: 24, // how often to reset statistics. On every rotation previously accumulated data is removed
7 max: 1000,
8 ttlInMsecs: 0,
9})
You can retrieve accumulated statistics from the cache, or from the record directly:
1// this is the same 2const statistics = sharedRecord.getStatistics() 3const alsoStatistics = cache.getStatistics() 4 5/* 6{ 7 'some-cache-id': { 8 '2023-04-06': { 9 cacheSize: 100, // how many elements does cache currently have 10 evictions: 5, // how many elements were evicted due to cache being at max capacity 11 expirations: 0, // how many elements were removed during get due to their ttl being exceeded 12 hits: 0, // how many times element was successfully retrieved from cache during get 13 emptyHits: 0, // out of all hits, how many were null, undefined or ''? 14 falsyHits: 0, // out of all hits, how many were falsy? 15 misses: 1, // how many times element was not in cache or expired during get 16 invalidateOne: 1, // how many times element was invalidated individually 17 invalidateAll: 2, // how many times entire cache was invalidated 18 sets: 0, // how many times new element was added 19 }, 20 }, 21} 22 23Note that date here reflects start of the rotation. If statistics weren't rotated yet, and another day started, it will still be counted against the day of the rotation start 24*/
Copyright (c) 2023 Igor Savin
Based on tiny-lru, created by Jason Mulligan
Licensed under the MIT license.
No vulnerabilities found.
No security vulnerabilities found.