cache

package
v1.3.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 13, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package cache manages favorites, blocklist, and offline cache storage

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CalculateFLACBytesPerSecond

func CalculateFLACBytesPerSecond(favoritesDir string) int64

CalculateFLACBytesPerSecond calculates average FLAC bytes/second from favorites

func DeleteCache

func DeleteCache(offlineDir, name string) error

DeleteCache removes a cache by name

func EstimateDiskUsage

func EstimateDiskUsage(bitrate, targetSeconds int, flacBytesPerSec int64) map[string]int64

EstimateDiskUsage estimates disk usage for cache recording

func FormatBytes

func FormatBytes(b int64) string

FormatBytes formats bytes into human-readable string

func FormatDuration

func FormatDuration(seconds int64) string

FormatDuration formats seconds to human-readable string

func GenerateCacheName

func GenerateCacheName(station, bitrate int) string

GenerateCacheName creates an auto-generated cache name

func GetFreeDiskSpace

func GetFreeDiskSpace(path string) (int64, error)

GetFreeDiskSpace returns free disk space in bytes for the given path. Returns 0 if the platform doesn't support querying disk space.

func ParseBitrate

func ParseBitrate(s string) (int, error)

ParseBitrate parses bitrate string like "320", "flac" to int

func ParseDuration

func ParseDuration(s string) (int, error)

ParseDuration parses duration string like "2h", "3.5h" to seconds

Types

type BlockFetcher

type BlockFetcher func(station, bitrate int) (BlockInfo, error)

BlockFetcher is a function that fetches a block of songs from RP API

type BlockInfo

type BlockInfo struct {
	BlockID string
	Songs   []CachedSong
}

BlockInfo holds metadata about a fetched block

type CacheConfig

type CacheConfig struct {
	Station       int       `json:"station"`
	Bitrate       int       `json:"bitrate"`
	TargetSeconds int       `json:"target_seconds"`
	CreatedAt     time.Time `json:"created_at"`
}

CacheConfig stores cache recording configuration

type CacheEntry

type CacheEntry struct {
	Name          string    `json:"name"`
	Station       int       `json:"station"`
	Bitrate       int       `json:"bitrate"`
	TargetSeconds int       `json:"target_seconds"`
	ActualSeconds int       `json:"actual_seconds"`
	SongCount     int       `json:"song_count"`
	SizeBytes     int64     `json:"size_bytes"`
	CreatedAt     time.Time `json:"created_at"`
}

CacheEntry represents a completed offline cache

func ListCaches

func ListCaches(offlineDir string) ([]CacheEntry, error)

ListCaches returns all available caches

type CacheIndex

type CacheIndex struct {
	Caches []CacheEntry `json:"caches"`
}

CacheIndex holds all cache entries

func LoadCacheIndex

func LoadCacheIndex(offlineDir string) (*CacheIndex, error)

LoadCacheIndex loads the cache index from file

func (*CacheIndex) Save

func (ci *CacheIndex) Save(offlineDir string) error

Save writes the cache index to file

type CacheManager

type CacheManager struct {
	// contains filtered or unexported fields
}

CacheManager manages favorites, blocklist, and offline caches

func NewCacheManager

func NewCacheManager(favoritesDir, blocklistDir string, maxFavorites int) *CacheManager

NewCacheManager creates a new cache manager

func (*CacheManager) AddBlocklist

func (c *CacheManager) AddBlocklist(song *models.Song, autoBlocked bool) error

AddBlocklist adds a song to blocklist

func (*CacheManager) AddFavorite

func (c *CacheManager) AddFavorite(song *models.Song, fileExt string) (string, error)

AddFavorite adds a song to favorites and triggers audio download

func (*CacheManager) EnsureDirectories

func (c *CacheManager) EnsureDirectories() error

EnsureDirectories creates the cache directories and loads metadata

func (*CacheManager) GetBlocklist

func (c *CacheManager) GetBlocklist() ([]CachedSong, error)

GetBlocklist returns all blocklisted songs (newest first)

func (*CacheManager) GetBlocklistCount

func (c *CacheManager) GetBlocklistCount() (int, error)

GetBlocklistCount returns the number of blocklisted songs

func (*CacheManager) GetFavoriteByEventID

func (c *CacheManager) GetFavoriteByEventID(eventID int64) (*CachedSong, error)

GetFavoriteByEventID returns a favorite song by event ID

func (*CacheManager) GetFavoriteCount

func (c *CacheManager) GetFavoriteCount() (int, error)

GetFavoriteCount returns the number of favorites

func (*CacheManager) GetFavorites

func (c *CacheManager) GetFavorites() ([]CachedSong, error)

GetFavorites returns all favorite songs (newest first)

func (*CacheManager) GetFavoritesDiskSpace

func (c *CacheManager) GetFavoritesDiskSpace() string

GetFavoritesDiskSpace calculates total disk space used by favorites

func (*CacheManager) GetOfflineDir

func (c *CacheManager) GetOfflineDir() string

GetOfflineDir returns the offline cache directory

func (*CacheManager) IsBlocked

func (c *CacheManager) IsBlocked(song *models.Song) bool

IsBlocked checks if a song is in blocklist by SongID

func (*CacheManager) IsFavorite

func (c *CacheManager) IsFavorite(song *models.Song) bool

IsFavorite checks if a song is in favorites

func (*CacheManager) RemoveBlocklist

func (c *CacheManager) RemoveBlocklist(songID int64) error

RemoveBlocklist removes a song from blocklist by SongID

func (*CacheManager) RemoveFavorite

func (c *CacheManager) RemoveFavorite(eventID int64) error

RemoveFavorite removes a song from favorites by event ID

func (*CacheManager) RemoveFavoriteBySong added in v1.2.0

func (c *CacheManager) RemoveFavoriteBySong(song *models.Song) error

RemoveFavoriteBySong removes a song from favorites using stable identity

func (*CacheManager) SetOfflineDir

func (c *CacheManager) SetOfflineDir(dir string)

SetOfflineDir sets the offline cache directory

func (*CacheManager) StartFavoriteDownload

func (c *CacheManager) StartFavoriteDownload(song *models.Song, fileExt string, onDone func(success bool))

StartFavoriteDownload downloads the audio for a favorite and adds it to metadata.

func (*CacheManager) SyncAutoBlocklist

func (c *CacheManager) SyncAutoBlocklist(songIDs map[int64]string) error

SyncAutoBlocklist synchronizes the auto-blocklist with the fetched RP low-rated songs. It removes stale AutoBlocked=true entries whose SongID is not in the new list, and adds new entries for songs that aren't already in the blocklist. songIDs are the RP song IDs that should be auto-blocked.

func (*CacheManager) ToggleBlocklist

func (c *CacheManager) ToggleBlocklist(song *models.Song) (bool, error)

ToggleBlocklist adds or removes a song from blocklist (manual, not auto)

func (*CacheManager) ToggleFavorite

func (c *CacheManager) ToggleFavorite(song *models.Song, fileExt string) (bool, string, error)

ToggleFavorite checks if a song is already a favorite.

func (*CacheManager) WaitForDownloads

func (c *CacheManager) WaitForDownloads(timeout time.Duration)

WaitForDownloads waits for all in-progress downloads to complete, up to a timeout.

type CacheRecorder

type CacheRecorder struct {
	// contains filtered or unexported fields
}

CacheRecorder handles recording a cache session

func NewCacheRecorder

func NewCacheRecorder(offlineDir, name string, station, bitrate, targetSeconds int) *CacheRecorder

NewCacheRecorder creates a new cache recorder

func (*CacheRecorder) Cleanup

func (r *CacheRecorder) Cleanup() error

Cleanup removes the entire cache directory (used on interrupt)

func (*CacheRecorder) Record

func (r *CacheRecorder) Record(blockFetcher BlockFetcher, progressFn func(ProgressInfo)) error

Record downloads songs until target duration is reached. It fetches a block, downloads all songs, then sleeps until ~120s before the last song's scheduled end time. Then it polls every 5s for a new block. Returns error or nil on success.

func (*CacheRecorder) Setup

func (r *CacheRecorder) Setup() error

Setup creates the cache directory structure and config

type CachedSong

type CachedSong struct {
	EventID         int64  `json:"event_id"`
	SongID          int64  `json:"song_id,omitempty"`
	Title           string `json:"title"`
	Artist          string `json:"artist"`
	Album           string `json:"album"`
	Year            string `json:"year"`
	Duration        int64  `json:"duration"`
	GaplessURL      string `json:"gapless_url"`
	CoverLarge      string `json:"cover_large"`
	Rating          string `json:"rating"`
	ListenerRating  string `json:"listener_rating"`
	AudioPath       string `json:"audio_path,omitempty"`
	SchedTimeMillis int64  `json:"sched_time_millis,omitempty"`
	AddedAt         int64  `json:"added_at"`
	AutoBlocked     bool   `json:"auto_blocked,omitempty"`
}

CachedSong represents a song stored in cache

func LoadCache

func LoadCache(offlineDir, name string) ([]CachedSong, error)

LoadCache loads a cache's songs for offline playback

func (*CachedSong) ToSong

func (cs *CachedSong) ToSong() *models.Song

ToSong converts a CachedSong to a models.Song

func (*CachedSong) UnmarshalJSON

func (cs *CachedSong) UnmarshalJSON(data []byte) error

UnmarshalJSON implements custom JSON unmarshaling to handle string event_id

type ProgressInfo

type ProgressInfo struct {
	SongIndex     int
	TotalSeconds  int64
	TargetSeconds int
	TotalBytes    int64
	CurrentSong   string
	Duration      int64
	Size          int64
}

ProgressInfo holds recording progress information

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL