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

memory-cache2

v3.0.0

Published

Creates an object cache in memory for use in browser or node. Cache instances are observable and can listen for events.

Downloads

692

Readme

Memory Cache

Instantiates a mutable object cache in memory. Uses Map and Symbol primitives, so it will only work on modern browsers (versions >= 2015), and node 4+. See benchmarks here.

Note about previous versions:

If you're using previous versions, You can find v1 documentation here and v2 documentation here and you can install using memory-cache2@1 and memory-cache2@2. I have dropped support for functionality like getIn, setIn, concatIn, mergeIn. Instead, I opted refactoring to the more performant JS maps, only allowing merge and concat, and adding other features like each, has, and size. I also refactored the event emitter to only allow observing and not triggering. Triggering should only occur internally, as the cache does things.

API

Installation

npm install --save memory-cache2

Usage

Instantiate

You can either pass an existing object and convert it into a cache, or you can start a brand new cache.


const MemoryCache = require('memory-cache');

// Empty cache
const cache = new MemoryCache;

// or

// Pass obj as cache
const cache = new MemoryCache( myObj || returnObj() );
Set

Defines key value. Returns value.


cache.set('testing', { set: true, arr: [1,2,3] });
Get

Returns key value.


const val = cache.get('testing');

// > { set: true, arr: [1,2,3] }

const entireCache = cache.get(true);
// > { testing: { set: true, arr: [1,2,3] }}
Remove

Remove key from cache.


cache.remove('testing');
// > true
Reset

Resets cache object to empty object.


cache.reset();
// > {}
Expire

Expire cache after a certain amount of time in milliseconds.


cache.expire('testing', 3000);
// Expires in 3 seconds
Set and Expire

Set the cache and expire after a certain amount of time in miliseconds.


cache.set('testing', { value: 1 }, 3000);
// > { value: 1 }
// Expires in 3 seconds
Merge

Merge objects stored in cache. Works with arrays or objects only.


const val = cache.merge('testing', { merged: true });
// > { set: true, arr: [1,2,3], merged: true }
Concat

Concatenates cached array.


cache.set('array', ['a', 'b', 'c']);
cache.concat('array', [1,2,3]);
// > ['a', 'b', 'c', 1, 2, 3];
Has

Checks to see if cache has a key


cache.has('array')
// > true

cache.has('what')
// > false
Each

Iterates through cache.


cache.each(function (value, key) {

    console.log(value, key);
});
Keys

Gets all cached keys


cache.keys;
// > ['testing', 'array']
Size

Get size of cache


cache.size;
// > 2
Debug

Turns logging off or on.


// Instantiate cache with debugging
const cache = new MemoryCache({}, {
    debug: true
});

// Set debugging after instantiation
cache.debug = true || false;

cache.set('testing', { set: true });
// MemoryCache: set testing { set: true }

cache.get('testing');
// MemoryCache: get testing
Events

MemoryCache events can be observed. All events return an object with at least key and value, except for reset. You can bind or unbind to any event using the following API:

  • cache.onGet((obj) => {}): Binds a function to get event.
  • cache.oneGet((obj) => {}): Binds a function 1 time to get event.
  • cache.offGet(/* instantiated fn */): Remove a function from get event.
  • cache.onSet((obj) => {}): Binds a function to set event.
  • cache.oneSet((obj) => {}): Binds a function 1 time to set event.
  • cache.offSet(/* instantiated fn */): Remove a function from set event.
  • cache.onRemove((obj) => {}): Binds a function to remove event.
  • cache.oneRemove((obj) => {}): Binds a function 1 time to remove event.
  • cache.offRemove(/* instantiated fn */): Remove a function from remove event.
  • cache.onExpire((obj) => {}): Binds a function to expire event.
  • cache.oneExpire((obj) => {}): Binds a function 1 time to expire event.
  • cache.offExpire(/* instantiated fn */): Remove a function from expire event.
  • cache.onMerge((obj) => {}): Binds a function to merge event.
  • cache.oneMerge((obj) => {}): Binds a function 1 time to merge event.
  • cache.offMerge(/* instantiated fn */): Remove a function from merge event.
  • cache.onConcat((obj) => {}): Binds a function to concat event.
  • cache.oneConcat((obj) => {}): Binds a function 1 time to concat event.
  • cache.offConcat(/* instantiated fn */): Remove a function from concat event.
  • cache.onReset((obj) => {}): Binds a function to reset event.
  • cache.oneReset((obj) => {}): Binds a function 1 time to reset event.
  • cache.offReset(/* instantiated fn */): Remove a function from reset event.

Example:


// Instantiate cache with event binding
const cache = new MemoryCache({}, {
    events: true
});

// Set event binding after instantiation
cache.events = true || false;

// Happens on every get
cache.onGet(function(opts) {

    fs.writeFileSync(`${opts.name}.json`, JSON.strigify(opts.value));
});

// Only happens one time
cache.oneReset(function() {

    console.warn('Cache was reset!!!');
});

Contributing

  • Include 100% test coverage.
  • Must support older browsers.
  • Submit an issue first for significant changes.