Skip to content

store key-value pairs for a limited time and get a cleanup hook before actual deletion

License

Notifications You must be signed in to change notification settings

chiefbiiko/hooked-ttl-map

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

hooked-ttl-map

build status AppVeyor Build Status


A Map subtype to be used as a temporary cache allowing for cleanup operations before any item is deleted from the map through a willDelete hook.


Get it!

npm install --save hooked-ttl-map

Usage

var HookedTtlMap = require('hooked-ttl-map')
var cache = new HookedTtlMap()

cache.set(27, 'fraud', 419) // time-to-live, key, value

console.log(cache, cache.size)

setTimeout(function () {
  console.log(cache, cache.size) // will be empty
}, 28)

API

var cache = new HookedTtlMap([ttl, iterable][, willDelete(key, value, doDelete)])

Create a new HookedTtlMap instance. If iterable is provided (must be a 2d array) all passed items get added to the map during instantiation, and deleted after ttl. If willDelete is a function it will be called as a cleanup hook before items are deleted from the set but after ttl has exceeded. Make sure to call the doDelete callback at the end of the willDelete hook to actually perform the deletion.

cache.set(ttl, key, value[, willDelete(key, value, doDelete)])

Provide ttl, time to live in ms, a key, and a value that will get deleted from the map at the end of their lifetime. If the last argument is a function it will be called as a cleanup hook before valules are deleted from the map but after ttl has exceeded. Make sure to call the doDelete callback at the end of the willDelete hook to actually perform the deletion.


License

MIT

About

store key-value pairs for a limited time and get a cleanup hook before actual deletion

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published