Merge pull request #1306 from TandoorRecipes/performance_refactor
facets cache-only on initial load
This commit is contained in:
commit
506d7a8bb2
@ -6,6 +6,7 @@ from django.core.cache import caches
|
|||||||
from django.db.models import Avg, Case, Count, Func, Max, OuterRef, Q, Subquery, Value, When
|
from django.db.models import Avg, Case, Count, Func, Max, OuterRef, Q, Subquery, Value, When
|
||||||
from django.db.models.functions import Coalesce, Substr
|
from django.db.models.functions import Coalesce, Substr
|
||||||
from django.utils import timezone, translation
|
from django.utils import timezone, translation
|
||||||
|
from django.utils.translation import gettext as _
|
||||||
|
|
||||||
from cookbook.filters import RecipeFilter
|
from cookbook.filters import RecipeFilter
|
||||||
from cookbook.helper.HelperFunctions import Round, str2bool
|
from cookbook.helper.HelperFunctions import Round, str2bool
|
||||||
@ -259,9 +260,16 @@ class RecipeFacet():
|
|||||||
}
|
}
|
||||||
caches['default'].set(self._SEARCH_CACHE_KEY, self._cache, self._cache_timeout)
|
caches['default'].set(self._SEARCH_CACHE_KEY, self._cache, self._cache_timeout)
|
||||||
|
|
||||||
def get_facets(self):
|
def get_facets(self, from_cache=False):
|
||||||
if self._cache is None:
|
if from_cache:
|
||||||
pass
|
return {
|
||||||
|
'cache_key': self.hash_key or '',
|
||||||
|
'Ratings': self.Ratings or {},
|
||||||
|
'Recent': self.Recent or [],
|
||||||
|
'Keywords': self.Keywords or [],
|
||||||
|
'Foods': self.Foods or [],
|
||||||
|
'Books': self.Books or []
|
||||||
|
}
|
||||||
return {
|
return {
|
||||||
'cache_key': self.hash_key,
|
'cache_key': self.hash_key,
|
||||||
'Ratings': self.get_ratings(),
|
'Ratings': self.get_ratings(),
|
||||||
|
@ -604,6 +604,8 @@ class RecipePagination(PageNumberPagination):
|
|||||||
max_page_size = 100
|
max_page_size = 100
|
||||||
|
|
||||||
def paginate_queryset(self, queryset, request, view=None):
|
def paginate_queryset(self, queryset, request, view=None):
|
||||||
|
if queryset is None:
|
||||||
|
raise Exception
|
||||||
self.facets = RecipeFacet(request, queryset=queryset)
|
self.facets = RecipeFacet(request, queryset=queryset)
|
||||||
return super().paginate_queryset(queryset, request, view)
|
return super().paginate_queryset(queryset, request, view)
|
||||||
|
|
||||||
@ -613,7 +615,7 @@ class RecipePagination(PageNumberPagination):
|
|||||||
('next', self.get_next_link()),
|
('next', self.get_next_link()),
|
||||||
('previous', self.get_previous_link()),
|
('previous', self.get_previous_link()),
|
||||||
('results', data),
|
('results', data),
|
||||||
('facets', self.facets.get_facets())
|
('facets', self.facets.get_facets(from_cache=True))
|
||||||
]))
|
]))
|
||||||
|
|
||||||
|
|
||||||
|
@ -98,9 +98,10 @@
|
|||||||
v-model="settings.search_keywords"
|
v-model="settings.search_keywords"
|
||||||
:options="facets.Keywords"
|
:options="facets.Keywords"
|
||||||
:load-options="loadKeywordChildren"
|
:load-options="loadKeywordChildren"
|
||||||
:flat="true"
|
|
||||||
searchNested
|
|
||||||
:multiple="true"
|
:multiple="true"
|
||||||
|
:flat="true"
|
||||||
|
:auto-load-root-options="false"
|
||||||
|
searchNested
|
||||||
:placeholder="$t('Keywords')"
|
:placeholder="$t('Keywords')"
|
||||||
:normalizer="normalizer"
|
:normalizer="normalizer"
|
||||||
@input="refreshData(false)"
|
@input="refreshData(false)"
|
||||||
@ -126,9 +127,10 @@
|
|||||||
v-model="settings.search_foods"
|
v-model="settings.search_foods"
|
||||||
:options="facets.Foods"
|
:options="facets.Foods"
|
||||||
:load-options="loadFoodChildren"
|
:load-options="loadFoodChildren"
|
||||||
:flat="true"
|
|
||||||
searchNested
|
|
||||||
:multiple="true"
|
:multiple="true"
|
||||||
|
:flat="true"
|
||||||
|
:auto-load-root-options="false"
|
||||||
|
searchNested
|
||||||
:placeholder="$t('Ingredients')"
|
:placeholder="$t('Ingredients')"
|
||||||
:normalizer="normalizer"
|
:normalizer="normalizer"
|
||||||
@input="refreshData(false)"
|
@input="refreshData(false)"
|
||||||
@ -400,22 +402,28 @@ export default {
|
|||||||
if (!this.searchFiltered) {
|
if (!this.searchFiltered) {
|
||||||
params.options = { query: { last_viewed: this.settings.recently_viewed } }
|
params.options = { query: { last_viewed: this.settings.recently_viewed } }
|
||||||
}
|
}
|
||||||
this.genericAPI(this.Models.RECIPE, this.Actions.LIST, params).then((result) => {
|
this.genericAPI(this.Models.RECIPE, this.Actions.LIST, params)
|
||||||
window.scrollTo(0, 0)
|
.then((result) => {
|
||||||
this.pagination_count = result.data.count
|
window.scrollTo(0, 0)
|
||||||
|
this.pagination_count = result.data.count
|
||||||
|
|
||||||
this.facets = result.data.facets
|
this.facets = result.data.facets
|
||||||
// if (this.facets?.cache_key) {
|
// if (this.facets?.cache_key) {
|
||||||
// this.getFacets(this.facets.cache_key)
|
// this.getFacets(this.facets.cache_key)
|
||||||
// }
|
// }
|
||||||
this.recipes = this.removeDuplicates(result.data.results, (recipe) => recipe.id)
|
this.recipes = this.removeDuplicates(result.data.results, (recipe) => recipe.id)
|
||||||
if (!this.searchFiltered) {
|
if (!this.searchFiltered) {
|
||||||
// if meal plans are being shown - filter out any meal plan recipes from the recipe list
|
// if meal plans are being shown - filter out any meal plan recipes from the recipe list
|
||||||
let mealPlans = []
|
let mealPlans = []
|
||||||
this.meal_plans.forEach((x) => mealPlans.push(x.recipe.id))
|
this.meal_plans.forEach((x) => mealPlans.push(x.recipe.id))
|
||||||
this.recipes = this.recipes.filter((recipe) => !mealPlans.includes(recipe.id))
|
this.recipes = this.recipes.filter((recipe) => !mealPlans.includes(recipe.id))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
.then(() => {
|
||||||
|
this.$nextTick(function () {
|
||||||
|
this.getFacets(this.facets?.cache_key)
|
||||||
|
})
|
||||||
|
})
|
||||||
},
|
},
|
||||||
openRandom: function () {
|
openRandom: function () {
|
||||||
this.refreshData(true)
|
this.refreshData(true)
|
||||||
@ -525,8 +533,6 @@ export default {
|
|||||||
if (this.facets?.cache_key) {
|
if (this.facets?.cache_key) {
|
||||||
this.getFacets(this.facets.cache_key, "food", parentNode.id).then(callback())
|
this.getFacets(this.facets.cache_key, "food", parentNode.id).then(callback())
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
callback()
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
loadKeywordChildren({ action, parentNode, callback }) {
|
loadKeywordChildren({ action, parentNode, callback }) {
|
||||||
@ -538,8 +544,6 @@ export default {
|
|||||||
if (this.facets?.cache_key) {
|
if (this.facets?.cache_key) {
|
||||||
this.getFacets(this.facets.cache_key, "keyword", parentNode.id).then(callback())
|
this.getFacets(this.facets.cache_key, "keyword", parentNode.id).then(callback())
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
callback()
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -282,5 +282,9 @@
|
|||||||
"shopping_add_onhand_desc": "Mark food 'On Hand' when checked off shopping list.",
|
"shopping_add_onhand_desc": "Mark food 'On Hand' when checked off shopping list.",
|
||||||
"shopping_add_onhand": "Auto On Hand",
|
"shopping_add_onhand": "Auto On Hand",
|
||||||
"related_recipes": "Related Recipes",
|
"related_recipes": "Related Recipes",
|
||||||
"today_recipes": "Today's Recipes"
|
"today_recipes": "Today's Recipes",
|
||||||
|
"mark_complete": "Mark Complete",
|
||||||
|
"QuickEntry": "Quick Entry",
|
||||||
|
"shopping_add_onhand_desc": "Mark food 'On Hand' when checked off shopping list.",
|
||||||
|
"shopping_add_onhand": "Auto On Hand"
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user