Tingkatkan Prestasi Nest.js Anda dengan Penghias `@Cacheable` Tersuai

Susan Sarandon
Lepaskan: 2024-09-28 06:21:02
asal
164 orang telah melayarinya

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;
  };
}
Salin selepas log masuk

? 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()
}
Salin selepas log masuk

? 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 {}
Salin selepas log masuk

? 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
}
Salin selepas log masuk

? 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! ?

Atas ialah kandungan terperinci Tingkatkan Prestasi Nest.js Anda dengan Penghias `@Cacheable` Tersuai. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

sumber:dev.to
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel terbaru oleh pengarang
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!