optimze home page and product page
This commit is contained in:
@@ -92,24 +92,41 @@ class AllCategoriesV2(APIView):
|
||||
},
|
||||
)
|
||||
def get(self, request):
|
||||
from django.core.cache import cache
|
||||
|
||||
# Check cache first
|
||||
cache_key = 'all_categories_v2'
|
||||
cached_data = cache.get(cache_key)
|
||||
if cached_data:
|
||||
return Response(cached_data, status=status.HTTP_200_OK)
|
||||
|
||||
# Optimize query with prefetch_related to avoid N+1 queries
|
||||
unit_categories = UnitCategoryModel.objects.prefetch_related(
|
||||
Prefetch(
|
||||
'maincategorys',
|
||||
queryset=MainCategoryModel.objects.prefetch_related(
|
||||
queryset=MainCategoryModel.objects.only(
|
||||
'id', 'name', 'slug', 'icon', 'meta_title', 'meta_description', 'image', 'video'
|
||||
).prefetch_related(
|
||||
Prefetch(
|
||||
'subcategorys',
|
||||
queryset=SubCategoryModel.objects.annotate(
|
||||
queryset=SubCategoryModel.objects.only(
|
||||
'id', 'name', 'slug', 'icon', 'meta_title', 'meta_description', 'image', 'parent_id'
|
||||
).annotate(
|
||||
product_count=Count('products')
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
).all()
|
||||
).only('id', 'name', 'slug', 'icon', 'meta_title', 'meta_description', 'image').all()
|
||||
|
||||
categories_ser = self.serializer_class(
|
||||
instance=unit_categories, many=True, context={'request': request})
|
||||
return Response(categories_ser.data, status=status.HTTP_200_OK)
|
||||
|
||||
response_data = categories_ser.data
|
||||
# Cache for 10 minutes
|
||||
cache.set(cache_key, response_data, 60 * 10)
|
||||
|
||||
return Response(response_data, status=status.HTTP_200_OK)
|
||||
|
||||
|
||||
class ProductView(APIView):
|
||||
|
||||
Reference in New Issue
Block a user