cache
This commit is contained in:
@@ -5,39 +5,25 @@ import { ConfigService } from '@nestjs/config';
|
|||||||
import { Logger } from '@nestjs/common'; // 1. Import Logger
|
import { Logger } from '@nestjs/common'; // 1. Import Logger
|
||||||
|
|
||||||
export function cacheConfig(): CacheModuleAsyncOptions {
|
export function cacheConfig(): CacheModuleAsyncOptions {
|
||||||
// 2. Create a logger instance
|
|
||||||
const logger = new Logger('CacheConfig');
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
isGlobal: true,
|
isGlobal: true,
|
||||||
inject: [ConfigService],
|
inject: [ConfigService],
|
||||||
useFactory: (configService: 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 namespace = configService.get<string>('CACHE_NAMESPACE', 'app-cache');
|
||||||
|
|
||||||
const redisUri = configService.getOrThrow<string>('REDIS_URI');
|
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({
|
const store = new Keyv({
|
||||||
store: keyvRedisStore, // Use the instance
|
store: new KeyvRedis(redisUri),
|
||||||
namespace,
|
namespace,
|
||||||
ttl: ttl * 1000,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
store,
|
store,
|
||||||
ttl,
|
ttl, // cache-manager handles TTL
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -22,8 +22,8 @@ export class BulkReserveFoodDto {
|
|||||||
description: 'Array of food reservations to create',
|
description: 'Array of food reservations to create',
|
||||||
type: [BulkReserveFoodItemDto],
|
type: [BulkReserveFoodItemDto],
|
||||||
example: [
|
example: [
|
||||||
{ foodId: 'food-123', quantity: 5, expiresAt: '2024-12-31T23:59:59Z' },
|
{ foodId: 'food-123', quantity: 5 },
|
||||||
{ foodId: 'food-789', quantity: 3, expiresAt: '2024-12-31T23:59:59Z' },
|
{ foodId: 'food-789', quantity: 3 },
|
||||||
],
|
],
|
||||||
})
|
})
|
||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
|
|||||||
@@ -1,4 +0,0 @@
|
|||||||
import { PartialType } from '@nestjs/swagger';
|
|
||||||
import { CreateInventoryDto } from './create-inventory.dto';
|
|
||||||
|
|
||||||
export class UpdateInventoryDto extends PartialType(CreateInventoryDto) {}
|
|
||||||
@@ -143,7 +143,7 @@ export class InventoryService {
|
|||||||
const foodIds = [...new Set(items.map(item => item.foodId))];
|
const foodIds = [...new Set(items.map(item => item.foodId))];
|
||||||
|
|
||||||
// Load all foods in one query
|
// 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
|
// Verify all foods exist and belong to the restaurant
|
||||||
const foodMap = new Map<string, Food>();
|
const foodMap = new Map<string, Food>();
|
||||||
@@ -160,7 +160,7 @@ export class InventoryService {
|
|||||||
// Load all existing inventories in one query
|
// Load all existing inventories in one query
|
||||||
const existingInventories = await em.find(Inventory, {
|
const existingInventories = await em.find(Inventory, {
|
||||||
food: { id: { $in: foodIds } },
|
food: { id: { $in: foodIds } },
|
||||||
});
|
},{ lockMode: LockMode.PESSIMISTIC_WRITE });
|
||||||
|
|
||||||
const inventoryMap = new Map<string, Inventory>();
|
const inventoryMap = new Map<string, Inventory>();
|
||||||
for (const inventory of existingInventories) {
|
for (const inventory of existingInventories) {
|
||||||
|
|||||||
Reference in New Issue
Block a user