from django.core.cache import cache

from music.models import Artist, Album

CACHE_TTL = 300


def music_lists(request):
    artist_list = cache.get("music_lists_artist_list")
    if artist_list is None:
        artist_list = list(Artist.objects.all().only("id", "name"))
        cache.set("music_lists_artist_list", artist_list, CACHE_TTL)

    album_list = cache.get("music_lists_album_list")
    if album_list is None:
        album_list = list(Album.objects.all().only("id", "name"))
        cache.set("music_lists_album_list", album_list, CACHE_TTL)

    return {
        "artist_list": artist_list,
        "album_list": album_list,
    }
