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

coa-redis

v1.7.0

Published

redis component for coa

Downloads

7

Readme

coa-redis

GitHub license npm version npm downloads PRs Welcome

English | 简体中文

Redis database components for coajs, including data cache, message queue, timing task, distributed lock, etc.

Feature

  • Functional: Basic data connection based on ioredis. Pay attention to performance, full-featured
  • Lightweight: Only hundreds of lines of code, do not rely on other third-party libraries
  • TypeScript: All written in TypeScript, type constraint, IDE friendship

Component

  • Data Cache RedisCache: Data cache for Key-Value type
  • Message Queue RedisQueue RedisQueueWorker: Lightweight and efficient message queue based on Redis implementation
  • Timing Task RedisCron: Perform tasks on time through the cron expression and message queue
  • Distributed Lock RedisLock: Basic distributed lock mechanism by Redis

Quick Start

Install

yarn add coa-redis

Basic configuration

import { RedisBin } from 'coa-redis'

const redisConfig = {
  // host address
  host: '127.0.0.1',
  // port
  port: 6379,
  // password, if not write an empty string
  password: '123456',
  // database, default is 0
  db: 0,
  // Key prefix, distinguish between different project
  prefix: 'pre_',
  // Whether to display a query statement
  trace: false,
}

// Create a configuration instance, follow-up all components depending on this configuration instance
// Generally a database only needs to use an instance, the internal management connection pool
const redisBin = new RedisBin(redisConfig)

Component usage

Data Cache

Basic usage

// Create a cache instance
const redisCache = new RedisCache(redisBin)

// Note: In order to constrain the isolation of module data, all operations of the cache instance need to pass nsp parameters and cannot be omitted.
// Generally, the module name can be used as nsp, and the data between each nsp is isolated from each other.

// Set cache data
await redisCache.set('module1', 'id001', 'value001', 5 * 60 * 1000 /*5 minutes*/) // 1

// Read cache data
await redisCache.get('module1', 'id001') // value001

// Delete cache data (support to delete multiple data under the same nsp)
await redisCache.delete('module1', ['id001', 'id002']) // 2

Batch operate

// Batch set cache data
await redisCache.mSet('module1', { id101: 'value101' }, 5 * 60 * 1000 /*5 minutes*/) // 1
await redisCache.mSet('module2', { id201: 'value201', id202: { name: 'A2', title: 'a2' } }, 5 * 60 * 1000 /*5 minutes*/) // 2

// Batch read cache data
await redisCache.mGet('module1', ['id101']) // 'value101'
await redisCache.mGet('module2', ['id201', 'id202']) // { id201: 'value201', id202: { name: 'A2', title: 'a2' }

// Batch delete cache data (support to delete multiple data under different nsp)
await redisCache.mDelete([
  ['module1', ['id101']],
  ['module2', ['id201', 'id202']],
]) // 3

Syntactic sugar

// Get cache data, if there is no existence, follow the method to read and save
const resultWarp1 = await redisCache.warp(
  'module1',
  'id301',
  () => {
    // Do something here
    return Math.random() // return result
  },
  10 * 60 * 1000 /*10 minutes*/
)

resultWarp1 // return 0.3745813097015189 with in 10 minutes

// This Syntactic sugar                                                                                is equivalent to the following
async function getAndSetCache(nsp: string, id: string) {
  let result = await redisCache.get(nsp, id)
  if (result === undefined) {
    // Do something here
    result = Math.random() // get result
    await redisCache.set(nsp, id, result, 10 * 60 * 1000 /*10 minutes*/)
  }
  return result
}

// Batch get cache data
const resultWarp2 = await redisCache.mWarp(
  'module1',
  ['id301', 'id302'],
  ids => {
    const result = {} as { [id: string]: number }
    for (const id of ids) {
      // Do something here
      result[id] = Math.random()
    }
    return result // Return the result, be sure to ensure the key value
  },
  10 * 60 * 1000 /*10 minutes*/
)

resultWarp2 // { id301: 0.32430600236596074, id302: 0.29829421673682566 }

Message Queue

// Define a queue name and message type name
const QUEUE_NAME_1 = 'CHANNEL-1',
  MESSAGE_NAME_1 = 'NORMAL-MESSAGE-1'

// Define a message queue
const queue = new RedisQueue(redisBin, QUEUE_NAME_1)

// Define the consumer of the queue
const worker = new RedisQueueWorker(queue)
worker.on(MESSAGE_NAME_1, async (id, data) => {
  console.log(`message id is ${id}, message included with ${data}`)
})

// Produce a message
await queue.push(MESSAGE_NAME_1, 'message-id-001', { value: '001' }) // 1

Timing Task

// Timing task relies on the consumer of the message queue, and at the same time, you must specify the version number to avoid version upgrade instant task conflicts
const cron = new RedisCron(quque.worker, env.version)

// Execute at 10:00 and 16:00 each day
cron.on('0 0 10,16 * * *', () => {
  /**Do something**/
})

// Execute at 0:30 each day
cron.on('0 30 0 * * *', () => {
  /**Do something**/
})

// Execute every 10 minutes
cron.on('0 */10 * * * *', () => {
  /**Do something**/
})

// Execute at 0:00 on every Monday, Wednesday, Friday
cron.on('0 0 0 * * 1,3,5', () => {
  /**Do something**/
})

// Execute at 0:00 on every month of 1 day and 16
cron.on('0 0 0 1,16 * *', () => {
  /**Do something**/
})

Distributed Lock

// Create a lock method instance
const redisLock = new RedisLock(redisBin)

// Blocking execution, if the call is repeated, will wait for the last call to complete
await redisLock.start('lock-for-user-register', () => {
  // Do something, this thing will not be executed concurrently
})

// Try to execute, if it is repeated call, it will throw the RedisLock.Running error
await redisLock.try('lock-for-user-register', () => {
  // Do something, this thing will not be executed concurrently
})

// The throttle is executed, only 1 second is allowed once, and the repeat call will be queued to wait for the next 1 second.
await redisLock.throttle(
  'lock-for-user-register',
  () => {
    // Do something, this thing will not be executed concurrently
  },
  1000 /*1 second*/
)