[Audio] Add a local cache to reduce YouTube API calls #2837
No reviewers
Labels
No labels
Automated PR
Blocked
Blocked By: Damage Control
Blocked By: Dependency
Blocked By: Other PR
Breaking Change
Category: CI
Category: Cogs - Admin
Category: Cogs - Alias
Category: Cogs - Audio
Category: Cogs - Bank
Category: Cogs - Cleanup
Category: Cogs - CustomCommands
Category: Cogs - Dev
Category: Cogs - Downloader
Category: Cogs - Economy
Category: Cogs - Filter
Category: Cogs - General
Category: Cogs - Image
Category: Cogs - Mod
Category: Cogs - Modlog
Category: Cogs - Mutes
Category: Cogs - Permissions
Category: Cogs - Reports
Category: Cogs - Streams
Category: Cogs - Trivia
Category: Cogs - Trivia - Lists
Category: Cogs - Warnings
Category: Core - API - App Commands Package
Category: Core - API - Audio
Category: Core - API - Bank
Category: Core - API - Commands Package
Category: Core - API - Config
Category: Core - API - Other
Category: Core - API - Utils Package
Category: Core - Bot Class
Category: Core - Bot Commands
Category: Core - Command-line Interfaces
Category: Core - Downloader
Category: Core - Help
Category: Core - i18n
Category: Core - Modlog
Category: Core - Other Internals
Category: Core - RPC/ZMQ
Category: Docker
Category: Docs - Changelogs
Category: Docs - For Developers
Category: Docs - Install Guides
Category: Docs - Other
Category: Docs - User Guides
Category: Meta
Category: RPC/ZMQ methods
Category: Vendored Packages
Changelog Entry: Added
Changelog Entry: Pending
Changelog Entry: Skipped
Closed: Duplicate
Closed: External
Closed: Invalid
Closed: User Error
Closed: Won't Fix
Complex Issue
Docs-only
Feature Branch
Good First Issue
Help Wanted
High Priority
Needs Backport To stable-docs
No Activity
No Repro
QA: Bypassed
QA: Changes Requested
QA: Passed
Release Blocker
Status: Accepted
Status: Frozen
Status: In Progress
Status: Needs Discussion
Status: Needs Info
Status: Needs Triage
Status: PRs Welcome
Type: Bug
Type: Dependency Update
Type: Deprecation
Type: Enhancement
Type: Feature
Type: Informational
Type: Merge
Type: Optimisation
Type: Question
Type: Removal
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
Mrcoolgamer11/Red-DiscordBot!2837
Loading…
Reference in a new issue
No description provided.
Delete branch "localcache"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Type
Description of the changes
This PR adds a local cache to the Audio cog
This cache will be loaded in memory on cog load and will only write to memory on Cog unload/bot crash
This is to avoid killing your back-end with reads and write operations
This will allows the user to play Spotify songs that have been played in the past even if they run out of their YouTube API quota
Sorry for the repeated PR... was trying to fix the mess of a fork I ended up with when I created the original PR from v3/develop ... Note kids ... when pushing a PR double check you're in the correct branch ....
I looked this over and I have two questions. Why are you not using config for the cache and why doesn't the cache include the song link?
The cache is a dict for {song name as given by Spotify : YouTube url}
Reason config is not used is because this file can get stupidly large, so I don’t want to kill people’s backend with reads/writes so it only writes once the bot is shutdown/audio is unloaded. And cache is loaded to memory on load
Specially when #2861 is merged as that will have a [p]playlist update command and allow users to create playlists
Ah I misunderstood the question from @Stylite, He was asking why the Spotify URL isn't cached.
This is for a few reasons:
Audio._spotify_playlist()I could update this PR to cache Spotify API calls too but the issue I see here is that we use the following end points:
https://api.spotify.com/v1/trackshttps://api.spotify.com/v1/albumshttps://api.spotify.com/v1/playlistsAll of which returns multiple links and with #2861 we would only want to cache entries from
https://api.spotify.com/v1/tracksand this cache would become even larger than the current PR cache so I'd likely split it into 2 files@aikaterna If this is something you think we should cache it let me know and I'll update the PR before you review it, if so what information should be cached ? First song returned so if the user used a specific Link it will always return the same track regardless of what that link may return in the future?
This needs to write more frequently. While I understand the concerns on backends, there are other solutions such as a max cache size with age out, uing sqlite3 in the cog data dir (etc)
Okay so I done #2890 which uses SQL caching instead of a JSON to Memory (although a big slower, negligible really), this allows rooms to cache other things such as Lavalink track info to which could potentially allow for more tracks to be loaded in a short period without Lavalink timing out and throwing an error when loading multiple songs back to back or large playlists.
I would recommend we review that one and merge that instead of this one, but leaving this one open as a
simplecache option in case we decide that 2890 is not suitable to core.Superseded by #2904
Pull request closed