node-ttl

0.2.0 • Public • Published

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)

Readme

Keywords

Package Sidebar

Install

npm i node-ttl

Weekly Downloads

122

Version

0.2.0

License

MIT

Unpacked Size

20.1 kB

Total Files

10

Last publish

Collaborators

  • pkuebler