Code Monkey home page Code Monkey logo

cachd's Introduction

Cachd

A fast TTL Cache

for node >= 4.2

npm Travis node

About the memory mangement:

The cache stops caching more items if the memory is getting full (> 70% of the heap used). It accepts new items but removes the oldest ones before the ttl or max size is reached.

API

Create cache instance

var Cachd = require('cachd');


var myCache = new Cachd({
      ttl: 3600000                  // max age of items in msec (default: 3600000 -> 1h)
    , maxLength: 1000               // the maximum of items to store (default: 10000)
    , removalStrategy: 'leastUsed'  // remove the least used items if the cache is getting
                                    // too full (default: oldest)
});

Add item. You have to pass a unique hash and the value.

myCache.set(hash, value);

// alias of set
myCache.add(hash, value);

Check if an item exists

if (myCache.has(hash)) {
    // there it is ;)
}

Get an item

var item = myCache.get(hash);

Remove an item

myCache.remove(hash);

Get all hashes

var hashMap = myCache.getHashMap();

Get first cached item. This is the most accessed item if the cache has the «leastUsed» removal strategy, else its the most recent added item.

var item = mycache.getFirst();

Get last cached item. This is the least accessed item if the cache has the «leastUsed» removal strategy, else its the least recent added item.

var item = mycache.getFirst();

Events

the cache emits events when an item is added or one is removed

myCache.on('add', function(hash, value) {

});


myCache.on('remove', function(hash, value) {

});

cachd's People

Watchers

 avatar  avatar

cachd's Issues

A typo in README

There is the following typo in README.md:

About the memory mangement

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.