{"id":20408268,"url":"https://github.com/hanfengsan/nestjs-simple-redis-lock","last_synced_at":"2025-06-28T17:08:09.983Z","repository":{"id":35155915,"uuid":"213890145","full_name":"hanFengSan/nestjs-simple-redis-lock","owner":"hanFengSan","description":"Distributed lock with single redis instance, simple and easy to use for NestJS","archived":false,"fork":false,"pushed_at":"2023-05-14T12:48:59.000Z","size":29,"stargazers_count":45,"open_issues_count":5,"forks_count":19,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-06-18T06:05:55.557Z","etag":null,"topics":["distributed-lock","nestjs","redis-lock","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hanFengSan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-10-09T10:36:18.000Z","updated_at":"2025-01-17T05:10:18.000Z","dependencies_parsed_at":"2024-06-21T13:14:36.251Z","dependency_job_id":"4f9a027c-f841-48fb-89c6-3073959d1145","html_url":"https://github.com/hanFengSan/nestjs-simple-redis-lock","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/hanFengSan/nestjs-simple-redis-lock","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hanFengSan%2Fnestjs-simple-redis-lock","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hanFengSan%2Fnestjs-simple-redis-lock/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hanFengSan%2Fnestjs-simple-redis-lock/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hanFengSan%2Fnestjs-simple-redis-lock/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hanFengSan","download_url":"https://codeload.github.com/hanFengSan/nestjs-simple-redis-lock/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hanFengSan%2Fnestjs-simple-redis-lock/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262465785,"owners_count":23315641,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["distributed-lock","nestjs","redis-lock","typescript"],"created_at":"2024-11-15T05:29:36.564Z","updated_at":"2025-06-28T17:08:09.966Z","avatar_url":"https://github.com/hanFengSan.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# nestjs-simple-redis-lock\nDistributed lock with single redis instance, simple and easy to use for [Nestjs](https://github.com/nestjs/nest)\n\n## Installation\n```\nnpm install nestjs-simple-redis-lock\n```\n\n## Usage\nYou must install [nestjs-redis](https://github.com/kyknow/nestjs-redis), and use in Nest. This package use it to access redis:\n```JavaScript\n// app.ts\nimport { RedisModule } from 'nestjs-redis';\nimport { RedisLockModule } from 'nestjs-simple-redis-lock';\n\n@Module({\n  imports: [\n    ...\n    RedisModule.forRootAsync({ // import RedisModule before RedisLockModule\n      imports: [ConfigModule],\n      useFactory: (config: ConfigService) =\u003e ({\n        host: config.get('REDIS_HOST'),\n        port: config.get('REDIS_PORT'),\n        db: parseInt(config.get('REDIS_DB'), 10),\n        password: config.get('REDIS_PASSWORD'),\n        keyPrefix: config.get('REDIS_KEY_PREFIX'),\n      }),\n      inject: [ConfigService],\n    }),\n    RedisLockModule.register({}), // import RedisLockModule, use default configuration\n  ]\n})\nexport class AppModule {}\n```\n### 1. Simple example\n```TypeScript\nimport { RedisLockService } from 'nestjs-simple-redis-lock';\n\nexport class FooService {\n  constructor(\n    protected readonly lockService: RedisLockService, // inject RedisLockService \n  ) {}\n\n  async test1() {\n    try {\n      /**\n       * Get a lock by name\n       * Automatically unlock after 1min\n       * Try again after 100ms\n       * The max times to retry is 600, about 1min\n       */\n      await this.lockService.lock('test1');\n      // Do somethings\n    } finally { // use 'finally' to ensure unlocking\n      this.lockService.unlock('test1'); // unlock\n      // Or: await this.lockService.unlock('test1'); wait for the unlocking\n    }\n  }\n  \n  async test2() {\n    /**\n     * Automatically unlock after 2min\n     * Try again after 50ms if failed\n     * The max times to retry is 100\n     */\n    await this.lockService.lock('test1', 2 * 60 * 1000, 50, 100);\n    // Do somethings\n    await this.lockService.setTTL('test1', 60000); // Renewal the lock when the program is very time consuming, avoiding automatically unlock\n    this.lockService.unlock('test1');\n  }\n}\n```\n\n### 2. Example by using decorator\nUsing `nestjs-simple-redis-lock` by decorator, the locking and unlocking will be very easy.\nSimple example with constant lock name:\n```TypeScript\nimport { RedisLockService, RedisLock } from 'nestjs-simple-redis-lock';\n\nexport class FooService {\n  constructor(\n    protected readonly lockService: RedisLockService, // inject RedisLockService \n  ) {}\n\n  /**\n   * Wrap the method, starting with getting a lock, ending with unlocking\n   * The first parameter is lock name\n   * By default, automatically unlock after 1min.\n   * By default, try again after 100ms if failed\n   * By default, the max times to retry is 600, about 1min\n   */\n  @RedisLock('test2')\n  async test1() {\n    // Do somethings\n    return 'some values';\n  }\n\n  /**\n   * Automatically unlock after 2min\n   * Try again after 50ms if failed\n   * The max times to retry is 100\n   */ \n  @RedisLock('test2', 2 * 60 * 1000, 50, 100)\n  async test2() {\n    // Do somethings\n    return 'some values';\n  }\n}\n```\n\nThe first parameter of this decorator is a powerful function. It can use to determinate lock name by many ways. \nSimple example with dynamic lock name:\n```TypeScript\nimport { RedisLockService, RedisLock } from 'nestjs-simple-redis-lock';\n\nexport class FooService {\n  lockName = 'test3';\n\n  constructor(\n    protected readonly lockService: RedisLockService, // inject RedisLockService \n  ) {}\n\n  /**\n   * Determinate lock name from 'this'\n   * The first parameter is 'this', so you can access any member in 'this' for create a dynamic lock name.\n   */\n  @RedisLock((target) =\u003e target.lockName)\n  async test1() {\n    // Do somethings\n    return 'some values';\n  }\n\n  /**\n   * Determinate lock name from the parameters of the method\n   * The original parameters also pass to the function, so you can determinate the lock name by the parameters.\n   */\n  @RedisLock((target, param1, param2) =\u003e param1 + param2)\n  async test2(param1, param2) {\n    // Do somethings\n    return 'some values';\n  }\n}\n```\n\n## Configuration\n* Register:*\n```TypeScript\n@Module({\n  imports: [\n    RedisLockModule.register({\n      clientName: 'client_name', // the Redis client name in nestjs-redis, to use specific Redis client. Default to use default client\n      prefix: 'my_lock:', // By default, the prefix is 'lock:'\n    })\n  ]\n})\n```\n*Async register:*\n```TypeScript\n@Module({\n  imports: [\n    RedisLockModule.registerAsync({\n          imports: [ConfigModule],\n          useFactory: async (config: ConfigService) =\u003e ({\n            clientName: config.get('REDIS_LOCK_CLIENT_NAME')\n          }),\n          inject: [ConfigService],\n        }),\n  ]\n})\n```\n\n## Debug\nAdd a environment variable `DEBUG=nestjs-simple-redis-lock` when start application to check log:\n```json\n// package.json\n{\n  \"scripts\": {\n    \"start:dev\": \"DEBUG=nestjs-simple-redis-lock tsc-watch -p tsconfig.build.json --onSuccess \\\"node dist/main.js\\\"\",\n  }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhanfengsan%2Fnestjs-simple-redis-lock","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhanfengsan%2Fnestjs-simple-redis-lock","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhanfengsan%2Fnestjs-simple-redis-lock/lists"}