Coder Social home page Coder Social logo

jacktuck / fleeting Goto Github PK

View Code? Open in Web Editor NEW
3.0 1.0 1.0 19 KB

LRU (least-recently-used) in-memory cache for Node.js (less than 300 LOC!) — fast, efficient and with no dependencies.

License: MIT License

JavaScript 100.00%
lru cache cache-storage nodejs freshness algo

fleeting's Introduction

Fleeting

A LRU (least-recently-used) in-memory cache for Node.js (less than 300 LOC!).

Coverage Status Build Status FOSSA Status

Installation

$ npm install fleeting

API

  • purge() drop the entire cache
  • peek(key) => value return the value without bolstering its freshness
  • has(key) => value return the presence of the key without bolstering its freshness
  • get(key) => value return the value and bolsters its freshness
  • set(key, value) => value add a node to the cache
  • del(key) => value remove a node from the cache

Usage

var Fleeting = require('fleeting');

/*
    Here we set a ttl of 3 seconds and the max number of nodes to 5
*/
var fleeting = new Fleeting({
  ttl: 3000,
  max: 5
});

fleeting.on('evicted', function(i) {
  console.log('Evicted node: ', i);
});

/*
    Add 10 nodes to the cache with 0-9 as their keys and values. Notice that
    once we have 0-4 (5 nodes), we start to evict the least-recently-used nodes
    to make way for nodes 5-9.
*/
for (var i = 0; i < 10; i++) {
  console.log('Added node: ', i);
  fleeting.set(i, i);
}

/*
    At this point, we are left with nodes 5-9, which should also be evicted
    once they expire. Here we're expiring after 3 seconds, so I do a setTimeout
    to execute a .get on nodes 5-9 in 3 seconds time.
*/
setTimeout(function() {
  for (var i = 5; i < 10; i++) {
    console.log('Getting node: ', i);
    fleeting.get(i);
  }
}, 3000);

/*
    This should be your output:

    Added node:  0
    Added node:  1
    Added node:  2
    Added node:  3
    Added node:  4
    Added node:  5
    Evicted node:  { k: 0, v: 0, stale: false }
    Added node:  6
    Evicted node:  { k: 1, v: 1, stale: false }
    Added node:  7
    Evicted node:  { k: 2, v: 2, stale: false }
    Added node:  8
    Evicted node:  { k: 3, v: 3, stale: false }
    Added node:  9
    Evicted node:  { k: 4, v: 4, stale: false }
    Getting node:  5
    Getting node:  6
    Getting node:  7
    Getting node:  8
    Getting node:  9
    Evicted node:  { k: 5, v: 5, stale: true }
    Evicted node:  { k: 6, v: 6, stale: true }
    Evicted node:  { k: 7, v: 7, stale: true }
    Evicted node:  { k: 8, v: 8, stale: true }
    Evicted node:  { k: 9, v: 9, stale: true }
*/

License

FOSSA Status

fleeting's People

Contributors

fossabot avatar jacktuck avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Forkers

fossabot

fleeting's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.