Security News
Highlights from the 2024 Rails Community Survey
A record 2,709 developers participated in the 2024 Ruby on Rails Community Survey, revealing key tools, practices, and trends shaping the Rails ecosystem.
@textile/datastore-ttl
Advanced tools
An implementation of the Datastore interface that supports a time-to-live for key-value pairs.
An implementation of the Datastore interface that supports a time-to-live (TTL) for key-value pairs.
After the TTL expires on a given key, the entry will be automatically cleared from the datastore unless it is refreshed in the mean time. In this way you can build utilities like session managers where a given session is refreshed with each interaction but expires after a set period of time since the last interaction. This library borrows inspiration and ideas from level-ttl.
TTLDatastore
uses an internal scan every 10 seconds by default, this limits the available resolution of your TTL values, possibly delaying a delete for up to 10 seconds. The resolution can be tuned by passing the frequency
option to the constructor.
Of course, a scan takes some resources, particularly on a data store that makes heavy use of TTLs. If you don't require high accuracy for actual deletions then you can increase the frequency
. Note though that a scan only involves invoking a query that returns only the entries due to expire, so it doesn't have to manually check through all entries with a TTL. Depending on the backing Datastore, this could be reasonably efficient, or extremely slow. So keep that in mind.
You can set a default TTL value for all your keys by specifying the ttl
option to the constructor. This can be overridden by explicitly setting the TTL value on put
or by calling the top-level ttl
method.
npm i @textileio/datastore-ttl
import { Buffer } from 'buffer'
import { MemoryDatastore, Key } from 'interface-datastore'
import { TTLDatastore } from '@textile/datastore-ttl'
// Use any compliant Datastore
const child = new MemoryDatastore()
const key = new Key('foo')
const store = new TTLDatastore(child)
await ttl.put(key, Buffer.from('bar'), 1000)
// Wait 900 ms...
await sleep(900)
// Keep alive for another 100 ms from now
await ttl.ttl(key, 100)
await ttl.has(key) // true
await ttl.expiration(key) // <unix-timestamp>
await ttl.get(key) // <Buffer>
// Wait 110 ms
await sleep(110)
await ttl.has(key) // false
There are also several useful examples included in the tests.
See https://textileio.github.io/js-datastore-ttl
See the contributing file. PRs accepted!
Small note: If editing the README, please conform to the standard-readme specification.
MIT (c) 2019 Textile.io
Big thanks to the find folks behind Level/level-ttl
.
FAQs
An implementation of the Datastore interface that supports a time-to-live for key-value pairs.
The npm package @textile/datastore-ttl receives a total of 1 weekly downloads. As such, @textile/datastore-ttl popularity was classified as not popular.
We found that @textile/datastore-ttl demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A record 2,709 developers participated in the 2024 Ruby on Rails Community Survey, revealing key tools, practices, and trends shaping the Rails ecosystem.
Security News
In 2023, data breaches surged 78% from zero-day and supply chain attacks, but developers are still buried under alerts that are unable to prevent these threats.
Security News
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.