Skip to content

Commit f910df9

Browse files
committed
refactor: cache the pre-retrieved result
1 parent 80c090b commit f910df9

2 files changed

Lines changed: 19 additions & 11 deletions

File tree

engine-base/src/executor.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ impl Executor {
6767
let future = async move {
6868
info!("Searching {song} with engine {engine_id}…");
6969

70-
let result = engine
70+
let mut result = engine
7171
.search(song, ctx)
7272
.await
7373
.map_err(ExecutorError::EngineSearchError)?
@@ -76,11 +76,12 @@ impl Executor {
7676
})?;
7777

7878
// Try to retrieve to check if the source available to retrieve.
79-
// FIXME: cache it
80-
engine
81-
.retrieve(&result.identifier, ctx)
82-
.await
83-
.map_err(ExecutorError::EngineRetrieveError)?;
79+
result.pre_retrieve_result = Some(
80+
engine
81+
.retrieve(&result.identifier, ctx)
82+
.await
83+
.map_err(ExecutorError::EngineRetrieveError)?,
84+
);
8485

8586
// Specify the Error type explicitly.
8687
Ok::<SongSearchInformation, ExecutorError>(result)
@@ -130,11 +131,15 @@ impl Executor {
130131
) -> ExecutorResult<RetrievedSongInfo> {
131132
info!("Retrieving song from {}…", song.source);
132133

133-
let engine = self.resolve_engine(&song.source)?;
134-
engine
135-
.retrieve(&song.identifier, ctx)
136-
.await
137-
.map_err(ExecutorError::EngineRetrieveError)
134+
if let Some(ref retrieved) = song.pre_retrieve_result {
135+
Ok(retrieved.clone())
136+
} else {
137+
let engine = self.resolve_engine(&song.source)?;
138+
engine
139+
.retrieve(&song.identifier, ctx)
140+
.await
141+
.map_err(ExecutorError::EngineRetrieveError)
142+
}
138143
}
139144

140145
/// Validate engines to check if the engines specified are all registered.

types/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ pub struct SongSearchInformation {
9393
/// The details of this song.
9494
#[builder(default)]
9595
pub song: Option<Song>,
96+
/// The pre-retrieve result of this search.
97+
#[builder(default)]
98+
pub pre_retrieve_result: Option<RetrievedSongInfo>,
9699
}
97100

98101
/// The information of the song retrieved with `retrieve()`.

0 commit comments

Comments
 (0)