首頁 > web前端 > js教程 > 主體

使用自訂'@Cacheable”裝飾器增強 Nest.js 效能

Susan Sarandon
發布: 2024-09-28 06:21:02
原創
301 人瀏覽過

Enhance Your Nest.js Performance with a Custom `@Cacheable` Decorator

Caching is a fundamental technique to improve the performance and scalability of your applications. In Nest.js, caching can be seamlessly integrated using the built-in cache manager. In this article, we'll explore how to create a custom @Cacheable decorator to simplify caching in your Nest.js services or controllers.

? Why Use a Custom @Cacheable Decorator?

While Nest.js provides powerful caching mechanisms out of the box, applying caching logic directly within your methods can clutter your code. A custom decorator abstracts this logic, making your code cleaner and more maintainable.

? Creating the @Cacheable Decorator

Let's start by creating the @Cacheable decorator that we'll use to cache the results of our methods.

import { Cache } from 'cache-manager';

export function Cacheable(cacheKey: string) {
  return function (
    target: any,
    propertyName: string,
    descriptor: PropertyDescriptor,
  ) {
    const originalMethod = descriptor.value;

    descriptor.value = async function (...args: any[]) {
      const cache: Cache = this.cacheManager;

      if (!cache) {
        throw new Error(
          'Cannot use Cacheable() decorator without injecting the cache manager.',
        );
      }

      // Try to get cached data
      try {
        const cachedResult = await cache.get(cacheKey);
        if (cachedResult) {
          return cachedResult;
        }
      } catch (error) {
        console.error(`Cache get error for key: ${cacheKey}:`, error);
      }

      // Call the original method if cache miss
      const result = await originalMethod.apply(this, args);

      // Set the new result in cache
      try {
        await cache.set(cacheKey, result);
      } catch (error) {
        console.error(`Cache set error for key: ${cacheKey}:`, error);
      }

      return result;
    };

    return descriptor;
  };
}
登入後複製

? Explanation

  • Cache Retrieval: Before executing the original method, the decorator checks if the result is already cached.
  • Cache Miss Handling: If the result is not in the cache, it executes the original method and then caches the result.
  • Error Handling: Catches and logs any errors during cache retrieval or setting, ensuring your application doesn't crash due to caching issues.

? Using the @Cacheable Decorator

Here's how you can apply the @Cacheable decorator to a method in your service:

import { Injectable } from '@nestjs/common';
import { Cacheable } from './cacheable.decorator';

const SETTING_CACHE_KEY = 'settings';

@Injectable()
export class SettingsService {
  // Inject the cache manager
  constructor(private readonly cacheManager: Cache) {}

  /**
   * Retrieves settings from the cache if available, or loads them from the
   * repository and caches the result.
   *
   * @returns A promise that resolves to a `Settings` object.
   */
  @Cacheable(SETTING_CACHE_KEY)
  async getSettings(): Promise<Settings> {
    return await this.findAll();
  }

  // ... other methods like findAll() and buildTree()
}
登入後複製

? Explanation

  • Decorator Application: The @Cacheable decorator is applied to the getSettings() method with a specific cache key.
  • Dependency Injection: The cache manager is injected into the service to be used by the decorator.

? Integrating the Cache Manager in Nest.js

To use the cache manager in your application, you need to register it in your module:

import { Module } from '@nestjs/common';
import { CacheModule } from '@nestjs/cache-manager';
import { SettingsService } from './settings.service';

@Module({
  imports: [
    CacheModule.register({
      isGlobal: true,
      ttl: 300, // Time to live in seconds
      max: 100, // Maximum number of items in cache
    }),
  ],
  providers: [SettingsService],
})
export class AppModule {}
登入後複製

? Explanation

  • Global Cache: Setting isGlobal: true makes the cache manager available throughout your application.
  • TTL and Max Items: Configure the time-to-live (ttl) and maximum number of items (max) in the cache.

? Injecting the Cache Manager

Ensure that you inject the cache manager into any service or controller that uses the @Cacheable decorator:

import { Injectable } from '@nestjs/common';
import { Cache } from 'cache-manager';

@Injectable()
export class SettingsService {
  constructor(private readonly cacheManager: Cache) {}

  // ... your methods
}
登入後複製

? Conclusion

By creating a custom @Cacheable decorator, you can keep your methods clean and focus on the core logic, leaving caching concerns to the decorator. This approach enhances code readability and maintainability, making your Nest.js application more efficient and scalable.

Feel free to leave comments or questions below. Happy coding! ?

以上是使用自訂'@Cacheable”裝飾器增強 Nest.js 效能的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:dev.to
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板