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

nstarter-redis

v0.2.0

Published

nstarter redis 数据库连接管理组件

Downloads

26

Readme

nstarter-redis

nstarter-redis 用于对 nstarter 项目的 redis 数据库连接进行关联,包含连接操作的封装,维持策略,数据库连接的配置结构定义等内容。

安装

npm install -s nstarter-redis

组件定义

在 nstarter 工程中,使用简单的组件封装定义,即可将数据库连接实例包装为全局公用的 nstarter 组件实例。

import { BaseComponent, component } from 'nstarter-core';
import { config } from '../config';
import { LuaScripts } from '../lua';
import { RedisAdapter } from './lib/database/redis.adapter';
import { RedisConnector } from 'nstarter-redis';

@component()
export class RedisComponent extends BaseComponent {
    private readonly _redis: RedisConnector;

    constructor() {
        super();
        this._redis = new RedisConnector(config.storage.redis);
        this._redis.on('ready', () => {
            this.setReady(true);
        });
        this._redis.loadLuaScripts(LuaScripts);
    }

    public isReady(): boolean {
        return this._redis.isReady();
    }

    public get redis() {
        return this._redis.getClient<RedisAdapter>();
    }

    public async shutdown() {
        this._redis.disconnect();
    }
}

扩展redis方法

/**
 * Copyright (c) 2015-2022, FineX, All Rights Reserved.
 * @author Zed
 * @date 2022/8/8
 */
import { IRedis } from 'nstarter-redis';

export interface RedisAdapter extends IRedis {
    /**
     * 读写锁加锁
     * @param {string[]} lockArgs - 加锁 key
     * @param {Callback} callback - 回调函数
     * @example redis.readWriteLock(['aaa', 'X', 'bbb', 'IX', 'ccc', 'S', 'ddd', 'IS'], _.noop)
     */
    readWriteLock: (lockArgs: string[], callback: Callback) => void;
    /**
     * 读写锁解锁
     * @param {string[]} lockArgs - 解锁 key 列表
     * @param {Callback} callback - 回调函数
     * @example redis.readWriteUnlock(['aaa', ':X:1', 'bbb', ':IX:2', 'ccc', ':S:3', 'ddd', ':IS:4'], _.noop)
     */
    readWriteUnlock: (lockArgs: string[], callback: Callback) => void;
}

配置结构

nstarter-redis 提供了以下的配置参数,可用于配置服务器连接方式,支持 Standalone, Sentinels, Cluster 三种不同的服务器拓扑架构。支持ssl,密码/无密码模式认证。

redis:
  username: zed
  password: passw0rd
  host: localhost
  port: 6379
  sentinels:
  - host: server-1
    port: 6379
  - host: server-2
    port: 6379
  db: database
  ssl: true
  lazyConnect: false
  isCluster: false