{"_rev":"3-5cc1a3824112cc8c89fda1dc773d5380","time":{"created":"2023-03-16T19:14:23.571Z","0.7.0":"2023-03-16T06:26:54.627Z","modified":"2023-03-16T19:14:24.162Z","0.7.1":"2023-03-16T06:46:56.421Z","9.3.10":"2023-03-16T19:14:23.867Z"},"_id":"@davydmaker/nestjs-simple-redis-lock","name":"@davydmaker/nestjs-simple-redis-lock","dist-tags":{"latest":"9.3.10"},"versions":{"9.3.10":{"name":"@davydmaker/nestjs-simple-redis-lock","version":"9.3.10","description":"Distributed lock with single redis instance, simple and easy to use for Nestjs","main":"index.js","scripts":{"build":"rm -rf dist && tsc -p tsconfig.json","test":"echo \"Error: no test specified\" && exit 1"},"keywords":["nestjs","redis","redis lock","distributed lock"],"repository":{"type":"git","url":"git+ssh://git@github.com/davydmaker/nestjs-simple-redis-lock.git"},"author":{"name":"Alex Chen"},"license":"MIT","homepage":"https://github.com/davydmaker/nestjs-simple-redis-lock#readme","dependencies":{"@nestjs/common":"^9.3.10","@nestjs/core":"^9.3.10","@nestjs/terminus":"^9.2.1","debug":"^4.3.4","ioredis":"^5.3.1","rxjs":"^7.8.0"},"devDependencies":{"@types/debug":"^4.1.7","@types/node":"^18.15.3","typescript":"^4.9.5"},"types":"./index.d.ts","gitHead":"04a511a8303289d14f8fde582fc47da8b6a4bdca","bugs":{"url":"https://github.com/davydmaker/nestjs-simple-redis-lock/issues"},"_id":"@davydmaker/nestjs-simple-redis-lock@9.3.10","_nodeVersion":"18.14.2","_npmVersion":"9.5.0","dist":{"integrity":"sha512-0C55bZ3CN2KmljLeTBBeumyQwERvSVm+Z9VbDqkjfDwfxdlstNTm5zHzJUoTIUCE8QMBn+jxNls+cScED1xqbw==","shasum":"9c6e69c276ef7e9282c70e747320d89cdaaaf864","tarball":"https://registry.npmjs.org/@davydmaker/nestjs-simple-redis-lock/-/nestjs-simple-redis-lock-9.3.10.tgz","fileCount":27,"unpackedSize":110346,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQC2U9zzx3wOJtjuTsPx28KeFrwOkZVLhpMPPZXEMUsDcgIhAOOyMtZSQXoAQn9H0SpzAqbfH1xGFSJ/2mMKq/kft9p/"}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJkE2qPACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmoW8g/8Cjaa9cd6XTn0ZULdy1U134HM+kGUHEaKaWwsWZAOXAYqQC8o\r\ngFwYAI1JpIv8PRJnMKS5vc41s6WN2C45Ry6GeYrrXuBpNhXyZTDfNt+RTU2Z\r\n3Y8T7NhrbTJ90Yvhsf2JAEu1ey+83gnVlp7DB7jruaUMCLKE87u9WlP4sGbj\r\nMIRcxeS2eiEhNy7fdytakIHBN+wDifXAU83AhK86n4oypgtSPGC9MeKl9Mkr\r\nK/88d1J+8U1eeYAnUL1L06Rm7N1UcY76JxGdnrNrvTlWvJ/xLu74+xmfPHFz\r\nZNuzO4qU+mr7hR7Vn+Q9Z2OfQBmw15pHzFRA82vwbgfwBKdOE6B1KIpt0+iL\r\nFKmuSu2gEisOFu9muG+5KHKU7zA0Qwb4lP0dfLEBl8KAkoTu36/Th0sXj9hg\r\nE4pF2XeWCydWj9UK0V5nQDXu1uIY6qW5DBJ9pU2t9plrtygXK8sE6RLXM5up\r\nmmMiBfTS/TP5190eNb9VJQqOZ4WtSOFkXMRydWjM5SMlA/r/V09qLbR3lO+5\r\ng25TGDJ3rUzZ7skte7C5Uw6ysOQ2HSzPkpgCrJg+AV6lOav7fRgVQgnZWtrf\r\niM1ciBIGNltFXBNMiSD3knNJ1VGOSZ5iEu35n2xTwTSt3esoeoBBYqTlpkLi\r\naV09NaDzEQ9VeB8Sch1EYij0ArtLSLfpws4=\r\n=bx21\r\n-----END PGP SIGNATURE-----\r\n"},"_npmUser":{"name":"davydmaker","email":"davydmaker@gmail.com"},"directories":{},"maintainers":[{"name":"davydmaker","email":"davydmaker@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/nestjs-simple-redis-lock_9.3.10_1678994063650_0.3374863522383664"},"_hasShrinkwrap":false}},"maintainers":[{"name":"davydmaker","email":"davydmaker@gmail.com"}],"description":"Distributed lock with single redis instance, simple and easy to use for Nestjs","homepage":"https://github.com/davydmaker/nestjs-simple-redis-lock#readme","keywords":["nestjs","redis","redis lock","distributed lock"],"repository":{"type":"git","url":"git+ssh://git@github.com/davydmaker/nestjs-simple-redis-lock.git"},"author":{"name":"Alex Chen"},"bugs":{"url":"https://github.com/davydmaker/nestjs-simple-redis-lock/issues"},"license":"MIT","readme":"# @davydmaker/nestjs-simple-redis-lock\n\n**Fork of [@huangang/nestjs-simple-redis-lock](https://github.com/huangang/nestjs-simple-redis-lock) with updated dependencies and NestJS v9.**\n\nDistributed lock with single redis instance, simple and easy to use for [Nestjs](https://github.com/nestjs/nest)\n\n## Installation\n```\nnpm install @davydmaker/nestjs-simple-redis-lock\n```\n\n## Usage\nYou must install [nestjs-redis](https://github.com/liaoliaots/nestjs-redis), and use in Nest. This package use it to access redis:\n```JavaScript\n// app.ts\nimport { RedisLockModule } from '@davydmaker/nestjs-simple-redis-lock';\n\n@Module({\n  imports: [\n    ...\n    RedisModule.forRootAsync({ // import RedisModule before RedisLockModule\n      imports: [ConfigModule],\n      useFactory: (config: ConfigService) => ({\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.registerAsync({\n      useFactory: async (redisManager: RedisManager) => {\n        return { prefix: ':lock:', client: redisManager.getClient() }\n      },\n      inject: [RedisManager]\n    }), // import RedisLockModule, use default configuration\n  ]\n})\nexport class AppModule {}\n```\n### 1. Simple example\n```TypeScript\nimport { RedisLockService } from '@davydmaker/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 36000, about 1h\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 `@davydmaker/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 '@davydmaker/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 36000, about 1h\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 '@davydmaker/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) => 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) => 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) => ({\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","readmeFilename":"README.md"}