Git Product home page Git Product logo

node-memory-cache's Introduction

node-memory-cache

A lightweight memory caching module for node.js:

  • Key/Value store, except that for each key you can store multiple properties, like: key.property1 or key.property2.
  • Native support for counters.
  • You can set different expirations on properties, like: key.property1 expires every 1000ms, and key.property2 every 5000ms.

Installation

npm install node-memory-cache

Usage

var cache = require('node-memory-cache');

// Set an object property
cache.set("ObjectId", "ObjectProperty", "Value");

// Set an object property with a 3000ms expiration
cache.set("ObjectId", "ObjectPropertyWithExpiration", "Another Value", 3000);

// Get the object properties
cache.get("ObjectId", "ObjectProperty");
cache.get("ObjectId", "ObjectPropertyWithExpiration");

// Set a new property that is a counter, and increment it with +1
cache.increment("ObjectId", "CounterProperty", 1);

// Set a new property that is a counter, and increment it with +10, with a 5000ms expiration.
cache.increment("ObjectId", "CounterPropertyWithExpiration", 10, 5000);

// Decrementing a value, it is like incrementing it with a negative amount
cache.increment("ObjectId", "CounterProperty", -5);

// Expiration dates can be overwritten by specifying them again
cache.set("ObjectId", "ObjectPropertyWithExpiration", "Value", 3000);
cache.set("ObjectId", "ObjectPropertyWithExpiration", "Another Value", 5000); // Now the expiration has been updated to 5000ms

// The same for counters
cache.increment("ObjectId", "CounterPropertyWithExpiration", 1, 2000); // The counter has a 2000ms expiration

// The counter still keeps a 2000ms expiration
cache.increment("ObjectId", "CounterPropertyWithExpiration", 3); 
cache.increment("ObjectId", "CounterPropertyWithExpiration", 4);

// We update the expiration date to 5000ms while incrementing the value
cache.increment("ObjectId", "CounterPropertyWithExpiration", 1, 5000);

// To remove an expiration just set it to zero
cache.increment("ObjectId", "CounterPropertyWithExpiration", 1, 0); // We removed the expiration to this property

// Delete a property/counter
cache.delete("ObjectId", "ObjectProperty");

// Get all items stored
var data = cache.getAll();

// Initialize the cache with some predefined data: expected to be in the same format as the return value of getAll()
cache.populate(data);

Support

Open an issue on GitHub.

node-memory-cache's People

Contributors

subnetmarco avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

node-memory-cache'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.