-
-
Notifications
You must be signed in to change notification settings - Fork 602
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cache the ingredient list view and increase count method (only postgres)
While this caches the whole page and not parts of it (so things like the currently logged user will be shown), this should get us some breathing room while we find a permanent solution
- Loading branch information
1 parent
eb14b3d
commit e68afe7
Showing
6 changed files
with
88 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# This file is part of wger Workout Manager <https://github.com/wger-project>. | ||
# Copyright (C) wger Team | ||
# | ||
# wger Workout Manager is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU Affero General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# wger Workout Manager is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU Affero General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Affero General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
# Django | ||
from django.conf import settings | ||
from django.db import connections | ||
from django.db.models import ( | ||
Manager, | ||
QuerySet, | ||
) | ||
|
||
# wger | ||
from wger.utils.db import is_postgres_db | ||
|
||
|
||
class ApproximateCountQuerySet(QuerySet): | ||
""" | ||
Approximate count query set, postgreSQL only | ||
While this doesn't return an exact count, it does return an approximate | ||
in a much shorter amount of time, which is specially important for the | ||
ingredients | ||
https://testdriven.io/blog/django-approximate-counting/ | ||
""" | ||
|
||
def count(self): | ||
if self.query.where: | ||
return super(ApproximateCountQuerySet, self).count() | ||
|
||
if is_postgres_db() and not settings.TESTING: | ||
cursor = connections[self.db].cursor() | ||
cursor.execute( | ||
"SELECT reltuples FROM pg_class WHERE relname = '%s';" % self.model._meta.db_table | ||
) | ||
|
||
return int(cursor.fetchone()[0]) | ||
|
||
return super(ApproximateCountQuerySet, self).count() | ||
|
||
|
||
ApproximateCountManager = Manager.from_queryset(ApproximateCountQuerySet) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -540,6 +540,7 @@ | |
'DOWNLOAD_INGREDIENTS_FROM': DOWNLOAD_INGREDIENT_WGER, | ||
'EMAIL_FROM': 'wger Workout Manager <[email protected]>', | ||
'EXERCISE_CACHE_TTL': 3600, | ||
'INGREDIENT_CACHE_TTL': 604800, # one week | ||
'MIN_ACCOUNT_AGE_TO_TRUST': 21, | ||
'SYNC_EXERCISES_CELERY': False, | ||
'SYNC_EXERCISE_IMAGES_CELERY': False, | ||
|