Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

popular-cache

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

popular-cache

An in-memory LRU cache with built-in statistics and proxy mode.

  • 1.1.2
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

An in-memory LRU cache with easy statistics and smart proxy mode.

Installation

npm install popular-cache --save

Basic Usage

var pcache = require('popular-cache');
	cache = pcache({
		maxSize: 500,
		maxAge: 1000 * 60 * 60	// in millisecond
	}),
	smallCache = pcache(50);	// or simply sets max size

// basic usage
cache.set("key", "value")
cache.get("key")
cache.del("key")	// delete key
cache.size()		// get size
cache.reset()   	// resets cache

cache.hits()		// get total hits
cache.misses()		// get total misses

// print the most popular keys and the number of hits
cache.popular(function(key, value, hits){
	console.log(key + ': ' + hits);
}, 5);

// print the most recently used keys and the number of hits
cache.recent(function(key, value, hits){
	console.log(key + ': ' + hits);
}, 10);

Proxy Mode

Proxy mode is as simple as telling popular-cache what you want and then you just get and get. Cache misses and concurrent requests are handled automatically.

// some time consuming process like HTTP request
var httpRequest = function(key, callback){
	setTimeout(function(){
		if(key=='popular-cache') callback('I am popular-cache');
		else callback('others');
	}, 1000);
}
var cache = pcache(50).proxy(httpRequest);

cache.get('popular-cache', function(value){
	console.log(value); // I am popular-cache
});
cache.get('another key', function(value){
	console.log(value); // others
});

The only difference in proxy mode is how the value is returned.

APIs

  • set(key, value)

    • Stores a value.
    • Updates the "recently used"-ness of the entry.
    • Resets the age of the entry.
  • get(key) (Normal Mode)

    • Gets a value for a given key. Returns null if not found.
    • Updates the "recently used"-ness of the entry.
    • Increases the hits of the entry
  • get(key, function(value)) (Proxy Mode)

    • same as get(key) except how the value is returned. The callback function(value) is required in proxy mode to receive the value.
  • del(key)

    • Deletes an entry for a given key.
  • size()

    • Gets the current number of entries in the cache.
  • reset()

    • Deletes all entries in the cache.
  • hits()

    • Gets the total hits.
  • misses()

    • Gets the total misses.
  • recent(function(key, value, hits), [limit])

    • Iterate over recent used entries in reverse chronological order.
    • key: the key of the entry.
    • value: the value of the entry.
    • hits: the number of hits of the entry.
    • limit: optional, the maximum number of iterations.
  • popular(function(key, value, hits), [limit])

    • Iterate over most popular entries in descending order of hits.
    • key: the key of the entry.
    • value: the value of the entry.
    • hits: the number of hits of the entry.
    • limit: optional, the maximum number of iterations.

Keywords

FAQs

Package last updated on 27 Aug 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc