Coder Social home page Coder Social logo

smart-redis's Introduction

smart-redis

Redis cache which automatically build and refresh itself.

Motivation

There is data which take really long time to fetch from sources (e.g: reporting query, analytics query) but user need fast response time and result to be always be available. The solution is to create a cache that can build and refresh itself in the background.

If this sounds like your problem then it is exactly what you're looking for.

Features

  1. Auto build and update cache on specified interval.
  2. Zero downtime cache refresh.
  3. Support multiple application instances.

This library has been used in production for few months, but use at your own risk.

Usage

Install

npm install smart-redis --save

or

yarn add smart-redis

init

const CacheBuilder   = require('smart-redis').CacheBuilder;
let cacheBuilder = await CacheBuilder.build({
    redisOption: {
        host: 'localhost',
        port: 6379,
        password: ''
    },
    masterCacheKey: 'sample-key'
});

cache register

function sampleDataFetchingFn() {
    return [{
        'id': "id1",
        'value': 'value1'
    },
        {
            'id': "id2",
            'value': 'value2'
        },
    ]
}
cacheBuilder.register({
    dataFetchingFn: sampleDataFetchingFn,  // 
    cacheKeys: ['sample-key-list', 'sample-key-hash'],
    redisDataTypes: ['list', 'hash'],
    refreshInterval: 100,
    idProp: 'id',
    name: 'sampleCacheName'
});

cache should be built on the redis:

127.0.0.1:6379> keys *
1) "CACHE_MASTER_KEYsample-key"
2) "sample-key-hash"
3) "sample-key-list"
127.0.0.1:6379> lrange sample-key-list 0 1
1) "{\"id\":\"id1\",\"value\":\"value1\"}"
2) "{\"id\":\"id2\",\"value\":\"value2\"}"
127.0.0.1:6379> hgetall sample-key-hash
1) "id2"
2) "{\"id\":\"id2\",\"value\":\"value2\"}"
3) "id1"
4) "{\"id\":\"id1\",\"value\":\"value1\"}"
127.0.0.1:6379>

see lib/sample.js for full example.

API references

  1. CacheBuilder.build(options?): Promise entry point function to create CacheBuilder instance. options parameter:
  • redisOption: option to initialize node_redis, see here for full detail: http://redis.js.org/#api-rediscreateclient
  • masterCacheKey: optional prefix for master key, if dont have multiple CacheBuilder instance, you can safely ignore it.
  • logging: optional logging option for debugging purpose
  • logging.enable: whether enable debug logging, default is false
  • logging.logger: customized logger accept log4j and winston instance, default is built-in log4js logger
  • masterAliveCheck: whether to perform master alive check by ping master hostname, only work when application instance running on different machine that can be pinged, do not work inside Kubernetes.
  1. CacheBuilder.register(options): string function to register a new cache options parameter:
  • name: cache builder instance name, if not pass an unique name will be generated, should contains only alphanumeric, _ or -
  • dataFetchingFn: function to fetch data. 'list' data type expects an array, 'hash' data type expects an array of object.
  • cacheKeys: array of key strings,
  • redisDataTypes: array of data type corresponding to the aforementioned array of keys, only ['hash', 'list', 'string'] are supported
  • refreshInterval: interval to refresh,
  • idProp: property name to extract key use for hash data structure
    Returns: unique cache name will be auto generated if not passed

How it work?

smart-redis natively support multi application instances deployment

Cache building and refreshing

  1. Only master instance can build and refresh cache.
  2. Application can register a cache by provide a function to fetch the data, a cache key and a refresh interval.
  3. Cache will be built immediately upon registered and after each time specified refresh interval elapsed.
    1. smart-redis using the function to fetch the data, returned data is stored into cache
    2. smart-redis stored fetched data into cache corresponding to the specified data type
    3. go to sleep

Master election

Master instance is the application instance that responsible for cache building and refreshing. Only one application instance can become master.
When an application with smart-redis start, it will try to become a master instance

  1. If it becomes master: there will be a master key stored in redis pointing to this instance. cache will be built and refreshed from this instance
    • If this instance is terminated (by exit, SIGINT, or SIGTERM signal), it will release it master key.
  2. If it couldn't become master:
    • it will go to sleep and periodically check for master aliveness (if enabled):
      • If master is alive, go to sleep
      • If master is not alive, try to become master.
    • it also listen to master key deletion event. If master key got deleted, all non-master instances will try to become master.

TODO

  • better connection refused error handling
  • add support for custom parameter to the dataFetchingFn function.
  • add support for set data type.
  • encapsulate private function and constructor
  • un-register, update existing cache instance
  • add unit test
  • bug fix: disconnect will render master key monitoring disabled
  • add ES linting

smart-redis's People

Contributors

bobbui avatar bobevenup avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

xushiyan

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.