Add management command to refresh caches of spotify playlists

This commit is contained in:
Jake Howard 2022-08-27 12:59:12 +01:00
parent 2b0779b26a
commit 3d155bb46f
Signed by: jake
GPG Key ID: 57AFB45680EDD477
2 changed files with 20 additions and 1 deletions

View File

@ -60,6 +60,8 @@ class ListingPageTestCase(TestCase):
def test_feed_accessible(self) -> None:
with self.assertNumQueries(11):
response = self.client.get(self.page.url + self.page.reverse_subpage("feed"))
response = self.client.get(
self.page.url + self.page.reverse_subpage("feed")
)
self.assertEqual(response.status_code, 200)
self.assertEqual(response["Content-Type"], "application/rss+xml; charset=utf-8")

View File

@ -0,0 +1,17 @@
from django.core.cache import cache
from django.core.management.base import BaseCommand
from website.spotify.models import SpotifyPlaylistPage
from website.utils.queue import enqueue_or_sync
def refresh_cache(page_id: int) -> None:
page = SpotifyPlaylistPage.objects.get(id=page_id)
cache.delete(page.playlist_cache_key)
page.playlist_data # Prime cache
class Command(BaseCommand):
def handle(self, *args: list, **options: dict) -> None:
for page in SpotifyPlaylistPage.objects.all().defer_streamfields().iterator():
enqueue_or_sync(refresh_cache, args=[page.id])