From 2e9918f12ad987e162dbf584ea42416ca858ed3c Mon Sep 17 00:00:00 2001 From: Keannu Bernasol Date: Sun, 7 Jan 2024 02:40:49 +0800 Subject: [PATCH] Add caching to equipment instance viewset --- available_equipment_instances.txt | 0 docker-compose.yml | 3 ++- equipment_tracker/equipments/models.py | 3 ++- equipment_tracker/equipments/views.py | 10 +++++++++- equipment_tracker/schema.yml | 7 ------- 5 files changed, 13 insertions(+), 10 deletions(-) delete mode 100644 available_equipment_instances.txt diff --git a/available_equipment_instances.txt b/available_equipment_instances.txt deleted file mode 100644 index e69de29..0000000 diff --git a/docker-compose.yml b/docker-compose.yml index 49aedab..a33bfc3 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -29,7 +29,8 @@ services: - "11211:11211" entrypoint: - memcached - - -m 64 + - -m 128 + - -I 10m postgres: image: postgres diff --git a/equipment_tracker/equipments/models.py b/equipment_tracker/equipments/models.py index 70c2dd5..fe7fa45 100644 --- a/equipment_tracker/equipments/models.py +++ b/equipment_tracker/equipments/models.py @@ -49,7 +49,8 @@ class EquipmentInstance(models.Model): return f'{self.equipment.name}' def save(self, *args, **kwargs): - cache.delete('equipments') + cache.delete('available_equipment_instances') + cache.delete('equipment_instances') return super().save(*args, **kwargs) diff --git a/equipment_tracker/equipments/views.py b/equipment_tracker/equipments/views.py index a43b907..d1a03a7 100644 --- a/equipment_tracker/equipments/views.py +++ b/equipment_tracker/equipments/views.py @@ -67,6 +67,14 @@ class EquipmentInstanceViewSet(viewsets.ModelViewSet): serializer_class = serializers.EquipmentInstanceSerializer queryset = EquipmentInstance.objects.all().order_by('id') + def get_queryset(self): + key = 'equipment_instances' + queryset = cache.get(key) + if not queryset: + queryset = super().get_queryset() + cache.set(key, queryset, timeout=60*60*24) + return queryset + class AvailableEquipmentInstanceViewSet(generics.ListAPIView): if (not DEBUG): @@ -97,7 +105,7 @@ class AvailableEquipmentInstanceViewSet(generics.ListAPIView): queryset = EquipmentInstance.objects.exclude( id__in=non_finalized_equipments.values_list('id', flat=True) ) - cache.set(key, list(queryset), timeout=60*60*24) + cache.set(key, queryset, timeout=60*60*24) return queryset.prefetch_related('equipment') diff --git a/equipment_tracker/schema.yml b/equipment_tracker/schema.yml index 2e91902..3dbb1ad 100644 --- a/equipment_tracker/schema.yml +++ b/equipment_tracker/schema.yml @@ -1301,9 +1301,6 @@ components: type: string format: date-time readOnly: true - last_updated_by: - type: string - readOnly: true date_added: type: string format: date-time @@ -1315,7 +1312,6 @@ components: - equipment_name - id - last_updated - - last_updated_by - status EquipmentInstanceLog: type: object @@ -1560,9 +1556,6 @@ components: type: string format: date-time readOnly: true - last_updated_by: - type: string - readOnly: true date_added: type: string format: date-time