import { DatabaseContext, CacheOptions } from '../src/data/repository-data-contexts'; import { ReadRepository } from '../src/data/read-repository'; import { MemoryCacheManager } from '../src/data/cache-manager'; // Przykład użycia cache w repozytorium export function createSimpleCachedRepository() { // 1. Skonfiguruj opcje cache const cacheOptions: CacheOptions = { ttl: 300, // 5 minut store: 'memory', prefix: 'users', enabled: true }; // 2. Stwórz cache manager z opcjami const cacheManager = new MemoryCacheManager(cacheOptions); // 3. Stwórz context z cache (przykład - potrzebujesz prawdziwego source i mapper) const userContext = new DatabaseContext( userSource, // Twój source (np. MongoSource) userMapper, // Twój mapper sessionRegistry, // Twój session registry cacheManager // Cache manager z wbudowanymi opcjami ); // 4. Stwórz repository const userRepository = new ReadRepository(userContext); return userRepository; } // Przykład użycia w Use Case export class GetUsersUseCase { constructor(private userRepository: ReadRepository) {} async execute(): Promise> { // Repository automatycznie sprawdzi cache // Jeśli dane są w cache - zwróci je // Jeśli nie - pobierze z bazy i zapisze do cache return await this.userRepository.find({ where: { status: 'active' } }); } async getUsersCount(): Promise> { // Count również będzie cachowane return await this.userRepository.count({ where: { status: 'active' } }); } } // Przykład bez cache export function createNonCachedRepository() { const userContext = new DatabaseContext( userSource, userMapper, sessionRegistry // Bez cache managera - cache będzie wyłączone ); return new ReadRepository(userContext); } // Przykład z Redis cache (implementacja w soap-node-redis) // export function createRedisCachedRepository(redisClient: any) { // const cacheOptions: CacheOptions = { // ttl: 600, // 10 minut // store: 'redis', // prefix: 'users', // enabled: true // }; // // const cacheManager = new RedisCacheManager(redisClient, cacheOptions); // // const userContext = new DatabaseContext( // userSource, // userMapper, // sessionRegistry, // cacheManager // ); // // return new ReadRepository(userContext); // }