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

ha-store

v4.0.0

Published

Efficient data fetching

Downloads

544

Readme

ha-store Node Dependencies Status


HA-store is a wrapper for your data queries, it features:

  • Smart TLRU cache for 'hot' information
  • Supports mutliple caching levels
  • Request coalescing and batching (solves the Thundering Herd problem)
  • Insightful stats and events
  • Lightweight, configurable, battle-tested

Learn how you can improve your app's performance, design and resiliancy here!

Installing

npm install ha-store

Usage

// Create your store
const store = require('ha-store');
const itemStore = store({
  resolver: getItems,
  delimiter: ['language']
});

// Define your resolver
function getItems(ids, params, contexts) {
  // Ids will be a list of all the unique requested items
  // Params will be the parameters for the request, which must be declared in the `delimiter` config of the store
  // Contexts will be the list of originating context information

  // Now perform some exensive network call or database lookup...

  // Then, respond with your data formatted into this formats:
  // { '123': { language: 'fr', name: 'fred' } }
}

// Now to use your store
itemStore.get('123', { language: 'fr' }, { requestId: '123' })
  .then(item => /* The item you requested */);

// You can even ask for more than one item at a time
itemStore.getMany(['123', '456'], { language: 'en' }, { requestId: '123' })
  .then(items => /* All the items you requested, in Promise.allSettled fashion */);

Options

Name | Required | Default | Description --- | --- | --- | --- resolver | true | - | The method to wrap, and how to interpret the returned data. Uses the format <function(ids, params)> delimiter | false | [] | The list of parameters that, when passed, generate unique results. Ex: 'language', 'view', 'fields', 'country'. These will generate different combinations of cache keys. cache | false | {   enabled: false,   tiers: [   {     store: <instance of a store>,     limit: 5000,     ttl: 300000   }   ] } | A list of storage tiers for the data. The order indicates where to look first. It's recommended to keep an instance of an in-memory store, like ha-store/stores/in-memory as the first one, and then expend to external stores like ha-store-redis. Caching options for the data - limit - the maximum number of records, and ttl - time to live for a record in milliseconds. batch | false | {   enabled: false,   delay: 50,   limit: 100 } | Batching options for the requests - delay is the amount of time to wait before sending the batch, limit is the maximum number of data items to send in a batch.

*All options are in (ms)

Monitoring and events

HA-store emits events to track cache hits, miss and outbound requests.

Event | Format | Description --- | --- | --- localCacheHit | <number> | When the requested item is present in the first listed local store (usually in-memory). cacheHit | <number> | When the requested item is present in a store. cacheMiss | <number> | When the requested item not cached or coalesced and must be fetched. coalescedHit | <number> | When a record query successfully hooks to the promise of the same record in transit. query | <object> | When a batch of requests is about to be sent, gives the detail of the query and what triggered it. queryFailed | <object> | Indicates that the batch has failed. Retry policy will dictate if it should be re-attempted. querySuccess | <object> | Indicates that the batch request was successful.

You may also want to track the amount of contexts and records stored via the size method.

Testing

npm test

Benchmarks

Read instructions here

npm run bench

Contribute

Please do! This is an open source project - if you see something that you want, open an issue or file a pull request.

I am always looking for more maintainers, as well.

License

Apache 2.0 (c) Frederic Charette