npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@textile/datastore-ttl

v0.0.5

Published

An implementation of the Datastore interface that supports a time-to-live for key-value pairs.

Downloads

10

Readme

Time to Live Datastore (datastore-ttl)

Made by Textile Chat on Slack GitHub license GitHub package.json version npm (scoped) Release Docs Workflow standard-readme compliant

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.

Table of Contents

Background

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.

Default TTL

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.

Install

npm i @textileio/datastore-ttl

Usage

import { Buffer } from 'buffer'
import { MemoryDatastore, Key } from 'interface-datastore'
import { TTLDatastore } from '@textile/datastore-ttl'

// Simple promise-based sleep function
const sleep = (ms: number) => new Promise(r => setTimeout(r, ms))

// 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.

API

See https://textileio.github.io/js-datastore-ttl

Maintainers

Carson Farmer

Contributing

See the contributing file. PRs accepted!

Small note: If editing the README, please conform to the standard-readme specification.

License

MIT (c) 2019 Textile.io

Thanks

Big thanks to the find folks behind Level/level-ttl.