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 🙏

© 2025 – Pkg Stats / Ryan Hefner

node-ttl

v0.2.0

Published

Time to life Cache in node.js

Readme

node-ttl Build Status

Simple Time to life (ttl) NodeJS Store

Install

  npm install node-ttl

Examples:

Initialize (INIT):

var NodeTtl = require( "node-ttl" );
var ttl = new NodeTtl();

Options

  • ttl: (default: 0) the standard ttl in seconds for every element. 0 = unlimited
  • lastUsage: (default: false) check the time on the basis of past use or creation time.
  • checkPeriode: (default: 0) How frequently should be checked for expired items. 0 = only by get()
  • timeOutFunction: (default: null) This function is called when an item has expired.

Store a key (PUSH):

ttl.push(key, [ value ], [ timeOutFunction ], [ ttl ])

Save the key. Note: Need a value or a (global/options) timeOutFunction. If the value is empty, it is loaded from the timeOutFunction.

  • key the Key.
  • value (optional) A Variable, Object or Function.
  • timeOutFunction(container) (optional) Called when the time has expired or no value is defined.
  • ttl (optional) Custom Time to life.
ttl.push('key1', 'value1');
ttl.push('key2', {age: 42});

ttl.push('key4', {name: 'Philipp'}, null, 40);

ttl.push('key5', null, function(container) {
    console.log(container);

    /*
    {
        createTime: 1553, // Create (NodeJs Run Time in Seconds)
        lastUsage: 3432, // Last call get() (NodeJs Run Time in Seconds)
        ttl: 5000, // Time to Life
        key: 'key5',
        value: null,
        timeOutFunction: // this function
    }
     */
    
    container.value = 'My new Value';
    // Time Update?
    container.createTime = os.uptime();
});

Get a key (GET):

ttl.get(key)

Get the value from key. Returns the value. If not found return null;

var value = ttl.get('key');

Get a key async (GET ASYNC):

ttl.get(key, function(value){})

Get the value from key. Returns the value. If not found return null;

ttl.get('key', function(value) {
    // null if not found.    
});

Delete a key (DEL):

ttl.del(key)

Delete a Element by the key. Returns if success 1.

var value = ttl.del(key);
console.log(value);

// Return 0 if fails

Get multiple keys (MGET)

ttl.get(keys[])

Get the values by key Array. If the value was found it returns an object with the key value pairs;

var value = ttl.get(['key1', 'key2', 'key3']);
console.log(value);

/*
{
    "key1": 'value1'
    "key2": {age: 42},
    "key3": null // Not Found
}
 */

Get multiple keys async (MGET ASYNC)

ttl.get(keys[], function(values){})

Get the values by key Array. If the value was found it returns an object with the key value pairs;

var value = ttl.get(['key1', 'key2', 'key3'], function(values) {
    console.log(values);
});

Delete multiple keys (MDELETE)

ttl.del(keys[])

Delete Elements by a keys Array. Returns count of success deletes..

var value = ttl.del(['key1', 'key2']);
console.log(value);

// Return 2

Count Keys (SIZE):

ttl.size()

Get Count of Elements.

var value = ttl.size();
console.log(value);

Clear Storage (CLEAR):

ttl.clear()

Remove all storaged Elements.

ttl.clear();

console.log(ttl.size());
// Return 0

Get Options:

ttl.getOptions()

Returns the global Options.

var value = ttl.getOptions();
console.log(value);

/*
{
    ttl: 0,
    timeOutFunction: null,
    lastUsage: false,
    checkPeriode: 0
}
 */

Events

get

Fired when found a Key. You will get the key and the value as callback argument.

ttl.on("get", function( key, value ){
});

push

Fired when added a new Element. You will get the key and the container as callback argument.

ttl.on("push", function( key, container ){
    console.log(container);
    /*
    {
        createTime: 1553, // Create (NodeJs Run Time in Seconds)
        lastUsage: 3432, // Last call get() (NodeJs Run Time in Seconds)
        ttl: 5000, // Time to Life
        key: 'key5',
        value: null,
        timeOutFunction: // this function
    }
    */
});

del

Fired when an item expires or is deleted manually. You will get the key as callback argument.

ttl.on("del", function( key ){
});

clear

Fired when you call ttl.clear().

ttl.on("clear", function(){
});

expired

Fired when an item is expired. You will get the key and the container as callback argument.

ttl.on("expired", function( key, container ){
    console.log(container);
    /*
    {
        createTime: 1553, // Create (NodeJs Run Time in Seconds)
        lastUsage: 3432, // Last call get() (NodeJs Run Time in Seconds)
        ttl: 5000, // Time to Life
        key: 'key5',
        value: null,
        timeOutFunction: // this function
    }
    */
});

error

Fired when there was an error. You will get the msg as callback argument.

ttl.on("error", function( msg ){
    console.log(msg)
});

Contributors

List of Contributors

The MIT License (MIT)