Skip to content

Add local caching for viewed songs #2

Description

@KaykCaputo

Description

Currently, every time a user views a song, the app fetches it from the internet. Implementing local caching would improve performance and enable offline access to previously viewed songs.

Benefits

  • Faster load times for repeat views
  • Reduced bandwidth usage
  • Offline access to cached songs
  • Better user experience

Proposed Implementation

Use a simple JSON-based cache:

import json
import hashlib
from pathlib import Path

class SongCache:
    def __init__(self, cache_dir=".cache"):
        self.cache_dir = Path(cache_dir)
        self.cache_dir.mkdir(exist_ok=True)
    
    def get_cache_key(self, title, artist):
        return hashlib.md5(f"{title}_{artist}".encode()).hexdigest()
    
    def get(self, title, artist):
        cache_file = self.cache_dir / f"{self.get_cache_key(title, artist)}.json"
        if cache_file.exists():
            return json.loads(cache_file.read_text())
        return None
    
    def set(self, title, artist, content):
        cache_file = self.cache_dir / f"{self.get_cache_key(title, artist)}.json"
        cache_file.write_text(json.dumps({
            "title": title,
            "artist": artist,
            "content": content
        }))

Additional Features

  • Cache expiration (e.g., 30 days)
  • Cache size management
  • Manual cache clearing option in settings
  • Cache statistics display

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions