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

redis-cache-manager

v1.0.93

Published

Cache data using Redis with pub/sub implementation

Downloads

19

Readme

Redis Cache Manager

Use Redis to easily cache items and subscribing to them using Redis built in pub/sub.

Installation

run npm install redis-cache-manager --save

API

Init

import { RedisCacheManager } from 'redis-cache-manager';

// Constructor expects same config as redis client type (with redis defaults) with namespace property added for scoping
const rcm = new RedisCacheManager({namespace: 'my-app-name'});

Simple keys

Simple keys take the entire value given and pass it to JSON.stringify - stored as key - string pair in Redis

Set

// Currently set only uses JSON.stringify to create a key and a string of stored values.
rcm.set('my-key', {});
// Optionally you can pass a listener function to subscribe to changes in object (for every 'set' called on key)
// The data passed in the listener is the same type as the object passed.
rcm.set('my-key', {}, (myNewlySetData) => {
    // Do something with myNewlySetData
});

Get

// Currently set only uses JSON.parse to parse data from simple redis key and use in code.
const myData = rcm.get('my-key');
// Use myData

SetAll

// The method expects an array of objects, using the callback to create a unique identifier (lodash style).
// The method creates a list under my-namespace:my-key and hm objects with params stringified in each  my-namespace:my-key:uniqueId
rcm.setAll('my-key', dataArray, (singleObject) => singleObject.uniqueId)
            .then(redisClient => {
                // You can do something with the redis client object
            })
            .catch(err => {
                // Handle error
            });

GetAll

// Looks for a list of keys under 'my-namespace:my-key' and gets all objects.
rcm.getAll('my-key')
            .then(data => {
                // You can do something with the data
            })
            .catch(err => {
                // Handle error
            });

GetByIds

// Looks for a list of simple keys under 'my-namespace:my-key' and gets all objects matching ids.
rcm.getByIds('my-key', ['id1', 'id2', 'id3'])
            .then(data => {
                // You can do something with the data
            })
            .catch(err => {
                // Handle error
            });

HM keys

Saved as hashes in Redis - none atomic values (arrays, objects etc..) are converted to string using JSON.stringify before stored

HmSetAll

// Currently uses JSON.stringify on every parameter of given object in the array.
// The method expects an array of objects, using the callback to create a unique identifier (lodash style).
// The method creates a list under my-namespace:my-key and hm objects with params stringified in each  my-namespace:my-key:uniqueId
rcm.hmSetAll('my-key', dataArray, (singleObject) => singleObject.uniqueId)
            .then(redisClient => {
                // You can do something with the redis client object
            })
            .catch(err => {
                // Handle error
            });

HmSetOne

// Currently uses JSON.stringify on every parameter of given object.
// The method expects an object, using the callback to create a unique identifier (lodash style).
// The method creates hm object with params stringified in  my-namespace:my-key:uniqueId
rcm.hmSetOne('my-key', (singleObject) => singleObject.uniqueId, singleObject)
            .then(redisClient => {
                // You can do something with the redis client object
            })
            .catch(err => {
                // Handle error
            });

HmGetAll

// Looks for a list of hm keys under 'my-namespace:my-key' and gets all objects.
rcm.hmGetAll('my-key')
            .then(data => {
                // You can do something with the data
            })
            .catch(err => {
                // Handle error
            });

HmGetByIds

// Looks for a list of hm keys under 'my-namespace:my-key' and gets all objects matching ids.
rcm.hmGetByIds('my-key', ['id1', 'id2', 'id3'])
            .then(data => {
                // You can do something with the data
            })
            .catch(err => {
                // Handle error
            });

Indexes

You can set and get indexes (stored as sets) based on field names - can be used for quick filtering of data.

IndexByFields

// Sets indexes based on each item of data array and specific fields setting it in redis as namespace:indexes:indexKey:fieldName:someFieldValue : listOfIds
rcm.indexByFields(indexKey, dataArray, fieldNames, (singleObject) => singleObject.uniqueId)
            .then(done => {
                // Indexes set correctly
            })
            .catch(err => {
                // Handle error
            });

GetIndexByFields

// Gets all the indexed under a certain indexKey based on field names
rcm.getIndexByFields(indexKey, fieldNames)
            .then(data => {
                // Returns { [indexKey]: listOfIds } you can use for fetching ids or querying DB 
            })
            .catch(err => {
                // Handle error
            });

GetAllIndexs

// Sets indexes based on each item of data array and specific fields setting it in redis as namespace:indexes:indexKey:fieldName:someFieldValue : listOfIds
rcm.getAllIndexes(indexKey)
            .then(indexes => {
                // All indexes under key returned as { [key]: listOfIds} 
                // If no indexKey passed - all indexed will return
            })
            .catch(err => {
                // Handle error
            });

Listeners

Each setting of value in RCM is publishing data (stored in key) on the key name as Redis channel.
You can listen to changes on key, or key pattern

KeyChange

// Add a listener for a key. 
rcm.keyChange('my-key', (myNewlySetData) => {
    // Do something with myNewlySetData
});

KeysChange

// Add a listener for a global pattern under a key. (this will look for any change under 'my-key:*')
rcm.keysChange('my-key', (myNewlySetData) => {
    // Do something with myNewlySetData
});

Closing connection

Quit/Unref

// You can use quit/unref to quit or unref client and sub subscriber
rcm.quit();
rcm.unref();

Listeners on same key will override one another (listeners in set and keyChange)

Roadmap

  • [ ] Support arrays and 'psubscribe' functionality
  • [ ] Support advance data parsing (break down objects)
  • [ ] Add express dedicated middleware for generic api cache
  • [ ] Add ORM style module (relations etc...)