cache-map

A TTL cache with an API compatible with an ES6 Map

Usage no npm install needed!

<script type="module">
  import cacheMap from 'https://cdn.skypack.dev/cache-map';
</script>

README

CacheMap

src/CacheMap.js:21-172

A TTL cache with an API compatible with an ES6 Map. Requires node 6+.

constructor

src/CacheMap.js:39-59

Parameters

  • ttl number The TTL for all entries added to this map
  • evictInterval number? How frequently to delete expired entries from the internal map
  • intialValues Iterable<[K, V]>? An iterable such as an array or another map containing the initial values to set in the map

Returns void

clear

src/CacheMap.js:64-66

Removes all key/value pairs from the Map object.

Returns void

delete

src/CacheMap.js:72-74

Removes any value associated with that key

Parameters

  • key K the key to delete

Returns boolean

entries

src/CacheMap.js:80-87

Returns Iterator<[K, V]> a new iterator object that contains an array of [key, value] for each element in the map that has not expired

forEach

src/CacheMap.js:94-98

Calls fn once for each non-expired key-value pair in the map object

Parameters

  • fn function (value: V, index: K, map: any): any the function to call
  • thisArg any the value of this when fn is called

Returns void

get

src/CacheMap.js:104-110

Parameters

  • key K

Returns V? the value associated with the key, or undefined if there is none or that entry has expired

has

src/CacheMap.js:115-120

Parameters

  • key K

Returns boolean indicates if the key has an associated value which hasn't expired

keys

src/CacheMap.js:125-129

Returns Iterator<K> a new iterator containing the keys of each non-expired entry in the map

set

src/CacheMap.js:135-138

Sets the value in the map, with a ttl of Date.now() + map.ttl

Parameters

  • key K
  • value V

Returns any the map

size

src/CacheMap.js:143-145

The number of non-expired key/value pairs in the map

Returns number

values

src/CacheMap.js:150-154

Returns Iterator<V> a new iterator containing the values of each non-expired entry in the map

deleteExpired

src/CacheMap.js:159-163

Removes every entry from the internal map that has already expired

Returns void