perf: read gallery hot paths from spacetime cache

This commit is contained in:
kdletters
2026-05-17 00:03:07 +08:00
parent 99f539a601
commit d9c8473504
10 changed files with 347 additions and 113 deletions

View File

@@ -181,6 +181,55 @@ impl SpacetimeClient {
pub async fn list_custom_world_gallery_entries(
&self,
) -> Result<Vec<CustomWorldGalleryEntryRecord>, SpacetimeClientError> {
let records = self.read_custom_world_gallery_entries_from_cache().await?;
if !records.is_empty()
|| self
.custom_world_gallery_legacy_sync_attempted
.swap(true, std::sync::atomic::Ordering::SeqCst)
{
return Ok(records);
}
let _ = self
.sync_custom_world_gallery_entries_via_legacy_procedure()
.await;
self.read_custom_world_gallery_entries_from_cache().await
}
async fn read_custom_world_gallery_entries_from_cache(
&self,
) -> Result<Vec<CustomWorldGalleryEntryRecord>, SpacetimeClientError> {
self.read_after_connect(move |connection| {
let recent_play_counts = public_work_recent_play_counts(connection, "custom-world");
let mut entries = connection
.db()
.custom_world_gallery_entry()
.iter()
.collect::<Vec<_>>();
entries.sort_by(|left, right| {
right
.published_at
.cmp(&left.published_at)
.then(right.updated_at.cmp(&left.updated_at))
});
Ok(entries
.into_iter()
.map(|entry| {
let recent_play_count_7d = recent_play_counts
.get(&entry.profile_id)
.copied()
.unwrap_or(0);
map_custom_world_gallery_entry_row(entry, recent_play_count_7d)
})
.collect())
})
.await
}
async fn sync_custom_world_gallery_entries_via_legacy_procedure(
&self,
) -> Result<(), SpacetimeClientError> {
self.call_after_connect(
"list_custom_world_gallery_entries",
move |connection, sender| {
@@ -188,8 +237,8 @@ impl SpacetimeClient {
.procedures()
.list_custom_world_gallery_entries_then(move |_, result| {
let mapped = result
.map_err(SpacetimeClientError::from_sdk_error)
.and_then(map_custom_world_gallery_list_result);
.map(|_| ())
.map_err(SpacetimeClientError::from_sdk_error);
send_once(&sender, mapped);
});
},