[Audio] Add a local cache to reduce YouTube API calls #2837

Closed
Drapersniper wants to merge 10 commits from localcache into V3/develop
Drapersniper commented 2019-07-04 17:37:43 +02:00 (Migrated from github.com)

Type

  • Bugfix
  • Enhancement
  • New feature

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 ....

### Type - [ ] Bugfix - [X] Enhancement - [X] New feature ### 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 ....
Twentysix26 (Migrated from github.com) reviewed 2019-07-04 17:37:43 +02:00
Stylite commented 2019-07-17 17:04:10 +02:00 (Migrated from github.com)

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?

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?
Drapersniper commented 2019-07-17 17:11:06 +02:00 (Migrated from github.com)

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

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
Drapersniper commented 2019-07-17 18:02:45 +02:00 (Migrated from github.com)

Ah I misunderstood the question from @Stylite, He was asking why the Spotify URL isn't cached.

This is for a few reasons:

  1. Reduce Cache size (This has the potential to have millions of entries since the cache is never wiped)
  2. Spotify doesn't have hard limits on it's quota AFAIK
  3. Doing so would require a full rework of 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/tracks
  • https://api.spotify.com/v1/albums
  • https://api.spotify.com/v1/playlists

All of which returns multiple links and with #2861 we would only want to cache entries from https://api.spotify.com/v1/tracks and 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?

Ah I misunderstood the question from @Stylite, He was asking why the Spotify URL isn't cached. This is for a few reasons: 1. Reduce Cache size (This has the potential to have millions of entries since the cache is never wiped) 1. Spotify doesn't have hard limits on it's quota AFAIK 1. Doing so would require a full rework of `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/tracks` - `https://api.spotify.com/v1/albums` - `https://api.spotify.com/v1/playlists` All of which returns multiple links and with #2861 we would only want to cache entries from `https://api.spotify.com/v1/tracks` and 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?
mikeshardmind commented 2019-07-17 19:28:19 +02:00 (Migrated from github.com)

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)

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)
Drapersniper commented 2019-07-19 10:51:47 +02:00 (Migrated from github.com)

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 simple cache option in case we decide that 2890 is not suitable to core.

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 `simple` cache option in case we decide that 2890 is not suitable to core.
Drapersniper commented 2019-08-01 14:25:11 +02:00 (Migrated from github.com)

Superseded by #2904

Superseded by #2904

Pull request closed

Sign in to join this conversation.
No reviewers
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
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Mrcoolgamer11/Red-DiscordBot!2837
No description provided.