Giter Club home page Giter Club logo

mahsan's Introduction

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.

mahsan's People

Contributors

kirill89 avatar greenkeeper[bot] avatar nirhemed avatar

Stargazers

 avatar Katya Aronov avatar  avatar Slava avatar maayanyHo avatar Anatoli avatar  avatar

Watchers

Loic CHOLLIER avatar  avatar Roman Gurovich avatar James Cloos avatar marco carminati avatar Avraham avatar  avatar Jordan Aubry avatar  avatar prasanna avatar Andrey Persidsky avatar Anatoli avatar Maxime Portaz avatar  avatar  avatar David Haraszti avatar Oria Ratzon avatar Dor Amarm avatar Nimrod Milo avatar  avatar  avatar Daniel avatar Noam Fein avatar  avatar  avatar  avatar Xavier Pecheur avatar Tamas Toth avatar Asaf Taller avatar Szilárd Varga avatar Amandine Oberlin avatar Arthur Pastel avatar Helena Koncz avatar Sagi Barkol avatar  avatar  avatar Oleg avatar  avatar Udi-Yehuda Tamar avatar

mahsan's Issues

An in-range update of coveralls is breaking the build 🚨

The devDependency coveralls was updated from 3.0.8 to 3.0.9.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

coveralls is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • continuous-integration/travis-ci/push: The Travis CI build failed (Details).

Release Notes for Maintenance, dependency refinement
  • removed Istanbul, mocha dependencies
  • reverted cobertura-parse support (package dependency archived)
  • removed snyk (automated security updates from GitHub via Dependabot enabled)
  • improved Windows support
Commits

The new version differs by 15 commits.

  • 2e2b108 bump version
  • 50913ed Remove the now unused istanbul and mocha-lcov-reporter devDependencies
  • 1bceeff Revert #243.
  • 4aa11a2 Remove snyk.
  • 33eccc3 Revert "CI: use npm ci on Node.js >=8."
  • 236529b Update logger.js
  • 3a90b07 fix equality operator in logger.js
  • bbe2de5 Update package.json
  • 07ef879 Fix logger regression.
  • f58f8b9 README.md: Add GitHub Actions CI info.
  • 0488f10 Tweak README.md
  • be79dab CI: use npm ci on Node.js >=8.
  • ef51945 CI: Add Windows testing and lint
  • c202346 Fix Windows tests again.
  • 3d82534 use Coveralls GitHub action

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

An in-range update of eslint-plugin-import is breaking the build 🚨

The devDependency eslint-plugin-import was updated from 2.20.0 to 2.20.1.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

eslint-plugin-import is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • continuous-integration/travis-ci/push: The Travis CI build failed (Details).

Commits

The new version differs by 23 commits.

  • 45f0860 Bump to v2.20.1
  • 5d00854 [Fix] order: Fix alphabetize for mixed requires and imports
  • bbd166b [Fix] export: Handle function overloading in *.d.ts
  • 4665ec5 [Fix] no-absolute-path: fix a crash with invalid import syntax
  • 392c6b9 [Fix] named: for importing from a module which re-exports named exports from a node_modules module
  • cc5bde5 [Tests] named: add failing test for #1446
  • aff3a46 [meta] fix "files" field to include/exclude the proper files
  • 986ba74 docs: fix a few spelling mistakes
  • 6274d96 [Tests] set eslint-plugin/consistent-output lint rule to always require test case output assertions
  • a4d301b [meta] add missing changelog links
  • 2d42464 [Tests] only run the linter once, not on every build
  • 26f232b [Tests] add eslint-plugin-eslint-plugin internally and fix violations
  • 99647f1 [Docs]: Update Tidelift language to enterprise
  • f84d457 no-duplicates: allow duplicate if one is namespace and other not
  • 7e71b50 [Fix] extensions: Fix scope regex

There are 23 commits in total.

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.