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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@heavybit/hb-cache-package

v1.0.12

Published

Shared cache package that allows microservices to set, get and delete cache data

Downloads

19

Readme

Cache Package

This is a shared caching library for microservices using Bull and Redis. It processes cache requests (get, set, delete) asynchronously using queues, providing scalability and resilience.

Overview

The CacheService is a utility designed for managing cache operations using Redis. It allows other microservices to perform basic caching functionalities, including setting, retrieving, deleting, and invalidating cache data. This service is suitable for scenarios requiring fast access to frequently used data, reducing the load on the primary database and improving overall application performance.

Features

Synchronous Cache Operations: Directly interact with Redis for immediate cache operations. Role-Based Access: Ensures that only authorized services can access or modify cache data. TTL Management: Supports setting cache expiration times. Error Handling: Logs errors and handles connection issues gracefully.

Installation

To use the CacheService in your microservice, follow these steps:

npm install @pesalink/cache-package

Environment Variables

The following environment variables can be set to configure the Redis connection:

REDIS_HOST: Hostname of the Redis server (default: 127.0.0.1).

REDIS_PORT: Port number of the Redis server (default: 6379).

REDIS_USER: Username for Redis authentication (if applicable).

REDIS_PASSWORD: Password for Redis authentication (if applicable).

Import the CacheService:

Include the CacheService in your microservice:

const CacheService = require('@pesalink/cache-package');

Initialize the CacheService:

Create an instance of the CacheService:

const cacheService = new CacheService();

Usage

Setting Cache Data

To store data in the cache:

await cacheService.setCache({ entity: 'user', id: userId }, userData);

Parameters:

keyObj: An object representing the cache key (e.g., { entity: 'user', id: userId }). value: The data to cache (e.g., user information). expiryInSeconds (optional): The time in seconds before the cache expires (default: 3600 seconds).

Getting Cache Data

To retrieve data from the cache:

const data = await cacheService.getCache({ entity: 'user', id: userId });
if (!data) {
  // Fetch from DB and set in cache if not found
}

Parameters:

keyObj: An object representing the cache key (e.g., { entity: 'user', id: userId }).

Deleting Cache Data

To delete specific cache data:

await cacheService.deleteCache({ entity: 'user', id: userId });

Parameters:

keyObj: An object representing the cache key (e.g., { entity: 'user', id: userId }).

Invalidating Cache

To invalidate a cache entry:

await cacheService.invalidateCache({ entity: 'user', id: userId });

Parameters:

keyObj: An object representing the cache key (e.g., { entity: 'user', id: userId }).

Fetching Data with Cache Support

To fetch data with cache support (returning cached data if available, otherwise fetching from the database):

const data = await cacheService.getCachedData(
  { entity: 'user', id: userId },
  async () => await fetchUserFromDB(userId) // Your method to fetch from DB
);

Parameters:

cacheKey: An object representing the cache key. dbFetchMethod: A function that fetches data from the database if the cache is empty.

Closing the Connection

To gracefully close the Redis connection when the service is no longer needed:

await cacheService.disconnect();

Error Handling

The CacheService logs errors for failed Redis operations. It is recommended to handle potential exceptions in your code:

try {
  await cacheService.setCache({ entity: 'user', id: userId }, userData);
} catch (error) {
  console.error('Failed to set cache:', error);
}

Conclusion

The CacheService provides a simple and effective way for microservices to manage cache operations with Redis. By leveraging this service, you can improve data retrieval speeds and enhance the performance of your applications.