Add caching to equipment instance viewset

This commit is contained in:
Keannu Bernasol 2024-01-07 02:40:49 +08:00
parent b0b1f4db86
commit 2e9918f12a
5 changed files with 13 additions and 10 deletions

View file

@ -29,7 +29,8 @@ services:
- "11211:11211" - "11211:11211"
entrypoint: entrypoint:
- memcached - memcached
- -m 64 - -m 128
- -I 10m
postgres: postgres:
image: postgres image: postgres

View file

@ -49,7 +49,8 @@ class EquipmentInstance(models.Model):
return f'{self.equipment.name}' return f'{self.equipment.name}'
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
cache.delete('equipments') cache.delete('available_equipment_instances')
cache.delete('equipment_instances')
return super().save(*args, **kwargs) return super().save(*args, **kwargs)

View file

@ -67,6 +67,14 @@ class EquipmentInstanceViewSet(viewsets.ModelViewSet):
serializer_class = serializers.EquipmentInstanceSerializer serializer_class = serializers.EquipmentInstanceSerializer
queryset = EquipmentInstance.objects.all().order_by('id') 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): class AvailableEquipmentInstanceViewSet(generics.ListAPIView):
if (not DEBUG): if (not DEBUG):
@ -97,7 +105,7 @@ class AvailableEquipmentInstanceViewSet(generics.ListAPIView):
queryset = EquipmentInstance.objects.exclude( queryset = EquipmentInstance.objects.exclude(
id__in=non_finalized_equipments.values_list('id', flat=True) 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') return queryset.prefetch_related('equipment')

View file

@ -1301,9 +1301,6 @@ components:
type: string type: string
format: date-time format: date-time
readOnly: true readOnly: true
last_updated_by:
type: string
readOnly: true
date_added: date_added:
type: string type: string
format: date-time format: date-time
@ -1315,7 +1312,6 @@ components:
- equipment_name - equipment_name
- id - id
- last_updated - last_updated
- last_updated_by
- status - status
EquipmentInstanceLog: EquipmentInstanceLog:
type: object type: object
@ -1560,9 +1556,6 @@ components:
type: string type: string
format: date-time format: date-time
readOnly: true readOnly: true
last_updated_by:
type: string
readOnly: true
date_added: date_added:
type: string type: string
format: date-time format: date-time