xTaskjs 1.0 ya está disponible: cuentas por rol, documentación y una interfaz personalizable.

Paquetes

@xtaskjs/cache

Memory and Redis-backed cache models, method decorators, runtime inspection, and browser cache policies.

npm install @xtaskjs/cache reflect-metadata Ruta del paquete: packages/cache

Resumen

Qué controla este paquete en el runtime

Cache adds application-level data caching and client-facing HTTP cache control to xtaskjs. It registers named cache models, exposes injectable repositories and services, layers method decorators for cache read/write behavior, and can publish browser cache headers plus operational endpoints from the same package.

Qué ofrece

  • configureCache() centralizes default driver, TTL, namespace, Redis connection options, and shared HTTP cache defaults.
  • CacheModel(), Cacheable(), CachePut(), and CacheEvict() cover model registration plus read-through, write-through, and eviction flows.
  • CacheService, CacheRepository, and CacheAdminService are injectable for direct entry access, runtime inspection, and admin operations.
  • BrowserCache(), CacheView(), NoStore(), NoCache(), VaryBy(), and HttpCacheService handle Cache-Control, ETag, Last-Modified, and Vary headers.

Cómo encaja

  • Initialized automatically by @xtaskjs/core when the package is installed and configureCache()/registerCacheModel() have been called.
  • Supports in-memory stores by default and Redis-backed models when the redis client package is available.
  • Demonstrated by the 12-cache_app, 13-cache_redis_app, 14-http_cache_web_app, and 15-fastify_http_cache_web_app samples.

Mapa de uso

Qué peso tiene este paquete dentro del runtime

Arranque

4/5

Inyección de dependencias

4/5

Caching

5/5

Operaciones

5/5

Entrega HTTP

4/5

Flujo del paquete

Cómo atraviesa este paquete las fases del runtime de xtaskjs

Antes del arranque

Configure package defaults, register cache models, and optionally export a cache management controller so storage and HTTP policy behavior are known before requests arrive.

Durante CreateApplication()

The cache lifecycle manager publishes CacheService, CacheAdminService, model repositories, and HttpCacheService into the container while connecting Redis-backed stores when configured.

Durante app.close()

Connected stores and runtime cache state are released automatically so in-memory and Redis-backed caches shut down with the rest of the application.

Superficie API

Exports representativos del paquete original

Configuration and models

  • configureCache
  • registerCacheModel
  • CacheSettings
  • CacheModel

Repository and services

  • CacheRepository
  • CacheService
  • CacheAdminService
  • InjectCacheService
  • InjectCacheRepository

HTTP caching and management

  • HttpCacheService
  • InjectHttpCacheService
  • CacheResponse
  • BrowserCache
  • CacheView
  • NoStore
  • NoCache
  • VaryBy
  • createCacheManagementController
  • InjectCacheAdminService

Superficie de ciclo de vida

  • InjectCacheLifecycleManager
  • initializeCacheIntegration
  • shutdownCacheIntegration
  • resetCacheIntegration

Uso

Flujo típico de adopción

1. Configure models and defaults

Use configureCache() and CacheModel() to define namespace, driver selection, TTL behavior, Redis connectivity, and shared HTTP cache defaults once near app startup.

2. Apply method decorators and injection

Use Cacheable, CachePut, and CacheEvict on DI-managed services, then inject CacheRepository or CacheService when you need direct entry reads, writes, or inspection.

3. Expose client and operator controls

Apply BrowserCache, CacheView, NoStore, NoCache, and VaryBy to routes, and publish createCacheManagementController() when operators need runtime cache inspection endpoints.

Ejemplo

Fragmento de referencia

Model caching plus browser cache control
import { Controller, Get, Param } from "@xtaskjs/common";
import { Service } from "@xtaskjs/core";
import {
  BrowserCache,
  CacheModel,
  CacheRepository,
  Cacheable,
  InjectCacheRepository,
  configureCache,
  createCacheManagementController,
} from "@xtaskjs/cache";

configureCache({
  defaultDriver: "memory",
  defaultTtl: "45s",
  namespace: "catalog",
  httpCacheDefaults: { visibility: "public", maxAge: "2m", etag: true },
});

@CacheModel({ name: "products", ttl: "45s" })
export class ProductCacheModel {}

export const CacheManagementController = createCacheManagementController({
  path: "/ops/cache",
});

@Service()
export class CatalogService {
  constructor(
    @InjectCacheRepository(ProductCacheModel)
    private readonly products: CacheRepository<any>
  ) {}

  @Cacheable({ model: ProductCacheModel, key: (id: string) => id })
  async getProduct(id: string) {
    return { id, generatedAt: new Date().toISOString() };
  }
}

@Controller("/products")
export class CatalogController {
  constructor(private readonly catalog: CatalogService) {}

  @BrowserCache({ staleWhileRevalidate: "30s" })
  @Get(":id")
  show(@Param("id") id: string) {
    return this.catalog.getProduct(id);
  }
}

Ejemplos

Ejemplos oficiales para revisar después

Ejemplos de referencia: 12-cache_app, 13-cache_redis_app, 14-http_cache_web_app, 15-fastify_http_cache_web_app

Relacionados

Paquetes que suelen usarse junto a este