{"id":19419019,"url":"https://github.com/anchan828/nest-cache","last_synced_at":"2025-04-24T13:35:36.254Z","repository":{"id":37049807,"uuid":"502414517","full_name":"anchan828/nest-cache","owner":"anchan828","description":null,"archived":false,"fork":false,"pushed_at":"2024-10-29T04:13:04.000Z","size":3521,"stargazers_count":2,"open_issues_count":2,"forks_count":0,"subscribers_count":4,"default_branch":"main","last_synced_at":"2024-10-30T01:09:14.282Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/anchan828.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":"2022-06-11T17:32:07.000Z","updated_at":"2024-10-29T04:13:07.000Z","dependencies_parsed_at":"2024-02-23T00:22:24.043Z","dependency_job_id":"88716486-50ee-4e35-9413-84a6499f68dc","html_url":"https://github.com/anchan828/nest-cache","commit_stats":{"total_commits":1105,"total_committers":5,"mean_commits":221.0,"dds":"0.19999999999999996","last_synced_commit":"02c643f682a3d4cfed98d0aacedda722eb7d3850"},"previous_names":[],"tags_count":131,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anchan828%2Fnest-cache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anchan828%2Fnest-cache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anchan828%2Fnest-cache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anchan828%2Fnest-cache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/anchan828","download_url":"https://codeload.github.com/anchan828/nest-cache/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223825936,"owners_count":17209469,"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":[],"created_at":"2024-11-10T13:15:54.692Z","updated_at":"2024-11-10T13:15:55.248Z","avatar_url":"https://github.com/anchan828.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# @anchan828/nest-cache\n\n![npm](https://img.shields.io/npm/v/@anchan828/nest-cache.svg)\n![NPM](https://img.shields.io/npm/l/@anchan828/nest-cache.svg)\n\nA cache module for Nest framework (node.js) https://nestjs.com/\n\n## Installation\n\n```bash\n$ npm i --save @anchan828/nest-cache\n```\n\n## Quick Start\n\n- Import module\n\n```ts\n@Module({\n  imports: [CacheModule.register()],\n})\nexport class AppModule {}\n```\n\n```ts\n@Injectable()\nexport class ExampleService {\n  constructor(private readonly cacheService: CacheService) {}\n\n  private items: Item[] = Array(5)\n    .fill(0)\n    .map((_, index) =\u003e ({ id: index, name: `Item ${index}` }));\n\n  public async getItems(userId: number): Promise\u003cItem[]\u003e {\n    const cacheKey = `users/${userId}/items`;\n\n    const cache = await this.cacheService.get\u003cItem[]\u003e(cacheKey);\n\n    if (cache) {\n      return cache;\n    }\n\n    await this.cacheService.set(cacheKey, this.items);\n\n    return this.items;\n  }\n}\n```\n\n## Middleware\n\nYou can add middleware that is executed just before calling the cache method. It can be used as an interceptor to process the cache key or the value to be stored, or to define dependencies on the cache to manipulate other caches under certain conditions.\n\n```ts\n@Injectable()\nexport class ExampleService {\n  constructor(private readonly cacheService: CacheService) {}\n\n  public async update(userId: number, age: number): Promise\u003cvoid\u003e {\n    await this.cacheService.set(`users/${userId}`, age, 10, {\n      /**\n       * You can pass information to be processed under specific conditions used in middleware.\n       */\n      source: { userId },\n    });\n  }\n}\n\n@CacheMiddleware({\n  /**\n   * The priority of the middleware. The higher the number, the low the priority.\n   */\n  priority: 1,\n})\nclass TestCacheMiddleware implements ICacheMiddleware {\n  constructor(private readonly cacheService: CacheService) {}\n  /**\n   * If you want to set a hook for set, implement the set method.\n   */\n  async set(context: CacheContext\u003c\"set\"\u003e): Promise\u003cvoid\u003e {\n    /**\n     * Change data\n     */\n    context.key = `version-1/${context.key}`;\n    context.value = { data: context.value };\n    context.ttl = 1000;\n\n    /**\n     * Get the source passed from the set method\n     */\n    const source = context.getSource\u003c{ userId: number }\u003e();\n\n    /**\n     * Manage other caches under certain conditions\n     */\n    if (source?.userId === 1) {\n      this.cacheService.delete(\"another-cache-key\");\n    }\n  }\n\n  /**\n   * You can define middleware for most methods.\n   */\n  // ttl?(context: CacheContext\u003c\"ttl\"\u003e): Promise\u003cvoid\u003e;\n  // delete?(context: CacheContext\u003c\"delete\"\u003e): Promise\u003cvoid\u003e;\n  // mget?(context: CacheContext\u003c\"mget\"\u003e): Promise\u003cvoid\u003e;\n  // mset?(context: CacheContext\u003c\"mset\"\u003e): Promise\u003cvoid\u003e;\n  // mdel?(context: CacheContext\u003c\"mdel\"\u003e): Promise\u003cvoid\u003e;\n  // hget?(context: CacheContext\u003c\"hget\"\u003e): Promise\u003cvoid\u003e;\n  // hset?(context: CacheContext\u003c\"hset\"\u003e): Promise\u003cvoid\u003e;\n  // hdel?(context: CacheContext\u003c\"hdel\"\u003e): Promise\u003cvoid\u003e;\n  // hgetall?(context: CacheContext\u003c\"hgetall\"\u003e): Promise\u003cvoid\u003e;\n  // hkeys?(context: CacheContext\u003c\"hkeys\"\u003e): Promise\u003cvoid\u003e;\n}\n\n@Module({\n  imports: [CacheModule.register()],\n  prividers: [\n    /**\n     * Register middleware\n     */\n    TestCacheMiddleware,\n    ExampleService,\n  ],\n})\nexport class AppModule {}\n```\n\n## Using In-memory\n\n@anchan828/nest-cache has been extended to make more Redis commands available. In line with this, the memory store also provides compatibility features. Please use [@anchan828/nest-cache-manager-memory](https://www.npmjs.com/package/@anchan828/nest-cache-manager-memory) instead of the default memory store.\n\n```ts\nimport { memoryStore } from \"@anchan828/nest-cache-manager-memory\";\n\n@Module({\n  imports: [\n    CacheModule.register({\n      store: memoryStore,\n    }),\n  ],\n})\nexport class AppModule {}\n```\n\n## Using Redis\n\nYou can use Redis instead of in-memory cache. Please use [@anchan828/nest-cache-manager-ioredis](https://www.npmjs.com/package/@anchan828/nest-cache-manager-ioredis)\n\n_@anchan828/nest-cache-manager-ioredis_ has the ability to cache Redis results in AsyncLocalStorage. This is useful for elements that need to be accessed frequently.\n\n```ts\nimport { redisStore } from \"@anchan828/nest-cache-manager-ioredis\";\nconst asyncLocalStorage = new AsyncLocalStorage\u003cMap\u003cstring, any\u003e\u003e();\n@Module({\n  imports: [\n    CacheModule.register({\n      store: redisStore,\n      host: \"localhost\",\n      asyncLocalStorage,\n    }),\n  ],\n})\nexport class AppModule {}\n```\n\n## Supported for more Redis commands\n\n- hget\n- hset\n- hdel\n- hgetall\n- hkeys\n\n## License\n\n[MIT](LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanchan828%2Fnest-cache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fanchan828%2Fnest-cache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanchan828%2Fnest-cache/lists"}