@redpill-paris/quidol-redis-cache

    3.0.0 • Public • Published

    quidol-redis-cache

    Quidol Cache used for our real time show and REST API.

    • Async-mutex included in this package for a reliable cache.
    • Only one connection used per redis Instance, if you instanciante multiple QuidolCache this will not open a new connection but use a connection already open.

    Install

      yarn add @redpill-paris/quidol-redis-cache
    

    Configuration

    Redis Standalone

    const QuidolCache = require('@redpill-paris/quidol-redis-cache');
    
    const redisOptions = {
      host: 'localhost',
      port: '6379',
    }
    
    const cache = new QuidolCache({ redisOptions });

    Redis Cluster

    const QuidolCache = require('@redpill-paris/quidol-redis-cache');
    
    const redisClusterOptions = [
      {
        host: 'localhost',
        port: '6379',
      },
      {
        host: 'localhost',
        port: '6380',
      },
    ]
    
    const cache = new QuidolCache({ redisClusterOptions, type: 'cluster' });

    Redis Cluster

    const QuidolCache = require('@redpill-paris/quidol-redis-cache');
    
    const redisSentinelOptions = {
      sentinels: [
        { host: "sentinel-1", port: 26379 },
        { host: "sentinel-2", port: 26379 },
        { host: "sentinel-3", port: 26379 }
      ],
      name: "mymaster"
    }
    
    const cache = new QuidolCache({ redisSentinelOptions, type: 'sentinel' });

    Parameters available:

    • redisOptions: compatible with all options used in the connect from ioRedis.
    • defaultTTL: default expiration key in seconds(default 60).
    • type: cluster or standalone default(standalone).
    • redisClusterOptions: options passed to Redis.Cluster();

    Methods

    • get:
      cache.get(key, storeFunction(optionnal))

    return a Promise. If the cache is invalid or null the storeFunction will be executed and the result of this function will be stored in the cache.

    const userInfo = await cache.get(
      `userInfo:${userId}`,
      async () => {
        /* Some Async things, fetch user info from DB or other sources.
        ** The method passed in parameter can be sync or async it doesn't matter everything is handled in the package.
        */
        ...
        return userInfo
      }
    );

    del:

    cache.del(key)

    Return a Promise.

    await cache.del(`userInfo:${userId}`);

    delAll

    cache.delAll(match, count)

    Return a Promise.

    await cache.delAll('userInfo:*', 100);

    set:

    cache.set(key, value)

    Return a Promise

    await cache.set(`userInfo:${userId}`, {
      admin: true,
      nickname: 'Kubessandra',
    });

    Exemple:

    const QuidolCache = require('@redpill-paris/quidol-redis-cache');
    
    const redisOptions = {
      host: 'localhost',
      port: '6379',
    }
    const cache = new QuidolCache({ redisOptions });
    
    // Exemple for fetching user info with a cache of 60secs
    
    const userId = '123456';
    const userInfo = await cache.get(
      `userInfo:${userId}`,
      async () => {
        /* Some Async things, fetch user info from DB or other sources.
        ** The method passed in parameter can be sync or async it doesn't matter everything is handled in the package.
        */
        ...
        return userInfo
      }
    );
    
    // I can now use my userInfo without spamming the database everytime.
    console.log(userInfo);
    
    // If the user is Updated, you can del or set the key to invalide the cache and requesting a new fetch on the next req.
    await cache.del(`userInfo:${userId}`);
    
    // If there are multiple users, you can delete all this keys
    await cache.delAll('userInfo:*', 100);

    Install

    npm i @redpill-paris/quidol-redis-cache

    DownloadsWeekly Downloads

    43

    Version

    3.0.0

    License

    MIT

    Unpacked Size

    16.4 kB

    Total Files

    6

    Last publish

    Collaborators

    • the-1aw
    • insomniak
    • samspam9
    • alexandrelac
    • teva-redpill