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-cache-pro

v1.1.0

Published

A modern excellent fast well-tested reliable Caching Module that allows to keep your data in cache and incredibly fast to get it

Downloads

13

Readme

Memory Cache Pro

A modern excellent fast well-tested reliable Caching Module that allows to keep your data in cache and incredibly fast to get it

Memory Cache Pro Logo

Installation

npm i memory-cache-pro

Usage

import cache from 'memory-cache-pro';

/* Example 1 */

cache.put('John', 'Smith');
cache.get('John'); // Smith

/* Example 2 */

cache.put('superSecretKey', 'deleteItIn5Seconds', 5 * 1000);
cache.get('superSecretKey'); // deleteItIn5Seconds

setTimeout(() => {
  cache.get('superSecretKey'); // null
}, 5 * 1000);

/* Example 3 */

cache.put(
  'mySocksAreUnderTheCouch',
  'notifyMeAboutItIn5Seconds',
  5 * 1000,
  (key, value) => {
    console.log('Your socks 🧦🧦 are under the couch');
  }
);

/* Example 4 */

import { MemoryCachePro } from 'memory-cache-pro';

const myDogCache = new MemoryCachePro();
const myCatCache = new MemoryCachePro();

myDogCache.put('rule', 'StopEatMySlippers!');
myCatCache.put('rule', 'DoNotOpenMyFridge!');

myDogCache.get('rule'); // StopEatMySlippers!
myCatCache.get('rule'); // DoNotOpenMyFridge!

API

put(key, value, time, timeoutCallback)

  • Stores a value
  • If time isn't passed in, it is stored forever
  • Will actually remove the value in the specified time in ms (via setTimeout)
  • timeoutCallback is optional function fired after entry has expired with key and value passed (key, value)
  • Returns the cached value

get(key)

  • Retrieves a value for a given key
  • If value isn't cached, returns null

delete(key)

  • Deletes a key, returns a boolean specifying whether or not the key was deleted. False when the key doesn't exist. True when the key was deleted

tryToDelete(key)

  • Deletes a key, returns a boolean specifying whether or not the key was deleted. It won't be deleted if the key has a valid expire param that hasn't expire

clear()

  • Deletes all keys in the cache

size()

  • Returns the current number of entries in the cache

memsize()

  • Returns the number of entries taking up space in the cache
  • Will usually === size() unless a setTimeout removal went wrong

debug(status)

  • Turns on or off debugging

hits()

  • Returns the number of cache hits (only monitored in debug mode)

misses()

  • Returns the number of cache misses (only monitored in debug mode)

keys()

  • Returns all the cache keys

exportToJSON()

  • Returns a JSON string representing all the cache data
  • Any timeoutCallbacks will be ignored

importFromJSON(JSON, options)

  • Merges all the data from a previous call to export into the cache
  • Any existing entries before an import will remain in the cache
  • Any duplicate keys will be overwritten, unless skipDuplicates is true
  • Any entries that would have expired since being exported will expire upon being imported (but their callbacks will not be invoked)
  • Available options:
    • skipDuplicates: If true, any duplicate keys will be ignored when importing them. Defaults to false.
  • Returns the new size of the cache

MemoryCachePro()

  • MemoryCachePro constructor
  • note that default import would return the default instance of MemoryCachePro
  • while naming import is the actual class

Contributing

Any help, ideas and suggestions to improve this package are very welcome 👍

License

MIT

Copyright (c) 2022-present, Ivan Admaers