583 測定値
583 測定値

Nest.JS で分散キャッシュをマスターする方法

Kamil Fronczak4m2025/04/03
Read on Terminal Reader

長すぎる; 読むには

@nestixis/cache-manager は、NestJS 用の軽量で Redis 対応のライブラリで、Redis キャッシュを管理するためのシンプルな API を提供し、構成可能な TTL と高度なキャッシュ 戦略をサポートしています。
featured image - Nest.JS で分散キャッシュをマスターする方法
Kamil Fronczak HackerNoon profile picture
0-item

キャッシングは開発者側のトーンになる可能性があります. I have spent too many hours wrestling with slow APIs and overloaded databases, looking for a solution that is both effective and easy to implement.


そこで私は、私たちのオープンソース組織の有能なメンバーであるKarol (karol71927) が、 @nestixis/cache-manager を作成したときに感動しました。karol71927Nestixis


この軽量でRedis駆動型のライブラリは、私のNestJSプロジェクトのキャッシュを簡素化し、それがどのように変化したかを共有することを楽しみにしています。


The Challenge: Caching Complexity in NestJS

このシナリオはあまりにもよく知られています: あなたのアプリケーションはトラフィックが上昇するまでスムーズに動作し、突然データベースが負荷の下でバックアップします。 キャッシュは明らかな解決策です - データを一度保存し、迅速にサービスする - ですが、NestJSに統合することはしばしば困難です。 Redisは強力な機能を提供しますが、設定には通常、構成の争い、期限切れポリシーの管理、カスタムキャッシュキーの定義が含まれます。

I needed a tool that simplified the process while allowing precise control over what gets cached, such as request parameters or queries.

リクエストパラメータやクエリなどのキャッシュされたものに対する正確な制御を可能にするツールが必要でした。


So, Karol designed @nestixis/cache-manager to address these pain points with a clean, efficient approach. This package provides a straightforward API for managing Redis caching, complete with configurable TTLs and support for advanced caching strategies. It is available at its GitHub repo, and its design reflects our team's commitment to practical, reusable tools.

そのGitHubのレポ

Getting Started: Seamless Setup

Installation is as simple as it gets:


npm i @nestixis/cache-manager 
npm i @nestixis/cache-manager


NestJSアプリに統合するには、モジュールに登録してください:


<前>Import { Module } from '@nestjs/common'; import { CacheModule } from '@nestixis/cache-manager'; import { ConfigModule, ConfigService } from '@nestjs/config'; @Module({ imports: [ CacheModule.registerAsync:{ isGlobal: true, imports: [ConfigModule], useFactory: (configService: ConfigService) => ({ redis: { host: configService.get('REDIS_HOST')', t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t平成30年 平成30年 平成30年 平成30年 平成30年 平成30年 平成30年 平成30年 平成30年 平成30年 平成30年 平成30年 平成30年 平成30年 平成30年 平成30年 平成30年 平成30年 平成30年 平成30年 平成30年 平成30年 平成30年 平成30年 平成30年 平成30年 平成30年 平成30年 平成30年 平成30年 平成30年 平成30年 平成30年 平成30年 平成30年 平成30年 平成30年 平成30年 平成30年 平成30年 平成30年 平成30年 平成30年 平成30年


This set up Redis caching across your app with minimal effort. From there, you can interact with it manually in a service:


<前>import { Injectable } from '@nestjs/common'; import { CacheManager } from '@nestixis/cache-manager'; @Injectable() export class MyService { constructor(private readonly cacheManager: CacheManager) {} async getData(key: string) { const cached = wait this.cacheManager.get(key); if (cached) return cached; const = data await this.fetchData(); wait this.cacheMancheager.add(key, data, 1000); // Cache for 1 second return data; } async clearData(key: string) { await this.MancheManager.remove(key); } private fetchData() { new Promiseトップ > タグ > タグ > タグ > タグ > タグ > タグ > タグ > タグ > タグ > タグ > タグ > タグ > タグ > タグ > タグ > タグ > タグ > タグ > タグ > タグ > タグ > タグ > タグ > タグ > タグ > タグ > タグ > タグ > タグ > タグ > タグ > タグ > タグ > タグ > タグ > タグ > タグ > タグ > タグ > タグ >


このパッケージを区別するものは、特定のリクエストの詳細に基づいてキャッシュする能力である - Karol が慎重に含めた機能です。

コントローラー:


import { Controller, Get, Post, Delete, Param, UseInterceptors } from '@nestjs/common'; import { CacheInterceptor, CacheRemoveInterceptor, CacheTrackBy } from '@nestixis/cache-manager'; import { MyService } from './my.service'; @Controller('site/:token) @CacheTrackBy({プレフィックス:'site', ttl: 10000, // 10 秒で: { by: 'param', 名前: 'token', }, }, }) export class SiteController {constructor(private readonly service: MyService) } from './my.service'; @Controller('site/:token)<コード class="language-typescript data">import { Controller, Get, Post, Delete, Param, UseInterceptors } from '@nestjs/common'; import { CacheInterceptor, CacheRemoveInterceptor, CacheTrackBy } from '@nestixis/cache-manager'; import { MyService } from './my.service'; @Controller('site/:token') @CacheTrackBy({前置詞:'site', ttl: 10000, // 10 秒で: { by: 'param', 名前: 'token', }, }, }) export class SiteController {constructor(private readonly service: MyService) } from './my.service'; @CacheInterceptors(CacheInterceptor


The @CacheTrackBy decorator is the key here. It ensures caching is bound to the :token parameter, so /site/abc and /site/xyz each get their own cache entry.

@CacheTrackBy


CacheInterceptor は GET リクエストを処理し、CacheRemoveInterceptor は更新時にキャッシュをクリアします。

Trending Topics

blockchaincryptocurrencyhackernoon-top-storyprogrammingsoftware-developmenttechnologystartuphackernoon-booksBitcoinbooks