Git Product home page Git Product logo

node-lru-native's Introduction

Abandoned !!! Move to node-lru-addon

node-lru-native

Build Status

This is an implementation of a simple in-memory cache for node.js, supporting LRU (least-recently-used) eviction and TTL expirations.

It was developed as an alternative to the (excellent) node-lru-cache library for use with hashes with a very large number of items. V8 normally does a good job of optimizing the in-memory representation of objects, but it isn't optimized for an object that holds a huge amount of data. When you add a very large number of properties (particularly with non-integer keys) to an object, performance begins to suffer.

Rather than rely on V8 to figure out what we're trying to do, node-lru-native is a light wrapper around std::unordered_map from C++11. A std::list is used to track accesses so we can evict the least-recently-used item when necessary.

Based on the node-hashtable library by Issac Wagner.

Usage

Install via npm:

$ npm install lru-native

Then:

var LRUCache = require('lru-native');
var cache = new LRUCache({ maxElements: 1000 });
cache.set('some-key', 42);
var value = cache.get('some-key');

If you'd like to tinker, you can build the extension using node-gyp:

$ npm install -g node-gyp
$ node-gyp configure
$ node-gyp build

Configuration

To configure the cache, you can pass a hash to the LRUCache constructor with the following options:

var cache = new LRUCache({

  // The maximum number of items to add to the cache before evicting the least-recently-used item.
  // Default: 0, meaning there is no maximum.
  maxElements: 10000,

  // The maximum age (in milliseconds) of an item.
  // The item will be removed if get() is called and the item is too old.
  // Default: 0, meaning items will never expire.
  maxAge: 60000,

  // The initial number of items for which space should be allocated.
  // The cache will resize dynamically if necessary.
  size: 1000,

  // The maximum load factor for buckets in the unordered_map.
  // Typically you won't need to change this.
  maxLoadFactor: 2.0

});

API

set(key, value)

Adds the specified item to the cache with the specified key.

get(key)

Returns the item with the specified key, or undefined if no item exists with that key.

remove(key)

Removes the item with the specified key if it exists.

clear()

Removes all items from the cache.

size()

Returns the number of items in the cache.

stats()

Returns a hash containing internal information about the cache.

Changelog

  • 0.4.0 -- Use nan to across node version
  • 0.3.0 -- Changed memory allocation strategy, fixed issue where remove() would do a seek through the LRU list, code cleanup
  • 0.2.0 -- Fixed issue where maxAge-based removal would result in a seek through the LRU list
  • 0.1.0 -- Initial release

node-lru-native's People

Contributors

denghongcai avatar nkohari avatar skyblue avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

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.