Revert "Replace spotify data cache with new cached properties"

This reverts commit a3d9fa177e.
This commit is contained in:
Jake Howard 2022-10-02 16:17:49 +01:00
parent 5c49906da3
commit cd48ae7810
Signed by: jake
GPG Key ID: 57AFB45680EDD477
4 changed files with 32 additions and 48 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_model_property_cache @weekly ./manage.py refresh_spotify_playlists
*/10 * * * * ./manage.py publish_scheduled_pages */10 * * * * ./manage.py publish_scheduled_pages

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])

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,9 +60,20 @@ class SpotifyPlaylistPage(BaseContentPage):
def subtitle(self) -> str: def subtitle(self) -> str:
return self.playlist_data["description"] return self.playlist_data["description"]
@cached_model_property @cached_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:
return client.get_playlist(self.spotify_playlist_id) 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: def get_context(self, request: HttpRequest) -> dict:
context = super().get_context(request) context = super().get_context(request)

View File

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