This commit is contained in:
Petitminion 2023-06-26 18:09:20 +02:00
parent 601144edf8
commit 66e49cb12e
2 changed files with 18 additions and 8 deletions

View File

@ -125,9 +125,11 @@ class SessionRadio(SimpleRadio):
def cache_batch_radio_track(self, **kwargs): def cache_batch_radio_track(self, **kwargs):
BATCH_SIZE = 100 BATCH_SIZE = 100
# get cached RadioTracks if any # get cached RadioTracks if any
try : try:
cached_evaluated_radio_tracks = pickle.loads(cache.get(f"radiosessiontracks{self.session.id}")) cached_evaluated_radio_tracks = pickle.loads(
except TypeError : cache.get(f"radiosessiontracks{self.session.id}")
)
except TypeError:
cached_evaluated_radio_tracks = None cached_evaluated_radio_tracks = None
# get the queryset and apply filters # get the queryset and apply filters
@ -173,8 +175,10 @@ class SessionRadio(SimpleRadio):
return queryset return queryset
def get_choices_v2(self, quantity, **kwargs): def get_choices_v2(self, quantity, **kwargs):
if cache.get(f"radiosessiontracks{self.session.id}") : if cache.get(f"radiosessiontracks{self.session.id}"):
cached_radio_tracks = pickle.loads(cache.get(f"radiosessiontracks{self.session.id}")) cached_radio_tracks = pickle.loads(
cache.get(f"radiosessiontracks{self.session.id}")
)
logger.info("Using redis cache for radio generation") logger.info("Using redis cache for radio generation")
radio_tracks = cached_radio_tracks radio_tracks = cached_radio_tracks
if len(radio_tracks) < quantity: if len(radio_tracks) < quantity:
@ -280,7 +284,9 @@ class CustomMultiple(SessionRadio):
def validate_session(self, data, **context): def validate_session(self, data, **context):
data = super().validate_session(data, **context) data = super().validate_session(data, **context)
if data.get("config") is None: try:
data["config"] is not None
except KeyError:
raise serializers.ValidationError( raise serializers.ValidationError(
"You must provide a configuration for this radio" "You must provide a configuration for this radio"
) )

View File

@ -208,7 +208,9 @@ class RadioSessionTracksViewSet(mixins.CreateModelMixin, viewsets.GenericViewSet
) )
# self.perform_create(serializer) # self.perform_create(serializer)
# dirty override here, since we use a different serializer for creation and detail # dirty override here, since we use a different serializer for creation and detail
evaluated_radio_tracks = pickle.loads(cache.get(f"radiosessiontracks{session.id}")) evaluated_radio_tracks = pickle.loads(
cache.get(f"radiosessiontracks{session.id}")
)
batch = evaluated_radio_tracks[:count] batch = evaluated_radio_tracks[:count]
serializer = self.serializer_class( serializer = self.serializer_class(
data=batch, data=batch,
@ -225,7 +227,9 @@ class RadioSessionTracksViewSet(mixins.CreateModelMixin, viewsets.GenericViewSet
# delete the tracks we sent from the cache # delete the tracks we sent from the cache
new_cached_radiotracks = evaluated_radio_tracks[count:] new_cached_radiotracks = evaluated_radio_tracks[count:]
cache.set(f"radiosessiontracks{session.id}", pickle.dumps(new_cached_radiotracks)) cache.set(
f"radiosessiontracks{session.id}", pickle.dumps(new_cached_radiotracks)
)
return Response( return Response(
serializer.data, status=status.HTTP_201_CREATED, headers=headers serializer.data, status=status.HTTP_201_CREATED, headers=headers