From 4319be7a04ef5aa0a124a9191ab5110d72819f26 Mon Sep 17 00:00:00 2001 From: morteza-mortezai Date: Wed, 17 Dec 2025 22:17:06 +0330 Subject: [PATCH] cache --- src/config/cache.config.ts | 24 ++++--------------- .../inventory/dto/bulk-reserve-food.dto.ts | 4 ++-- .../inventory/dto/update-inventory.dto.ts | 4 ---- src/modules/inventory/inventory.service.ts | 4 ++-- 4 files changed, 9 insertions(+), 27 deletions(-) delete mode 100644 src/modules/inventory/dto/update-inventory.dto.ts diff --git a/src/config/cache.config.ts b/src/config/cache.config.ts index 388a3f3..cae208f 100755 --- a/src/config/cache.config.ts +++ b/src/config/cache.config.ts @@ -5,39 +5,25 @@ import { ConfigService } from '@nestjs/config'; import { Logger } from '@nestjs/common'; // 1. Import Logger export function cacheConfig(): CacheModuleAsyncOptions { - // 2. Create a logger instance - const logger = new Logger('CacheConfig'); - return { isGlobal: true, inject: [ConfigService], useFactory: (configService: ConfigService) => { - const ttl = configService.get('CACHE_TTL', 120); + const ttl = configService.get('CACHE_TTL', 3600); const namespace = configService.get('CACHE_NAMESPACE', 'app-cache'); + const redisUri = configService.getOrThrow('REDIS_URI'); - // 3. Create the KeyvRedis store first - const keyvRedisStore = new KeyvRedis(redisUri); - - // 4. Attach event listeners - keyvRedisStore.client.on('connect', () => { - logger.log('✅ Redis connected successfully.'); - }); - keyvRedisStore.client.on('error', error => { - logger.error('❌ Redis connection error:', error); - }); - - // 5. Pass the store to Keyv const store = new Keyv({ - store: keyvRedisStore, // Use the instance + store: new KeyvRedis(redisUri), namespace, - ttl: ttl * 1000, }); return { store, - ttl, + ttl, // cache-manager handles TTL }; }, }; } + diff --git a/src/modules/inventory/dto/bulk-reserve-food.dto.ts b/src/modules/inventory/dto/bulk-reserve-food.dto.ts index a67965f..5af153b 100644 --- a/src/modules/inventory/dto/bulk-reserve-food.dto.ts +++ b/src/modules/inventory/dto/bulk-reserve-food.dto.ts @@ -22,8 +22,8 @@ export class BulkReserveFoodDto { description: 'Array of food reservations to create', type: [BulkReserveFoodItemDto], example: [ - { foodId: 'food-123', quantity: 5, expiresAt: '2024-12-31T23:59:59Z' }, - { foodId: 'food-789', quantity: 3, expiresAt: '2024-12-31T23:59:59Z' }, + { foodId: 'food-123', quantity: 5 }, + { foodId: 'food-789', quantity: 3 }, ], }) @IsNotEmpty() diff --git a/src/modules/inventory/dto/update-inventory.dto.ts b/src/modules/inventory/dto/update-inventory.dto.ts deleted file mode 100644 index 8b78399..0000000 --- a/src/modules/inventory/dto/update-inventory.dto.ts +++ /dev/null @@ -1,4 +0,0 @@ -import { PartialType } from '@nestjs/swagger'; -import { CreateInventoryDto } from './create-inventory.dto'; - -export class UpdateInventoryDto extends PartialType(CreateInventoryDto) {} diff --git a/src/modules/inventory/inventory.service.ts b/src/modules/inventory/inventory.service.ts index 812af3b..1cdb866 100644 --- a/src/modules/inventory/inventory.service.ts +++ b/src/modules/inventory/inventory.service.ts @@ -143,7 +143,7 @@ export class InventoryService { const foodIds = [...new Set(items.map(item => item.foodId))]; // Load all foods in one query - const foods = await em.find(Food, { id: { $in: foodIds } }, { lockMode: LockMode.PESSIMISTIC_WRITE }); + const foods = await em.find(Food, { id: { $in: foodIds } }); // Verify all foods exist and belong to the restaurant const foodMap = new Map(); @@ -160,7 +160,7 @@ export class InventoryService { // Load all existing inventories in one query const existingInventories = await em.find(Inventory, { food: { id: { $in: foodIds } }, - }); + },{ lockMode: LockMode.PESSIMISTIC_WRITE }); const inventoryMap = new Map(); for (const inventory of existingInventories) {