From 5aaa6bf187d14dca96deb061db6a62c11da7e4d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Commaille?= Date: Sun, 2 Feb 2025 12:20:13 +0100 Subject: [PATCH] feat(base): Add automatic media cache cleanups to MediaService MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Kévin Commaille --- .../event_cache/store/media/media_service.rs | 228 ++++++++++++++++-- .../src/event_cache/store/memory_store.rs | 11 +- .../src/event_cache_store.rs | 3 +- 3 files changed, 222 insertions(+), 20 deletions(-) diff --git a/crates/matrix-sdk-base/src/event_cache/store/media/media_service.rs b/crates/matrix-sdk-base/src/event_cache/store/media/media_service.rs index 8bd183adb..508eb3246 100644 --- a/crates/matrix-sdk-base/src/event_cache/store/media/media_service.rs +++ b/crates/matrix-sdk-base/src/event_cache/store/media/media_service.rs @@ -15,9 +15,14 @@ use std::{fmt, sync::Arc}; use async_trait::async_trait; -use matrix_sdk_common::{locks::Mutex, AsyncTraitDeps}; +use matrix_sdk_common::{ + executor::{spawn, JoinHandle}, + locks::Mutex, + AsyncTraitDeps, SendOutsideWasm, SyncOutsideWasm, +}; use ruma::{time::SystemTime, MxcUri}; use tokio::sync::Mutex as AsyncMutex; +use tracing::error; use super::MediaRetentionPolicy; use crate::{event_cache::store::EventCacheStoreError, media::MediaRequestParameters}; @@ -41,6 +46,15 @@ struct MediaServiceInner { /// A mutex to ensure a single cleanup is running at a time. cleanup_guard: AsyncMutex<()>, + + /// The time of the last media cache cleanup. + last_media_cleanup_time: Mutex>, + + /// The [`JoinHandle`] for an automatic media cleanup task. + /// + /// Used to ensure that only one automatic cleanup is running at a time, and + /// to stop the cleanup when the [`MediaServiceInner`] is dropped. + automatic_media_cleanup_join_handle: Mutex>>, } impl MediaService { @@ -61,7 +75,7 @@ impl Default for MediaService { impl