Improve query caching

This commit is contained in:
Keannu Bernasol 2024-01-07 12:21:14 +08:00
parent 8e43e06ec2
commit e482979d20
4 changed files with 28 additions and 6 deletions

View file

@ -28,7 +28,7 @@ BASE_DIR = Path(__file__).resolve().parent.parent
SECRET_KEY = str(os.getenv('SECRET_KEY')) SECRET_KEY = str(os.getenv('SECRET_KEY'))
# SECURITY WARNING: don't run with debug turned on in production! # SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False DEBUG = True
ALLOWED_HOSTS = ['127.0.0.1', 'localhost', '*'] ALLOWED_HOSTS = ['127.0.0.1', 'localhost', '*']
CSRF_TRUSTED_ORIGINS = [ CSRF_TRUSTED_ORIGINS = [

View file

@ -99,12 +99,12 @@ class AvailableEquipmentInstanceViewSet(generics.ListAPIView):
# Get all equipment instances associated with non-finalized transactions # Get all equipment instances associated with non-finalized transactions
non_finalized_equipments = EquipmentInstance.objects.filter( non_finalized_equipments = EquipmentInstance.objects.filter(
transaction__in=non_finalized_transactions transaction__in=non_finalized_transactions
).prefetch_related('equipment') ).order_by('id').prefetch_related('equipment')
# Get all equipment instances which are not associated with non-finalized transactions # Get all equipment instances which are not associated with non-finalized transactions
queryset = EquipmentInstance.objects.exclude( queryset = EquipmentInstance.objects.exclude(
id__in=non_finalized_equipments.values_list( id__in=non_finalized_equipments.values_list(
'id', flat=True).order_by('id') 'id', flat=True)
) )
cache.set(key, queryset, timeout=60*60*24) cache.set(key, queryset, timeout=60*60*24)

View file

@ -603,6 +603,7 @@ paths:
- api - api
security: security:
- jwtAuth: [] - jwtAuth: []
- {}
responses: responses:
'200': '200':
content: content:
@ -630,6 +631,7 @@ paths:
required: true required: true
security: security:
- jwtAuth: [] - jwtAuth: []
- {}
responses: responses:
'201': '201':
content: content:
@ -650,6 +652,7 @@ paths:
- api - api
security: security:
- jwtAuth: [] - jwtAuth: []
- {}
responses: responses:
'200': '200':
content: content:
@ -673,6 +676,7 @@ paths:
- api - api
security: security:
- jwtAuth: [] - jwtAuth: []
- {}
responses: responses:
'200': '200':
content: content:
@ -705,6 +709,7 @@ paths:
required: true required: true
security: security:
- jwtAuth: [] - jwtAuth: []
- {}
responses: responses:
'200': '200':
content: content:
@ -736,6 +741,7 @@ paths:
$ref: '#/components/schemas/PatchedEquipmentInstance' $ref: '#/components/schemas/PatchedEquipmentInstance'
security: security:
- jwtAuth: [] - jwtAuth: []
- {}
responses: responses:
'200': '200':
content: content:
@ -756,6 +762,7 @@ paths:
- api - api
security: security:
- jwtAuth: [] - jwtAuth: []
- {}
responses: responses:
'204': '204':
description: No response body description: No response body
@ -766,6 +773,7 @@ paths:
- api - api
security: security:
- jwtAuth: [] - jwtAuth: []
- {}
responses: responses:
'200': '200':
content: content:
@ -782,6 +790,7 @@ paths:
- api - api
security: security:
- jwtAuth: [] - jwtAuth: []
- {}
responses: responses:
'200': '200':
content: content:
@ -798,6 +807,7 @@ paths:
- api - api
security: security:
- jwtAuth: [] - jwtAuth: []
- {}
responses: responses:
'200': '200':
content: content:
@ -814,6 +824,7 @@ paths:
- api - api
security: security:
- jwtAuth: [] - jwtAuth: []
- {}
responses: responses:
'200': '200':
content: content:
@ -841,6 +852,7 @@ paths:
required: true required: true
security: security:
- jwtAuth: [] - jwtAuth: []
- {}
responses: responses:
'201': '201':
content: content:
@ -861,6 +873,7 @@ paths:
- api - api
security: security:
- jwtAuth: [] - jwtAuth: []
- {}
responses: responses:
'200': '200':
content: content:
@ -884,6 +897,7 @@ paths:
- api - api
security: security:
- jwtAuth: [] - jwtAuth: []
- {}
responses: responses:
'200': '200':
content: content:
@ -916,6 +930,7 @@ paths:
required: true required: true
security: security:
- jwtAuth: [] - jwtAuth: []
- {}
responses: responses:
'200': '200':
content: content:
@ -947,6 +962,7 @@ paths:
$ref: '#/components/schemas/PatchedEquipment' $ref: '#/components/schemas/PatchedEquipment'
security: security:
- jwtAuth: [] - jwtAuth: []
- {}
responses: responses:
'200': '200':
content: content:
@ -967,6 +983,7 @@ paths:
- api - api
security: security:
- jwtAuth: [] - jwtAuth: []
- {}
responses: responses:
'204': '204':
description: No response body description: No response body
@ -977,6 +994,7 @@ paths:
- api - api
security: security:
- jwtAuth: [] - jwtAuth: []
- {}
responses: responses:
'200': '200':
content: content:
@ -993,6 +1011,7 @@ paths:
- api - api
security: security:
- jwtAuth: [] - jwtAuth: []
- {}
responses: responses:
'200': '200':
content: content:

View file

@ -191,9 +191,10 @@ class TransactionSerializer(serializers.HyperlinkedModelSerializer):
# This updates the status field of all equipment instances in a single query # This updates the status field of all equipment instances in a single query
EquipmentInstance.objects.filter( EquipmentInstance.objects.filter(
id__in=[equipment.id for equipment in equipments]).update(status='Available') id__in=[equipment.id for equipment in equipments]).update(status='Available')
q = super().update(instance, validated_data)
cache.delete('available_equipment_instances') cache.delete('available_equipment_instances')
cache.delete('equipment_instances') cache.delete('equipment_instances')
return super().update(instance, validated_data) return q
# For Approved transactions, # For Approved transactions,
# If not changing to Borrowed or Cancelled, throw an error # If not changing to Borrowed or Cancelled, throw an error
@ -210,9 +211,10 @@ class TransactionSerializer(serializers.HyperlinkedModelSerializer):
# This updates the status field of all equipment instances in a single query # This updates the status field of all equipment instances in a single query
EquipmentInstance.objects.filter( EquipmentInstance.objects.filter(
id__in=[equipment.id for equipment in equipments]).update(status='Available') id__in=[equipment.id for equipment in equipments]).update(status='Available')
q = super().update(instance, validated_data)
cache.delete('available_equipment_instances') cache.delete('available_equipment_instances')
cache.delete('equipment_instances') cache.delete('equipment_instances')
return super().update(instance, validated_data) return q
# If there are no issues and a transaction changes from Approved to Borrowed, label the selected equipment's statuses as Borrowed # If there are no issues and a transaction changes from Approved to Borrowed, label the selected equipment's statuses as Borrowed
if instance.transaction_status == "Approved" and validated_data.get('transaction_status') == "Borrowed": if instance.transaction_status == "Approved" and validated_data.get('transaction_status') == "Borrowed":
@ -254,9 +256,10 @@ class TransactionSerializer(serializers.HyperlinkedModelSerializer):
# This updates the status field of all equipment instances in a single query # This updates the status field of all equipment instances in a single query
EquipmentInstance.objects.filter( EquipmentInstance.objects.filter(
id__in=[equipment.id for equipment in equipments]).update(status='Available') id__in=[equipment.id for equipment in equipments]).update(status='Available')
q = super().update(instance, validated_data)
cache.delete('available_equipment_instances') cache.delete('available_equipment_instances')
cache.delete('equipment_instances') cache.delete('equipment_instances')
return super().update(instance, validated_data) return q
# If the transaction changes from Returned: Pending Checking to With Breakages, we create a Breakage Report instance # If the transaction changes from Returned: Pending Checking to With Breakages, we create a Breakage Report instance
if instance.transaction_status == "Returned: Pending Checking" and validated_data.get('transaction_status') == "With Breakages: Pending Resolution": if instance.transaction_status == "Returned: Pending Checking" and validated_data.get('transaction_status') == "With Breakages: Pending Resolution":