From 55b723ba37ea7e1ff31b3b294fa649b8a2a9fe45 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Thu, 18 Aug 2022 09:21:57 +0100 Subject: [PATCH] Cache spotify data --- website/spotify/models.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/website/spotify/models.py b/website/spotify/models.py index 289e25e..22599e3 100644 --- a/website/spotify/models.py +++ b/website/spotify/models.py @@ -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