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

memo-cache

v1.1.9

Published

A memoization and caching library for NodeJS

Downloads

6

Readme

memo-cache

A memoization and caching library for NodeJS.

Dependencies Build Status Downloads NPM version Maintainability Known Vulnerabilities

Installation

$ npm install memo-cache

Usage

let memoCache = require('memo-cache');

API

Caching:

memoCache.cache.create(cacheName, options)
  • cacheName String - name of the cache
  • options Object - options for the cache, specifying any of the following:
    • cloneValues Boolean - should returned values be clones of the original? [Default: false]
    • maxSize Integer - maximum number of keys to store in this cache; if null, then unlimited [Default: null]

Return Value: An object with the following functions to modify the created cache. Please note that these functions do not require the cacheName.

  • set : function (key, value)
  • get : function (key)
  • getAll : function ()
  • exists : function (key)
  • clear : function ()
  • size : function ()
  • options : function ()
let myCache = memoCache.cache.create('myCache');
console.log(myCache);
// Output:
// { set: [Function],     -- function(key, value)
//  get: [Function],      -- function(key)
//  getAll: [Function]    -- function ()
//  exists: [Function],   -- function(key)
//  clear: [Function],    -- function()
//  size: [Function],     -- function()
//  options: [Function] } -- function()
memoCache.cache.set(cacheName, key, value)
  • cacheName String - name of the cache
  • key String - key to be used to store the value
  • value Any - value to be stored in the cache

Return Value: If the item is stored, then the stored value is returned, otherwise null is returned.

memoCache.cache.set('myCache', 'isExample', true);
// OR (if using the myCache variable from above):
myCache.set('isExample', true);
memoCache.cache.get(cacheName, key)
  • cacheName String - name of the cache
  • key String - key to be used to retrieve the value

Return Value: If there is an item stored at the given key, then the stored value is returned, otherwise null is returned.

memoCache.cache.get('myCache', 'isExample'); // => true
memoCache.cache.get('myCache', 'isNotExample'); // => null
// OR (if using the myCache variable from above):
myCache.get('isExample'); // => true
myCache.get('isNotExample'); // => null
memoCache.cache.getAll(cacheName)
  • cacheName String - name of the cache

Return Value: If there are items stored in the cache, they will be returned as a JS document.

memoCache.cache.getAll('myCache'); // => {}
memoCache.cache.get('notAValidCache'); // => null
// OR (if using the myCache variable from above):
myCache.getAll('isExample'); // => {}
myCache.getAll('notAValidCache'); // => null
memoCache.cache.remove(cacheName, key)
  • cacheName String - name of the cache
  • key String - key to be deleted

Return Value: If there is an item stored at the given key, then the stored value is returned, otherwise null is returned.

memoCache.cache.remove('myCache', 'isExample'); // => true
memoCache.cache.remove('myCache', 'isNotExample'); // => null
// OR (if using the myCache variable from above):
myCache.remove('isExample'); // => true
myCache.remove('isNotExample'); // => null
memoCache.cache.exists(cacheName, key)
  • cacheName String - name of the cache
  • key String - key to be checked

Return Value: If there is an item stored at the given key, then true is returned, otherwise false is returned.

memoCache.cache.exists('myCache', 'isExample'); // => true
memoCache.cache.exists('myCache', 'isNotExample'); // => false
// OR (if using the myCache variable from above):
myCache.exists('isExample'); // => true
myCache.exists('isNotExample'); // => false
memoCache.cache.clear(cacheName)
  • cacheName String - name of the cache

Return Value: If the cache is cleared, then true is returned, otherwise false is returned.

memoCache.cache.clear('myCache'); // => true
// OR (if using the myCache variable from above):
myCache.clear(); // => true
memoCache.cache.size(cacheName)
  • cacheName String - (optional) name of the cache; If not specified, the size of all caches is returned

Return Value: The size of the cache(s).

memoCache.cache.size('myCache'); // => 1
memoCache.cache.size(); // => 1 
// OR (if using the myCache variable from above):
myCache.size(); // => 1
memoCache.cache.options(cacheName)
  • cacheName String - name of the cache

Return Value: If the cache exists, then the options object is returned, otherwise null is returned.

memoCache.cache.options('myCache'); // => { cloneValues: boolean, maxSize: Number, memoHashFunction: Function }
// OR (if using the myCache variable from above):
myCache.options(); // => { cloneValues: boolean, maxSize: Number, memoHashFunction: Function }

Memoization:

memoCache.memoize(function, options)
  • cacheName String - name of the cache
  • options Object - options for the cache, specifying any of the following:
  • cloneValues Boolean - should returned values be clones of the original? [Default: false]
  • maxSize Integer - maximum number of keys to store in this cache; if null, then unlimited [Default: null]
  • memoHashFunction Function - used to map the input arguments to a String. The result of this function becomes the key for the function result value
let memoCache = require('memo-cache');
let myFunction = function (aString) { console.log('cache miss!'); return aString; };

let myFunctionMemoized = memoCache.memoize(myFunction, {maxSize: 10});
myFunctionMemoized('testing'); // => 'testing' (Prints 'cache miss!' to the console)
myFunctionMemoized('testing'); // => 'testing' (Does not print 'cache miss!')

Tests

$ npm test

Note: This requires mocha, should, async, and underscore.

Features

  • Cache Functionality via memoCache.cache
  • Can create multiple caches (allowing 'namespaces')
  • Memoization Functionality via memoCache.memoize()
  • Least Recently Used implementation when options.maxSize specified
  • Caching and Memoization included in one module (usually separate)