New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

@imballinstack/redis

Package Overview
Dependencies
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@imballinstack/redis

This package is a Redis client package (based on [node-redis](https://github.com/redis/node-redis)) and takes heavy inspiration from [lru-cache](https://github.com/isaacs/node-lru-cache).

latest
Source
npmnpm
Version
0.5.0
Version published
Maintainers
1
Created
Source

@imballinstack/redis

This package is a Redis client package (based on node-redis) and takes heavy inspiration from lru-cache.

Installation

To install, do either of the commands below (depending on your package manager). It is worth noting that redis here is a peer dependency, so you need to install it yourselves.

Currently this package is compatible with redis v4.

# With npm.
npm i redis @imballinstack/redis redis

# With yarn.
yarn add redis @imballinstack/redis redis

Usage

The functions stored in fetchersRecord will be pivotal when you call redisClient.fetch. Depending on the key, the params will follow the function mapped to the fetcher in fetchersRecord with that matching key.

import { RedisClient } from '@imballinstack/redis';

const redisClient = new RedisClient({
  fetchersRecord: {
    user: (userId: string) => fetchUser(userId)
  },
  processors: {
    // For example: if we do `fetchUser("1")` then the resulting key will be `user:1`.
    cacheKeyProcessor: {
      user: (userId) => userId
    }
  },
  redisClientOptions: {
    socket: {
      host: '127.0.0.1'
    }
  }
});
await redisClient.connect();

// Fetch the resource.
let user1 = await redisClient.fetch({
  key: 'user',
  params: ['1']
});

// Get the cached response (from before).
user1 = await redisClient.fetch({
  key: 'user',
  params: ['1']
});

// These 2 will just result in 1 fetch, because the params are the same.
const users = await Promise.all([
  redisClient.fetch({
    key: 'user',
    params: ['1']
  }),
  redisClient.fetch({
    key: 'user',
    params: ['2']
  })
]);

// Or, fetch them together. This will result in 1 roundtrip to Redis + N fetches to the domain service, where N is number of cache miss.
const users2 = await redisClient.fetchMultiple([
  {
    key: 'user',
    params: ['1']
  },
  {
    key: 'user',
    params: ['2']
  }
]);

Development

We are using Yarn Modern, so you will need to have at least Node 18. You will need to install the dependencies first by running the command below.

yarn

Testing

We are running tests with docker-compose for the Redis container. The RedisClient for the tests are using the following options to play around container network issue.

socket: {
  host: '127.0.0.1';
}

After that, do:

# Run automated tests.
yarn test

# Run a single-run test.
yarn test:script

License

MIT

FAQs

Package last updated on 31 Jan 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts