npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nano-cache

v1.1.2

Published

simple node cache module

Downloads

5,466

Readme

nano-cache

A little in-memory cache solution for nodejs.

Features

  • In memory cache storage for any JSON serializable data.
  • Expiration configured by number of accesses or time interval.
  • Limits memory usage by total usage or minimum free memory.
  • Supports compressed memory for higher capacity
  • Eviction by Least Recently Used algorithm.
  • Stats for cache hits, memory used, and evictions.
  • Not constrained by heap memory limit (about 1.5GB, depending on platform)
  • Emits events when some actions occurs.

Installation

> npm install --save-dev nano-cache

Use

The NanoCache constructor is also a singleton which can be used directly or as a factory.

// use the default singleton
var cache = require('nano-cache');

// or construct your own
var NanoCache = require('nano-cache');
var cache = new NanoCache();

// listen to events
cache.on('del', function (deletedKey) { /* ... */ });
cache.on('get', function (accessedKey) { /* ... */ });
cache.on('set', function (setKey) { /* ... */ });
cache.on('clear', function () { /* ... */ });

new NanoCache(options)

Use of the default singleton is optional. New cache objects can be constructed with custom configurations.

var NanoCache = require('nano-cache');
var cache = new NanoCache({
    ttl: 30000,                      // max aged for cache entry
    limit: 5,                        // max hits for a cache entry
    bytes : 100 * NanoCache.SIZE.MB, // max memory use for data
});
cache.set('mykey', myvalue);

cache.set(key, value, options)

Set the item in the cache dictionary, overriding any previous value or settings for the key. The value must be a JSON-serializable object which includes simple strings, numbers, or booleans.

var cache = require('nano-cache');
NanoCache.set('mykey', myvalue, {
    ttl: 60000, // ttl 60 seconds
    limit: 10 // limits the read count to 10 times, the 10'th time will expire the cache
});

cache.get(key)

Returns the value from the cache, or null if non-existent or expired.

NanoCache.set('mykey', myvalue)
var value = NanoCache.get('mykey');

Methods

  • get(key) returns a value
  • set(key, value, options) sets a key/value pair, returns the value set
  • del(key) deletes a key/value pair, returns the value deleted
  • clear() delete everything
  • clearExpired() deletes all expired key with their values to free up memory
  • isTTLExpired(key) check if a key's ttl is up, returns true/false, always false if there is no ttl set
  • isLimitReached(key) check if a key's read count has reached its limit, returns true/false, always false if there is no limit set
  • info(key) returns information about key, including access time, hits, and expiry
  • stats() returns number of items in cache, total byte size, and hit/miss ratio

EventEmitter

Every cache instance extends the EventEmitter object, so it has all of its methods. However, the most common used ones are probably the following:

  • on(eventName, callback)
  • once(eventName, callback)
  • emit(eventName, callback)
  • removeListener(eventName)
  • removeAllListeners(eventName, listenerCallbackReference)

Native Events

Every cache instance will emit the following events.

  • 'del' when a key is deleted. Its listeners callbacks will receive the deletedKey as the 1st argument.
  • 'get' when a key is accessed. Its listeners callbacks will receive the accessedKey as the 1st argument.
  • 'set' when a key is set. Its listeners callbacks will receive the setKey as the 1st argument.
  • 'clear' when all is clear from memory. No argument is passed to the listeners.

Constructor Options

  • ttl time in msec before the item is removed from cache. defaults to null for no limit.
  • limit maximum number of reads before item is removed from cache. defaults to null for no limit.
  • bytes maximum number of bytes before an item removed from the cache. defaults to Infinity for no limit.

Advanced Options

  • compress - use compression to reduce in-memory cache size. Defaults to true, but can be disabled for improved speed at the cost of memory size.
  • minFreeMem - items will be evicted from cache if os.freemem() is lower. Defaults to 0% of total memory.
  • maxEvictBytes - maximum amount of memory to be evicted on check, which leaves time for garbage collection, defaults to 5% of total memory.

License

Copyright (c) 2017 Cxense Inc

Authors:

  • aziz.khoury
  • greg.kindel

MIT license https://opensource.org/licenses/MIT