I'm currently working this website. The problem is that the pages takes 3-4 seconds to get the first bye, and I think it's because query to load data in the pages is very slow.
In the store page, it basically uses an object of a store to show the store's basic information and uses ForeignKey relation to access the store's images. In addition, it uses ManyToManyField to access the store's similar stores which are a store object as well. For this solved, I used prefetch_related and select_related so minimized many of duplicated queries. But still it shows a low performance.
After that I thought, I could improve it with caching, so I did the below. To be honest, what I expected with caching was like super fast loading as it is supposed to store all the processed queries and just show the data from the requested cache. But, to be honest, it still shows 3-4 seconds loading performance. Am I using the cache in a wrong way?
settings.py
...
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
'LOCATION': 'django_cache',
}
}
...
views.py
class StoreDetailView(View):
def get(self, request, *args, **kwargs):
store_domainKey = self.kwargs['store_domainKey']
cache_store = 'store-{0}'.format(store_domainKey)
store = cache.get(cache_store, None)
if not store:
try:
q = (
Store.objects
.select_related('price_range')
.prefetch_related(
'top_keywords', 'sub_keywords', 'ship_to', 'likes',
'question_set', 'question_set__answer_set',
'similar_stores', 'sponsored_stores', 'image_set'
)
.get(domainKey=store_domainKey)
)
except Store.DoesNotExist:
raise Http404
cache.set(cache_store, q)
store = cache.get(cache_store)
cache_similar_stores = 'similar-stores-{0}'.format(store_domainKey)
similar_stores = cache.get(cache_similar_stores, None)
if not similar_stores:
sponsored_stores_ids = sponsored_stores.values_list('sponsor_receiver__id', flat=True)
q = (
Similarity.objects
.select_related('similar_store', 'similar_store__price_range')
.prefetch_related('similar_store__image_set')
.filter(similar_target=store)
.exclude(Q(similar_store__id__in=sponsored_stores_ids) | Q(similar_store=store))
)
cache.set(cache_similar_stores, q)
similar_stores = cache.get(cache_similar_stores)
page = request.GET.get('page', 1)
paginator = Paginator(similar_stores, 5)
try:
similar_stores = paginator.page(page)
except PageNotAnInteger:
similar_stores = paginator.page(1)
except EmptyPage:
similar_stores = paginator.page(paginator.num_pages)
context = {
'store': store,
'sponsored_stores': sponsored_stores,
'similar_stores': similar_stores,
'top_3_similar_store_string': top_3_similar_store_string,
}
return render(request, template, context)