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

mahsan

v0.2.2

Published

A simple caching module that provides you an ability to cache and invalidate your data.

Downloads

449

Readme

mahsan

Build Status NPM version Coverage Status Greenkeeper badge

NPM

Fast and simple NodeJS caching library with distributed invalidation policy

A simple caching module that provides an ability to cache and invalidate your data. Unlike other modules, Mahsan supports the following features:

  • Data is stored only in memory (without serialization/deserialization) - this why it's so fast.
  • It has a strict invalidation policy which allows clients to invalidate cached entities easily.
  • We optimize data transfer to the distributed storage in order to remain performant even on slow connections.

Install

  npm install mahsan --save

Example

Simple

// Init
const Cache = require('mahsan');
const cache = new Cache();

// ... do something ...
await cache.set('myKey', {foo: 'bar'});
// ... do something ...
const data = await cache.get('myKey');
console.log(data); // {foo: 'bar'}

Complex

const Cache = require('mahsan');
const cache = new Cache();

async getCourses() {
    // Try get data from cache.
    const data = await cache.get(['courses', 'lecturers']);

    if (data) return data;

    const dbResult = await sql(`SELECT c.name AS name, l.name AS lecturer
                                FROM courses AS c
                                LEFT JOIN lecturers AS l ON l.id = c.lecturer_id;`);

    // Save all courses names and related lecturers names in cache (in current NodeJs instance).
    await cache.set(['courses', 'lecturers'], dbResult);
    return dbResult;
}

async getLecturers() {
    // Try get data from cache.
    const data = await cache.get(['lecturers']);

    if (data) return data;

    const dbResult = await sql(`SELECT * FROM lecturers;`);

    // Save all lecturers in cache (in current NodeJs instance).
    await cache.set(['lecturers'], dbResult);
    return dbResult;
}

async deleteCourse(id) {
    await sql(`DELETE FROM courses WHERE id = ?;`, [id]);
    // Invalidate cache for "getCourses()" function.
    await cache.invalidate('courses');
}

async deleteLecturer(id) {
    await sql(`DELETE FROM lecturers WHERE id = ?;`, [id]);
    // Invalidate cache for both "getCourses()" and "getLecturers()" function.
    await cache.invalidate('lecturers');
}

API

const cache = new Cache(options)

  • options (Object)
    • ttl (Number) is a default time to live for all .set() calls in milliseconds. Infinity by default.
    • checkPeriod (Number) is a period in milliseconds, used for deleting expired data. 5 min (5 * 60 * 1000) by default.
    • manager (String|ValidityManager) is a ValidityManager instance or one predefined "InMemory" or "Redis". InMemory by default.
    • managerOptions (Object) is currently necessary only for "Redis" manager.
      • redisClient (redis client).
      • prefix (String) is a prefix for all keys in Redis.
      • ttl (Number) is a default time to live for all Redis keys in milliseconds. 7 days (7 * 24 * 60 * 60 * 1000) by default. This option is necessary in order not to clog up Redis.

await cache.set(keyParts, value, ttl)

Put the value to the cache.

  • keyParts (String|Array) is a list of keys used to save the value.
  • value (Any) is a user data.
  • ttl (Number) is a time to live in milliseconds. This argument affects only local instance cache.

await cache.get(keyParts)

Get the value from the cache.

  • keyParts (String|Array) is a list of keys used to retrieve a value previously saved with cache.set() on this NodeJS instance.

await cache.has(keyParts)

Check a value availability.

  • keyParts (String|Array) is a list of keys used to retrieve a value previously saved with cache.set() on this NodeJS instance.

await cache.delete(keyParts)

Delete a value from the cache (on this NodeJS instance).

  • keyParts (String|Array) is a list of keys used to retrieve a value previously saved with cache.set() on this NodeJS instance.

cache.clear()

Clear cache on this NodeJS instance.

await cache.invalidate(keyParts)

Invalidate keys locally (in case of Node.JS instance) or globally (in case of Redis).

  • keyParts (String|Array) is a list of keys used to retrieve a value.

ValidityManager

ValidityManager is an interface which has two methods async sign(keyParts) and async update(keyParts). ValidityManager may be override (for example, with an own implementation of a custom shared storage).

Notes

  • value argument always stored by reference. Don't modify it after setting/getting from cache.
  • mahsan (מחסן) is a storage in Hebrew.