Giter Site home page Giter Site logo

nodex-cache's Introduction

nodex-cache

A simple caching module for node js that has set, get and delete methods. Keys can have a timeout (ttl) after which they expire and are deleted from the cache.

Installation

npm i --save nodex-cache

Usage

const node_cache = require('nodex-cache');
let { cache, expire } = node_cache;

Simple Cache

const simpleCache = cache();

simpleCache.set('a', 1)
simpleCache.set('b', 3)

console.log(simpleCache.remove('b')) // 3
console.log(simpleCache.get('a')) // 1
console.log(simpleCache.remove('b')) // undefined

Expiring Cache

const expirationHandler = (key, value) => {
    console.log(`expired ${key}: ${value}`) // expired b: 2
}
const expiringCache = expire(cache(), expirationHandler)

expiringCache.set('a', 1, 1000)
expiringCache.set('b', 2) //default expiry is 1000
expiringCache.set('c', 3) //default expiry is 1000

console.log(expiringCache.remove('b')) // 2
console.log(expiringCache.get('a')) // 1

setTimeout(() => {
    console.log(expiringCache.get('a')) // undefined
}, 1200)

Example - Build Setup

# install dependencies
npm install

# run the code
npm start

License

MIT © Liju Kuriakose

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.