remove food cache
This commit is contained in:
@@ -22,7 +22,7 @@ export class FoodService {
|
||||
private readonly restRepository: RestRepository,
|
||||
private readonly em: EntityManager,
|
||||
private readonly cacheService: CacheService,
|
||||
) { }
|
||||
) {}
|
||||
|
||||
async create(restId: string, createFoodDto: CreateFoodDto): Promise<Food> {
|
||||
const { categoryId, ...rest } = createFoodDto;
|
||||
@@ -65,7 +65,7 @@ export class FoodService {
|
||||
await this.em.persistAndFlush(food);
|
||||
|
||||
// Invalidate cache for the restaurant
|
||||
await this.invalidateRestaurantFoodsCache(restaurant.slug);
|
||||
// await this.invalidateRestaurantFoodsCache(restaurant.slug);
|
||||
|
||||
return food;
|
||||
}
|
||||
@@ -77,17 +77,17 @@ export class FoodService {
|
||||
/**
|
||||
* Public food detail (only active foods are visible).
|
||||
*/
|
||||
async findPublicById(foodId: string, userId?: string):Promise<any> {
|
||||
async findPublicById(foodId: string, userId?: string): Promise<any> {
|
||||
const food = await this.foodRepository.findOne({ id: foodId, isActive: true }, { populate: ['category'] });
|
||||
if (!food) throw new NotFoundException(FoodMessage.NOT_FOUND);
|
||||
let isFavorite = false
|
||||
let isFavorite = false;
|
||||
if (userId) {
|
||||
isFavorite = await this.em.count(Favorite, { user: { id: userId }, food: { id: foodId } }) > 0
|
||||
isFavorite = (await this.em.count(Favorite, { user: { id: userId }, food: { id: foodId } })) > 0;
|
||||
}
|
||||
return {
|
||||
...food,
|
||||
isFavorite,
|
||||
};;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -101,20 +101,20 @@ export class FoodService {
|
||||
}
|
||||
|
||||
async findAllByRestaurant(slug: string): Promise<Food[]> {
|
||||
const cacheKey = `${this.FOODS_BY_RESTAURANT_CACHE_KEY_PREFIX}${slug}`;
|
||||
// const cacheKey = `${this.FOODS_BY_RESTAURANT_CACHE_KEY_PREFIX}${slug}`;
|
||||
|
||||
// Try to get from cache first
|
||||
const cachedFoods = await this.cacheService.get<string>(cacheKey);
|
||||
if (cachedFoods) {
|
||||
try {
|
||||
const parsed: unknown = JSON.parse(cachedFoods);
|
||||
if (Array.isArray(parsed)) {
|
||||
return parsed as Food[];
|
||||
}
|
||||
} catch {
|
||||
// If parsing fails, continue to fetch from DB
|
||||
}
|
||||
}
|
||||
// const cachedFoods = await this.cacheService.get<string>(cacheKey);
|
||||
// if (cachedFoods) {
|
||||
// try {
|
||||
// const parsed: unknown = JSON.parse(cachedFoods);
|
||||
// if (Array.isArray(parsed)) {
|
||||
// return parsed as Food[];
|
||||
// }
|
||||
// } catch {
|
||||
// // If parsing fails, continue to fetch from DB
|
||||
// }
|
||||
// }
|
||||
|
||||
// If not in cache, fetch from database
|
||||
const restaurant = await this.restRepository.findOne({ slug });
|
||||
@@ -125,7 +125,7 @@ export class FoodService {
|
||||
const foods = await this.foodRepository.find({ restaurant: { slug }, isActive: true }, { populate: ['category'] });
|
||||
|
||||
// Cache the result
|
||||
await this.cacheService.set(cacheKey, JSON.stringify(foods), this.FOODS_CACHE_TTL);
|
||||
// await this.cacheService.set(cacheKey, JSON.stringify(foods), this.FOODS_CACHE_TTL);
|
||||
|
||||
return foods;
|
||||
}
|
||||
@@ -154,7 +154,7 @@ export class FoodService {
|
||||
await this.em.persistAndFlush(food);
|
||||
|
||||
// Invalidate cache for the restaurant
|
||||
await this.invalidateRestaurantFoodsCache(food.restaurant.slug);
|
||||
// await this.invalidateRestaurantFoodsCache(food.restaurant.slug);
|
||||
|
||||
return food;
|
||||
}
|
||||
@@ -165,12 +165,12 @@ export class FoodService {
|
||||
throw new NotFoundException(FoodMessage.NOT_FOUND);
|
||||
}
|
||||
|
||||
const restaurantSlug = food.restaurant.slug;
|
||||
// const restaurantSlug = food.restaurant.slug;
|
||||
food.deletedAt = new Date();
|
||||
await this.em.persistAndFlush(food);
|
||||
|
||||
// Invalidate cache for the restaurant
|
||||
await this.invalidateRestaurantFoodsCache(restaurantSlug);
|
||||
// await this.invalidateRestaurantFoodsCache(restaurantSlug);
|
||||
}
|
||||
|
||||
async removeFavorite(userId: string, foodId: string) {
|
||||
@@ -189,12 +189,11 @@ export class FoodService {
|
||||
await this.em.persistAndFlush(favorite);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Invalidate cache for restaurant foods
|
||||
*/
|
||||
private async invalidateRestaurantFoodsCache(slug: string): Promise<void> {
|
||||
const cacheKey = `${this.FOODS_BY_RESTAURANT_CACHE_KEY_PREFIX}${slug}`;
|
||||
await this.cacheService.del(cacheKey);
|
||||
}
|
||||
// private async invalidateRestaurantFoodsCache(slug: string): Promise<void> {
|
||||
// const cacheKey = `${this.FOODS_BY_RESTAURANT_CACHE_KEY_PREFIX}${slug}`;
|
||||
// await this.cacheService.del(cacheKey);
|
||||
// }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user