Skip to content

Commit f2ad11a

Browse files
committed
refactor(client): Create a common struct for client caches
1 parent 12c3272 commit f2ad11a

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2025 The Matrix.org Foundation C.I.C.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
use tokio::sync::RwLock;
16+
17+
use super::ClientServerCapabilities;
18+
19+
/// A collection of in-memory data that the `Client` might want to cache to
20+
/// avoid hitting the homeserver every time users request the data.
21+
pub(super) struct ClientCaches {
22+
/// Server capabilities, either prefilled during building or fetched from
23+
/// the server.
24+
pub(super) server_capabilities: RwLock<ClientServerCapabilities>,
25+
}

crates/matrix-sdk/src/client/mod.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use std::{
2222
sync::{Arc, Mutex as StdMutex, RwLock as StdRwLock, Weak},
2323
};
2424

25+
use caches::ClientCaches;
2526
use eyeball::{SharedObservable, Subscriber};
2627
use eyeball_im::{Vector, VectorDiff};
2728
use futures_core::Stream;
@@ -103,6 +104,7 @@ use crate::{
103104
};
104105

105106
mod builder;
107+
pub(crate) mod caches;
106108
pub(crate) mod futures;
107109

108110
pub use self::builder::{sanitize_server_name, ClientBuildError, ClientBuilder};
@@ -263,9 +265,8 @@ pub(crate) struct ClientInner {
263265
/// User session data.
264266
pub(super) base_client: BaseClient,
265267

266-
/// Server capabilities, either prefilled during building or fetched from
267-
/// the server.
268-
server_capabilities: RwLock<ClientServerCapabilities>,
268+
/// Collection of in-memory caches for the [`Client`].
269+
pub(crate) caches: ClientCaches,
269270

270271
/// Collection of locks individual client methods might want to use, either
271272
/// to ensure that only a single call to a method happens at once or to
@@ -351,16 +352,18 @@ impl ClientInner {
351352
#[cfg(feature = "e2e-encryption")] encryption_settings: EncryptionSettings,
352353
cross_process_store_locks_holder_name: String,
353354
) -> Arc<Self> {
355+
let caches = ClientCaches { server_capabilities: server_capabilities.into() };
356+
354357
let client = Self {
355358
server,
356359
homeserver: StdRwLock::new(homeserver),
357360
auth_ctx,
358361
sliding_sync_version: StdRwLock::new(sliding_sync_version),
359362
http_client,
360363
base_client,
364+
caches,
361365
locks: Default::default(),
362366
cross_process_store_locks_holder_name,
363-
server_capabilities: RwLock::new(server_capabilities),
364367
typing_notice_times: Default::default(),
365368
event_handlers: Default::default(),
366369
notification_handlers: Default::default(),
@@ -1730,7 +1733,7 @@ impl Client {
17301733
&self,
17311734
f: F,
17321735
) -> HttpResult<T> {
1733-
let caps = &self.inner.server_capabilities;
1736+
let caps = &self.inner.caches.server_capabilities;
17341737
if let Some(val) = f(&*caps.read().await) {
17351738
return Ok(val);
17361739
}
@@ -1799,7 +1802,7 @@ impl Client {
17991802
/// functions makes it possible to force reset it.
18001803
pub async fn reset_server_capabilities(&self) -> Result<()> {
18011804
// Empty the in-memory caches.
1802-
let mut guard = self.inner.server_capabilities.write().await;
1805+
let mut guard = self.inner.caches.server_capabilities.write().await;
18031806
guard.server_versions = None;
18041807
guard.unstable_features = None;
18051808

@@ -2421,7 +2424,7 @@ impl Client {
24212424
.base_client
24222425
.clone_with_in_memory_state_store(&cross_process_store_locks_holder_name)
24232426
.await?,
2424-
self.inner.server_capabilities.read().await.clone(),
2427+
self.inner.caches.server_capabilities.read().await.clone(),
24252428
self.inner.respect_login_well_known,
24262429
self.inner.event_cache.clone(),
24272430
self.inner.send_queue_data.clone(),

0 commit comments

Comments
 (0)