add cache interceptor for hot endpoints
deploy to danak / build_and_deploy (push) Has been cancelled

This commit is contained in:
2026-06-04 17:04:28 +03:30
parent 0d619f313d
commit 7995ae2948
16 changed files with 165 additions and 19 deletions
@@ -19,6 +19,8 @@ import { API_HEADER_SLUG } from 'src/common/constants';
import { SuperAdminAuthGuard } from 'src/modules/auth/guards/superAdminAuth.guard';
import { Permissions } from 'src/common/decorators/permissions.decorator';
import { Permission } from 'src/common/enums/permission.enum';
import { CacheResponse } from 'src/common/decorators/cache-response.decorator';
import { CacheKeyPrefixes } from 'src/common/constants/cache-keys.constant';
@ApiTags('restaurants')
@Controller()
@@ -26,6 +28,7 @@ export class RestaurantsController {
constructor(private readonly restaurantsService: RestaurantsService) { }
@Get('public/restaurants/:slug')
@CacheResponse({ keyPrefix: CacheKeyPrefixes.RESTAURANT_SPEC, params: ['slug'] })
@ApiOperation({ summary: 'Get restaurant specification by slug' })
@ApiHeader(API_HEADER_SLUG)
@ApiParam({ name: 'slug', required: true, description: 'Restaurant slug' })
@@ -27,6 +27,8 @@ import { Schedule } from '../entities/schedule.entity';
import { WalletTransaction } from '../../users/entities/wallet-transaction.entity';
import { SmsLog } from '../../notifications/entities/smsLogs.entity';
import { Notification } from '../../notifications/entities/notification.entity';
import { CacheService } from 'src/modules/utils/cache.service';
import { CacheKeys } from 'src/common/constants/cache-keys.constant';
@Injectable()
@@ -34,6 +36,7 @@ export class RestaurantsService {
constructor(
private readonly em: EntityManager,
private readonly restRepository: RestRepository,
private readonly cacheService: CacheService,
) { }
async setupRestuarant(dto: CreateRestaurantDto): Promise<Restaurant> {
@@ -184,13 +187,28 @@ export class RestaurantsService {
}
const previousSlug = restaurant.slug;
this.restRepository.assign(restaurant, dto);
await this.em.persistAndFlush(restaurant);
await this.invalidateRestaurantCache(previousSlug);
if (restaurant.slug !== previousSlug) {
await this.invalidateRestaurantCache(restaurant.slug);
}
return restaurant;
}
private async invalidateRestaurantCache(slug?: string): Promise<void> {
if (!slug) return;
await Promise.all([
this.cacheService.del(CacheKeys.restaurantSpec(slug)),
this.cacheService.del(CacheKeys.foodsByRestaurant(slug)),
this.cacheService.del(CacheKeys.categoriesByRestaurant(slug)),
]);
}
async remove(id: string) {
// Soft delete the restaurant by setting isActive to false
const restaurant = await this.restRepository.findOne({ id });
@@ -11,11 +11,12 @@ import { ScheduleController } from './controllers/schedule.controller';
import { RestaurantCrone } from './crone/restaurant.crone';
import { JwtModule } from '@nestjs/jwt';
import { AuthModule } from '../auth/auth.module';
import { UtilsModule } from '../utils/utils.module';
@Module({
controllers: [RestaurantsController, ScheduleController],
providers: [RestaurantsService, RestRepository, ScheduleRepository, ScheduleService, RestaurantCrone],
imports: [MikroOrmModule.forFeature([Restaurant, Schedule]), JwtModule, forwardRef(() => AuthModule)],
imports: [MikroOrmModule.forFeature([Restaurant, Schedule]), JwtModule, forwardRef(() => AuthModule), UtilsModule],
exports: [RestRepository, ScheduleRepository, ScheduleService, RestaurantsService],
})
export class RestaurantsModule { }