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

redibox-hook-memset

v1.1.0

Published

Synchronised data sets stored in memory across all servers - for quick synchronous access to data that is commonly used but not likely to update frequently.

Downloads

8

Readme

Coverage Status Downloads npm version dependencies build License

RediBox Memset

Memset is a persistent, in memory, self updating cache tool. The main difference between Memset and Cache is that Cache sets and returns data from redis using specified triggers within your code, whereas Memset provides self updating cached data from memory which is accessible synchronously.

When to use Memset

You should consider using Memset when:

  • You have persistent data that doesn't need updating on demand
  • You always need certain data available quickly and is used frequently

Installation

Firstly, ensure you have RediBox installed.

Install Memset via npm:

npm install redibox-hook-memset --save

Usage

Configure sets

Within your redibox config, we'll setup a new memset object containing a sets array. Each set item consists of a key name, runs function and interval.

  • key: A property name in which the data will be stored under and accessed within Memset.
  • runs: A function or string (which returns a function) of the data to store. This must return a promise or data.
  • interval: A string of the update time, compatible with (Later.js)(https://bunkat.github.io/later/parsers.html#text). Internally this use a similar setup to Schedule.
{
  memset: {
    sets: [{
      key: 'categories',
      runs: function(set, sets) {
        // Return a promise or data
        return ProductCategories.find({ active: true });
      },
      interval: 'every 5 minutes',
    }]
  }
}

Accessing Memset Data

When your application boots - all of the sets are run, no matter what the interval. This means your data is accessible at all times.

Very simply access the data by key name:

// assumes 'RediBox' is your predefined redibox instance
const categories = RediBox.hooks.memset.categories;

// With Sails hook
const categories = Memset.categories;

Gotchas

If your sets require other Memset data to run, keep in mind that on boot the sets run in the order provided in the array. For example:

Broken Example:

sets: [
  {
    key: 'cars',
    runs: function(set, sets) {
      // Return a promise or data
      return Cars.find({
        manufacturer: sets.carManufacturers,
      });
    },
    interval: 'every 2 minutes',
  }, {
    key: 'carManufacturers',
    runs: function(set, sets) {
      // Return a promise or data
      return CarManufacturers.find({ active: true });
    },
    interval: 'every 5 minutes',
  }
]

This won't work because on boot, the carManufacturers data hasn't been set. To fix this, the carManufacturers would need to come before cars.

Working example:

sets: [
  {
    key: 'carManufacturers',
    runs: function(set, sets) {
      // Return a promise or data
      return CarManufacturers.find({ active: true });
    },
    interval: 'every 5 minutes',
  }, {
    key: 'cars',
    runs: function(set, sets) {
      // Return a promise or data
      return Cars.find({
        manufacturer: sets.carManufacturers,
      });
    },
    interval: 'every 2 minutes',
  },
]

This will now function as expected because 'Cars' can now access to the data previously created by CarManufacturers.

Memset vs Cache

Before diving into Memset, you should first understand when to use Memset over the Cache methods.

Memset should be used for common top level datasets which are accessed across your application, which is not likely to frequently update. Cache should be used for low level specific datasets which are less likely to be accessed and frequently need updating.

Example: Imagine an online shopping website, where users are able to browse for products via category, view a single product and login to our website. The website contains around 100 categories and thousands of products.

Memset:

  1. The product categories would be commonly accessed dataset on the website, which do not change frequently. In this case we could use Memset to update the categories every 5 minutes, which would be reflected across the site to all users.

Cache:

  1. Since a single product is unlikely to be accessed frequently, and could be subject to regularly fluctuating prices changes/stock changes it would not make sense to store this in Memset. Instead Cache should be used for specific product control with low cache times or the cached product data will need to be reset on price change within the application (Cache.del()).
  2. The amount of users in the application is subject to constant change, along with users data/settings/authentication. This would not make sense to store in Memset, and should be controlled individually with custom Cache levels.