Replace spotify data cache with new cached properties

Also replace the update script with one which will do everything
This commit is contained in:
Jake Howard 2022-09-30 09:07:31 +01:00
parent 7c008c2149
commit a3d9fa177e
Signed by: jake
GPG key ID: 57AFB45680EDD477
4 changed files with 48 additions and 32 deletions

View file

@ -1,5 +1,5 @@
@weekly ./manage.py update_index @weekly ./manage.py update_index
@daily ./manage.py clearsessions @daily ./manage.py clearsessions
@daily ./manage.py update_unsplash_photos @daily ./manage.py update_unsplash_photos
@weekly ./manage.py refresh_spotify_playlists @weekly ./manage.py refresh_model_property_cache
*/10 * * * * ./manage.py publish_scheduled_pages */10 * * * * ./manage.py publish_scheduled_pages

View file

@ -1,17 +0,0 @@
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])

View file

@ -1,13 +1,13 @@
from datetime import timedelta from datetime import timedelta
from functools import cached_property from functools import cached_property
from django.core.cache import cache
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.db import models from django.db import models
from django.http.request import HttpRequest from django.http.request import HttpRequest
from wagtail.admin.panels import FieldPanel from wagtail.admin.panels import FieldPanel
from website.common.models import BaseContentPage from website.common.models import BaseContentPage
from website.utils.cache import cached_model_property
from . import client from . import client
@ -60,20 +60,9 @@ class SpotifyPlaylistPage(BaseContentPage):
def subtitle(self) -> str: def subtitle(self) -> str:
return self.playlist_data["description"] return self.playlist_data["description"]
@cached_property @cached_model_property
def playlist_cache_key(self) -> str:
return f"spotify_playlist_{self.spotify_playlist_id}"
@cached_property
def playlist_data(self) -> dict: def playlist_data(self) -> dict:
playlist_data = cache.get(self.playlist_cache_key) return client.get_playlist(self.spotify_playlist_id)
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: def get_context(self, request: HttpRequest) -> dict:
context = super().get_context(request) context = super().get_context(request)

View file

@ -0,0 +1,44 @@
from django.apps import apps
from django.core.cache import cache
from django.core.management.base import BaseCommand
from website.utils.cache import get_cache_key, get_cached_model_properties
from website.utils.queue import enqueue_or_sync
def refresh_cache(
app_label: str, model_name: str, model_id: int, cached_model_properties: list[str]
) -> None:
Model = apps.get_model(app_label, model_name)
instance = Model.objects.get(id=model_id)
cache.delete_many(
[
get_cache_key(instance, getattr(instance.__class__, name).real_func)
for name in cached_model_properties
]
)
# Prime caches again
for name in cached_model_properties:
getattr(instance, name)
class Command(BaseCommand):
def handle(self, *args: list, **options: dict) -> None:
for Model in apps.get_models():
cached_model_properties = get_cached_model_properties(Model)
if not cached_model_properties:
continue
for instance_id in (
Model.objects.all().values_list("id", flat=True).iterator()
):
enqueue_or_sync(
refresh_cache,
[
Model._meta.app_label,
Model._meta.model_name,
instance_id,
cached_model_properties,
],
)