Mapa de uso
Qué peso tiene este paquete dentro del runtime
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
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