This commit is contained in:
2025-12-17 22:17:06 +03:30
parent b663bfa592
commit 4319be7a04
4 changed files with 9 additions and 27 deletions
+5 -19
View File
@@ -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<number>('CACHE_TTL', 120);
const ttl = configService.get<number>('CACHE_TTL', 3600);
const namespace = configService.get<string>('CACHE_NAMESPACE', 'app-cache');
const redisUri = configService.getOrThrow<string>('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
};
},
};
}
@@ -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()
@@ -1,4 +0,0 @@
import { PartialType } from '@nestjs/swagger';
import { CreateInventoryDto } from './create-inventory.dto';
export class UpdateInventoryDto extends PartialType(CreateInventoryDto) {}
+2 -2
View File
@@ -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<string, Food>();
@@ -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<string, Inventory>();
for (const inventory of existingInventories) {