From 9971c23749993477989c1ab42ded51c06020e317 Mon Sep 17 00:00:00 2001 From: Bartosz Sypytkowski Date: Tue, 10 Dec 2024 10:06:48 +0100 Subject: [PATCH 1/2] fix: update redis cache when inserting collabs in batches --- .../src/collab/cache/collab_cache.rs | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/services/appflowy-collaborate/src/collab/cache/collab_cache.rs b/services/appflowy-collaborate/src/collab/cache/collab_cache.rs index eeeaf7d1..af1d6230 100644 --- a/services/appflowy-collaborate/src/collab/cache/collab_cache.rs +++ b/services/appflowy-collaborate/src/collab/cache/collab_cache.rs @@ -256,6 +256,35 @@ impl CollabCache { &self, records: Vec, ) -> Result<(), AppError> { - self.disk_cache.batch_insert_collab(records).await + let mem_cache_params: Vec<_> = records + .iter() + .map(|r| { + ( + r.params.object_id.clone(), + r.params.encoded_collab_v1.clone(), + cache_exp_secs_from_collab_type(&r.params.collab_type), + ) + }) + .collect(); + + self.disk_cache.batch_insert_collab(records).await?; + + let mem_cache = self.mem_cache.clone(); + tokio::spawn(async move { + let now = chrono::Utc::now().timestamp(); + for (oid, data, expire) in mem_cache_params { + if let Err(err) = mem_cache + .insert_encode_collab_data(&oid, &data, now, Some(expire)) + .await + { + error!( + "Failed to insert collab `{}` into memory cache: {}", + oid, err + ); + } + } + }); + + Ok(()) } } From c4514ca5bf864d9ac477449e56b2f64261ec0f8d Mon Sep 17 00:00:00 2001 From: Bartosz Sypytkowski Date: Tue, 10 Dec 2024 10:12:33 +0100 Subject: [PATCH 2/2] chore: add explanation --- .../appflowy-collaborate/src/collab/cache/collab_cache.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/services/appflowy-collaborate/src/collab/cache/collab_cache.rs b/services/appflowy-collaborate/src/collab/cache/collab_cache.rs index af1d6230..b90f9b7c 100644 --- a/services/appflowy-collaborate/src/collab/cache/collab_cache.rs +++ b/services/appflowy-collaborate/src/collab/cache/collab_cache.rs @@ -269,6 +269,12 @@ impl CollabCache { self.disk_cache.batch_insert_collab(records).await?; + // We'll update cache in the background. The reason is that Redis + // doesn't have a good way to do batch insert, so we'll do it one + // by one which may take time if there are many records. + // + // Most of the code doesn't rely on the cache being the only source + // of truth and accepts possibility that its update may fail. let mem_cache = self.mem_cache.clone(); tokio::spawn(async move { let now = chrono::Utc::now().timestamp();