[PR #3599] [MERGED] Add in-memory user cache #4031

Closed
opened 2026-04-25 00:18:02 +02:00 by adam · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/advplyr/audiobookshelf/pull/3599
Author: @mikiher
Created: 11/10/2024
Status: Merged
Merged: 11/11/2024
Merged by: @advplyr

Base: masterHead: add-user-cache


📝 Commits (1)

📊 Changes

2 files changed (+117 additions, -25 deletions)

View changed files

📝 server/Auth.js (+10 -20)
📝 server/models/User.js (+107 -5)

📄 Description

This PR Addresses point 5 from the conclusions of the image loading performance analysis.

An in-memory cache check and insert is introduced in all user model methods that return a single user (with their media progress) from the database. Caching this saves around 15 ms per authenticated request.

The cache doesn't have a TTL, and is invalidated only if needed (i.e. when changes to the user were not made through the cached object). I have convinced myself that all relevant writes to user and media progress are made through user model methods that are covered by a call to the maybeInvalidate method (and specifically in createUpdateMediaProgressFromPayload)

Note that this covers point 5, but I've decided not address point 4 (Remove media progress from req.user), since at this point media progress is quite entangled with req.user and requires quite a few changes in the code, which aren't worth it since the cache already solves the db performance issue. I think it would still be worthwhile disentangle them in the future, but this change makes it less urgent.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/advplyr/audiobookshelf/pull/3599 **Author:** [@mikiher](https://github.com/mikiher) **Created:** 11/10/2024 **Status:** ✅ Merged **Merged:** 11/11/2024 **Merged by:** [@advplyr](https://github.com/advplyr) **Base:** `master` ← **Head:** `add-user-cache` --- ### 📝 Commits (1) - [`a382482`](https://github.com/advplyr/audiobookshelf/commit/a38248217303413fd9bb14bd96c79801b43c60b2) Add in-memory user cache ### 📊 Changes **2 files changed** (+117 additions, -25 deletions) <details> <summary>View changed files</summary> 📝 `server/Auth.js` (+10 -20) 📝 `server/models/User.js` (+107 -5) </details> ### 📄 Description This PR Addresses point 5 from the [conclusions](https://github.com/advplyr/audiobookshelf/discussions/3570#discussioncomment-11119792) of the image loading performance analysis. An in-memory cache check and insert is introduced in all user model methods that return a single user (with their media progress) from the database. Caching this saves around 15 ms per authenticated request. The cache doesn't have a TTL, and is invalidated only if needed (i.e. when changes to the user were not made through the cached object). I have convinced myself that all relevant writes to user and media progress are made through user model methods that are covered by a call to the `maybeInvalidate` method (and specifically in `createUpdateMediaProgressFromPayload`) Note that this covers point 5, but I've decided not address point 4 (Remove media progress from req.user), since at this point media progress is quite entangled with req.user and requires quite a few changes in the code, which aren't worth it since the cache already solves the db performance issue. I think it would still be worthwhile disentangle them in the future, but this change makes it less urgent. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
adam added the pull-request label 2026-04-25 00:18:02 +02:00
adam closed this issue 2026-04-25 00:18:02 +02:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/audiobookshelf#4031