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

hotpath

v1.0.0

Published

Memory restricted hot path cache

Downloads

17

Readme

Hotpath

From bigpipe.ioVersion npmBuild StatusDependenciesCoverage Status

Hotpath is memory restricted cache layer. It's intended use is to cache the most critical code or data of your application without using to much memory. Optimizations for the sake of performance are great as long as they do not affect the memory management of your process.

Our use case:

Severing files directly out of node's memory minimizes the about of I/O we need to serve the given file. The restricted hot path cache ensures that only the top requested files get cached and we do not blow out of memory. It only takes a small percentage of free memory. 2% by default and 10% if you have more than 1.7 GB available on your system.

Installation

The module is released frequently to the npm registry and can be installed using:

npm install --save hotpath

Usage

In all code example we assume that the library has been required as following:

'use strict';

var HotPath = require('hotpath');

To create a new hot path cache you need to construct a new instance:

var hotpath = new HotPath({ option });

To customize your HotPath instance you can supply the following options in the constructor:

  • maximum: The maximum amount of memory we can allocate for our given cache. Defaults to 1.7 gb the value is parsed with the bytes module.
  • available: The amount of memory available on the system. We will calculate our cache size based on this. It should be given as bytes. Defaults to the os.freemem() result.
  • prefix: A prefix for the keys to prevent dictionary attacks when storing things like __proto__ in the object. Defaults to _HotPath
  • countkey: Include the size of they in the allocated memory count. Defaults to false.

hotpath.set

To add items to your cache you need to call the set method with 2 argument:

  1. key: name where your data is stored under.
  2. data: value that needs to be stored.

If the supplied data is not an Buffer we will automatically transform it in to a new buffer for you. So when you retrieve your data from the cache again it will always be a Buffer instance.

var stored = hotpath.set('my-custom-key', new Buffer('<my important blob of data>'));

If you item is successfully stored in the cache it will return true as boolean. The only reason why you would get a false boolean is when the data you want to store is larger than the available size.

hotpath.allocated

The .allocated property allows you to check how many bytes are currently stored in your cache. To see how much data you can still store you can subtract it from the .free property.

console.log('in-use', hotpath.allocated);
console.log('available', hotpath.free - hotpath.allocated);

hotpath.get

Retrieve the value that is stored in the cache. Please note that any modification you do on this buffer will also be changed in the cache as this is NOT a copy. The get method accepts one argument:

  1. key: name where your data is stored under.
var value = hotpath.get('my-custom-key');

hotpath.remove

To remove individual values from the cache you can use the remove method. It takes one argument:

  1. key: name where your data is stored under.
var removed = hotpath.remove('my-custom-key');

The remove method returns a boolean indicating if the removal was successful. When you receive false we could not locate the item in the cache.

hotpath.reset

If you want to reset the complete cache instead of removing just a single individual value you should call the reset method. It creates a fresh new object where all data is stored in and reset the allocated property.

hotpath.reset();

hotpath.destroy

If you no longer which to use or reference your hot path cache you should destroy it completely in order to release the saved memory again. Only call this method if you will no longer reference any of it's methods as all the internal structure will be nuked to death.

hotpath.destroy();

License

MIT