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'))
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
DEBUG = True
ALLOWED_HOSTS = ['127.0.0.1', 'localhost', '*']
CSRF_TRUSTED_ORIGINS = [

View file

@ -99,12 +99,12 @@ class AvailableEquipmentInstanceViewSet(generics.ListAPIView):
# Get all equipment instances associated with non-finalized transactions
non_finalized_equipments = EquipmentInstance.objects.filter(
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
queryset = EquipmentInstance.objects.exclude(
id__in=non_finalized_equipments.values_list(
'id', flat=True).order_by('id')
'id', flat=True)
)
cache.set(key, queryset, timeout=60*60*24)

View file

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

View file

@ -191,9 +191,10 @@ class TransactionSerializer(serializers.HyperlinkedModelSerializer):
# This updates the status field of all equipment instances in a single query
EquipmentInstance.objects.filter(
id__in=[equipment.id for equipment in equipments]).update(status='Available')
q = super().update(instance, validated_data)
cache.delete('available_equipment_instances')
cache.delete('equipment_instances')
return super().update(instance, validated_data)
return q
# For Approved transactions,
# 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
EquipmentInstance.objects.filter(
id__in=[equipment.id for equipment in equipments]).update(status='Available')
q = super().update(instance, validated_data)
cache.delete('available_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 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
EquipmentInstance.objects.filter(
id__in=[equipment.id for equipment in equipments]).update(status='Available')
q = super().update(instance, validated_data)
cache.delete('available_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 instance.transaction_status == "Returned: Pending Checking" and validated_data.get('transaction_status') == "With Breakages: Pending Resolution":