Cache spotify data

This commit is contained in:
Jake Howard 2022-08-18 09:21:57 +01:00
parent 872a6c0b30
commit 55b723ba37
Signed by: jake
GPG Key ID: 57AFB45680EDD477

View File

@ -1,3 +1,6 @@
from functools import cached_property
from django.core.cache import cache
from django.core.exceptions import ValidationError
from django.db import models
from django.http.request import HttpRequest
@ -32,7 +35,21 @@ class SpotifyPlaylistPage(BaseContentPage):
def reading_time(self) -> int:
return 0
@cached_property
def playlist_cache_key(self) -> str:
return f"spotify_playlist_{self.spotify_playlist_id}"
def get_playlist_data(self) -> dict:
playlist_data = cache.get(self.playlist_cache_key)
if playlist_data is None:
playlist_data = client.get_playlist(self.spotify_playlist_id)
# Cache for 1 week
cache.set(self.playlist_cache_key, playlist_data, 604800)
return playlist_data
def get_context(self, request: HttpRequest) -> dict:
context = super().get_context(request)
context["playlist"] = client.get_playlist(self.spotify_playlist_id)
context["playlist"] = self.get_playlist_data()
return context