-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR lays groundwork for implementing caching of `RegistryClient` instances. It just sets up all the types and refactors, no caching is done yet. There is also a small functional change: local registry client does not print `Unpacking` status in normal mode. This wasn't a big deal, and it allowed removing the `is_offline` method from `RegistryClient`. commit-id:afcc7328
- Loading branch information
Showing
7 changed files
with
235 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
use std::path::PathBuf; | ||
|
||
use anyhow::{bail, Result}; | ||
use tracing::trace; | ||
|
||
use crate::core::registry::client::{BeforeNetworkCallback, RegistryClient, RegistryResource}; | ||
use crate::core::registry::index::IndexRecords; | ||
use crate::core::{Config, ManifestDependency, PackageId}; | ||
|
||
pub struct RegistryClientCache<'c> { | ||
client: Box<dyn RegistryClient + 'c>, | ||
_config: &'c Config, | ||
} | ||
|
||
impl<'c> RegistryClientCache<'c> { | ||
pub fn new(client: Box<dyn RegistryClient + 'c>, config: &'c Config) -> Result<Self> { | ||
Ok(Self { | ||
client, | ||
_config: config, | ||
}) | ||
} | ||
|
||
/// Layer over [`RegistryClient::get_records`] that caches the result. | ||
/// | ||
/// It takes [`ManifestDependency`] instead of [`PackageName`] to allow performing some | ||
/// optimizations by pre-filtering index records on cache-level. | ||
#[tracing::instrument(level = "trace", skip_all)] | ||
pub async fn get_records_with_cache( | ||
&self, | ||
dependency: &ManifestDependency, | ||
before_network: BeforeNetworkCallback, | ||
) -> Result<IndexRecords> { | ||
match self | ||
.client | ||
.get_records(dependency.name.clone(), before_network) | ||
.await? | ||
{ | ||
RegistryResource::NotFound => { | ||
trace!("package not found in registry, pruning cache"); | ||
bail!("package not found in registry: {dependency}") | ||
} | ||
RegistryResource::InCache => { | ||
trace!("getting records from cache"); | ||
todo!() | ||
} | ||
RegistryResource::Download { resource, .. } => { | ||
trace!("got new records, invalidating cache"); | ||
Ok(resource) | ||
} | ||
} | ||
} | ||
|
||
/// Layer over [`RegistryClient::download`] that caches the result. | ||
#[tracing::instrument(level = "trace", skip_all)] | ||
pub async fn download_with_cache( | ||
&self, | ||
package: PackageId, | ||
before_network: BeforeNetworkCallback, | ||
) -> Result<PathBuf> { | ||
match self.client.download(package, before_network).await? { | ||
RegistryResource::NotFound => { | ||
trace!("archive not found in registry, pruning cache"); | ||
bail!("could not find downloadable archive for package indexed in registry: {package}") | ||
} | ||
RegistryResource::InCache => { | ||
trace!("using cached archive"); | ||
todo!() | ||
} | ||
RegistryResource::Download { resource, .. } => { | ||
trace!("got new archive, invalidating cache"); | ||
Ok(resource) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.