# @casbin/redis-watcher 中文文档

规范地址：https://npmcn.com/package/%40casbin/redis-watcher/

版本：1\.2\.0
npm 元数据更新时间：2026-02-02T17:41:00.049Z
中文译文更新时间：2026-11-09T04:38:30.002Z
软件包许可：未声明

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40casbin/redis-watcher
原文对照：https://npmcn.com/package/%40casbin/redis-watcher/?lang=original
项目主页：http://casbin.org/
源码仓库：https://github.com/node-casbin/redis-watcher

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：25.5 kB，9 个文件
- 模块格式：CommonJS
- TypeScript 类型：包内自带
- 运行依赖：1 个
- 首次发布：2020-11-19T10:21:45.261Z
- 最新版本发布：2026-02-02T17:40:59.906Z
- 近 30 天下载量：17580（2026-10-09 至 2026-11-07，npm 官方下载统计）
- 已发布版本数：4（近 12 个月 1 个）

## 中文简介

节点卡宾的 Redis 监视器

## 安装

```sh
npm install @casbin/redis-watcher
```

也可以使用：`pnpm add @casbin/redis-watcher`、`yarn add @casbin/redis-watcher`、`bun add @casbin/redis-watcher`

## README

# Redis 监视器

[![CI](https://github.com/node-casbin/redis-watcher/actions/workflows/ci.yml/badge.svg)](https://github.com/node-casbin/redis-watcher/actions/workflows/ci.yml) [![Coverage Status](https://coveralls.io/repos/github/node-casbin/redis-watcher/badge.svg?branch=master)](https://coveralls.io/github/node-casbin/redis-watcher?branch=master) [![NPM version](https://img.shields.io/npm/v/@casbin/redis-watcher.svg?style=flat-square)](https://npmjs.com/package/@casbin/redis-watcher) [![NPM download](https://img.shields.io/npm/dm/@casbin/redis-watcher.svg?style=flat-square)](https://npmjs.com/package/@casbin/redis-watcher) [![Discord](https://img.shields.io/discord/1022748306096537660?logo=discord&label=discord&color=5865F2)](https://discord.gg/S5UjpzGZjN)

雷迪斯监视器是 [重写](https://redis.io/) 监视器 [节点卡斯宾](https://github.com/casbin/node-casbin)。有了这个库,Node-Casbin就可以通过Redis pub/sub在多个执行人实例中同步政策变化.

这个监视器基于 [雄风](https://github.com/luin/ioredis) 并同时支持单个 Redis 实例和 Redis 集群.

## 安装

```bash
npm install @casbin/redis-watcher
```

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

## 简单示例

使用 Redis :

```typescript
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 组 :

```typescript
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
}
```

## 获取帮助

-   [节点卡斯宾](https://github.com/casbin/node-casbin)
-   [卡斯宾论坛](https://forum.casbin.com/)

## 许可证

这个项目是在Apache 2.0许可下进行的. 见 [自由主义](https://npmcn.com/package/%40casbin/redis-watcher/LICENSE) 完整许可证文本的文件.

## 常见问题

### @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 和提交记录判断。

