@casbin/redis-watcher

中文文档
1.2.0发布于 2026/02/03TypeScriptCommonJS近 30 天下载 1.8万

节点卡宾的 Redis 监视器

中文使用文档
文档目录4 节
  1. 安装
  2. 简单示例
  3. 获取帮助
  4. 许可证

Redis 监视器

CI Coverage Status NPM version NPM download Discord

雷迪斯监视器是 重写 监视器 节点卡斯宾。有了这个库,Node-Casbin就可以通过Redis pub/sub在多个执行人实例中同步政策变化.

这个监视器基于 雄风 并同时支持单个 Redis 实例和 Redis 集群.

安装

npm install @casbin/redis-watcher

注:旧软件包名称 redis-watcher 国家防范机制已经贬值。 请使用 @casbin/redis-watcher 换句话说.

简单示例

使用 Redis :

import { RedisWatcher } from '@casbin/redis-watcher';
import { newEnforcer } from 'casbin';

async function myFunction() {
    // Initialize the watcher
    // See https://github.com/luin/ioredis/blob/master/API.md#new-redisport-host-options
    const watcher = await RedisWatcher.newWatcher('redis://localhost:6379/5');

    // Initialize the enforcer
    const enforcer = await newEnforcer('examples/authz_model.conf', 'examples/authz_policy.csv');

    // Set the watcher for the enforcer
    enforcer.setWatcher(watcher);

    // Update the policy
    await enforcer.addPolicy('alice', 'data1', 'read');
    
    // The policy change will be synchronized to other enforcers via Redis pub/sub
}

使用 Redis 组 :

import { RedisWatcher } from '@casbin/redis-watcher';
import { newEnforcer } from 'casbin';

async function myFunction() {
    // Initialize the watcher with Redis cluster
    // See https://github.com/luin/ioredis/blob/master/API.md#new-clusterstartupnodes-options
    const watcher = await RedisWatcher.newWatcherWithCluster([
        { port: 7000, host: 'localhost' },
        { port: 7001, host: 'localhost' },
        { port: 7002, host: 'localhost' }
    ]);

    // Initialize the enforcer
    const enforcer = await newEnforcer('examples/authz_model.conf', 'examples/authz_policy.csv');

    // Set the watcher for the enforcer
    enforcer.setWatcher(watcher);

    // Update the policy
    await enforcer.addPolicy('bob', 'data2', 'write');
    
    // The policy change will be synchronized to other enforcers via Redis pub/sub
}

获取帮助

许可证

这个项目是在Apache 2.0许可下进行的. 见 自由主义 完整许可证文本的文件.

版本与发布记录

@casbin/redis-watcher 共发布 4 个版本,首次发布于 2020/11/19;过去 12 个月发布了 1 个版本。

版本发布日期距今
1.2.0latest2026/02/039 个月前
1.0.22022/08/174 年前
1.0.12021/05/095 年前
1.0.02020/11/195 年前

关于 @casbin/redis-watcher 的常见问题

@casbin/redis-watcher 是什么?

节点卡宾的 Redis 监视器

如何安装 @casbin/redis-watcher?

在项目目录运行 npm install @casbin/redis-watcher,或使用 pnpm add / yarn add / bun add。

@casbin/redis-watcher 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@casbin/redis-watcher 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@casbin/redis-watcher 可以商用吗?

@casbin/redis-watcher 使用 未声明 许可。package.json 没有声明许可证。没有许可证的代码默认保留全部权利,商用前请联系作者或查看仓库中的 LICENSE 文件。以上为概括,不构成法律意见,请以许可原文为准。

@casbin/redis-watcher 的下载量有多少?

根据 npm 官方下载统计,@casbin/redis-watcher 在 2026/10/09 – 2026/11/07 这 30 天内被下载 17,580 次,日均约 586 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@casbin/redis-watcher 还在维护吗?

最新版本 1.2.0 发布于 2026/02/03(9 个月前);至今共发布 4 个版本,其中过去 12 个月发布了 1 个;首次发布于 2020/11/19。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。