Skip to content
Download

Adaptador de cache (Redis)

O CacheAdapter é uma pequena abstração sobre o cliente Redis nativo do Bun, focada em:

  • set (permanente ou com TTL)
  • get (tipado, sempre retorna um objeto)
  • exists
  • remove

Importação

ts
import { CacheAdapter } from "@grupodiariodaregiao/bunstone";

Configuração

Por padrão, ele usa o cliente global redis do Bun (lê REDIS_URL / VALKEY_URL).

ts
const cache = new CacheAdapter();

Ou passe uma URL de conexão personalizada:

ts
const cache = new CacheAdapter({ url: "redis://localhost:6379" });

Set

ts
await cache.set("user:1", { id: 1, name: "Alice" }, { permanent: true });
await cache.set("session:123", { userId: 1 }, { ttlSeconds: 60 * 60 }); // 1 hora

Get (tipado)

get<T>() faz o parse de JSON automaticamente e sempre retorna um objeto (se a chave não existir, retorna {}).

ts
type UserCache = { id: number; name: string };
const user = await cache.get<UserCache>("user:1");

Exists / Remove

ts
const exists = await cache.exists("user:1");
await cache.remove("user:1");

Exemplo prático

Veja como usar o Cache Adapter em um controller:

ts
import {
  Module,
  Controller,
  Post,
  Get,
  AppStartup,
  CacheAdapter,
  FormData,
} from "../../index";

@Controller("cache")
class CacheController {
  constructor(private readonly cache: CacheAdapter) {}

  @Get(":key")
  async getCache(key: string) {
    const value = await this.cache.get(key);
    return { key, value };
  }

  @Post(":key")
  async setCache(key: string, @Body() body: any) {
    await this.cache.set(key, body, { ttlSeconds: 60 });
    return { success: true };
  }
}

@Controller("upload")
class UploadController {
  @Post()
  async uploadFile(@FormData() formData: any) {
    // Access form fields and files
    const { fields, files } = formData;
    return {
      receivedFields: Object.keys(fields),
      receivedFiles: Object.keys(files),
    };
  }
}

@Module({
  controllers: [CacheController, UploadController],
  providers: [CacheAdapter],
})
class AppModule {}

const app = await AppStartup.create(AppModule);
console.log("Adapters example configured.");

Veja no GitHub

Distribuído sob a Licença MIT.