pub struct CacheManager { /* private fields */ }Expand description
Local cache manager for gpacks
Implementations§
Source§impl CacheManager
impl CacheManager
Sourcepub fn new() -> Result<Self>
pub fn new() -> Result<Self>
Create a new cache manager
Uses the default cache directory (~/.cache/ggen/gpacks on Unix systems).
§Example
use ggen_core::cache::CacheManager;
let cache = CacheManager::new()?;
println!("Cache directory: {:?}", cache.cache_dir());Sourcepub fn with_dir(cache_dir: PathBuf) -> Result<Self>
pub fn with_dir(cache_dir: PathBuf) -> Result<Self>
Create a cache manager with custom directory (for testing)
§Example
use ggen_core::cache::CacheManager;
use std::path::PathBuf;
let cache = CacheManager::with_dir(PathBuf::from("/tmp/ggen-cache"))?;
println!("Cache directory: {:?}", cache.cache_dir());Sourcepub fn cache_dir(&self) -> &Path
pub fn cache_dir(&self) -> &Path
Get the cache directory path
§Example
use ggen_core::cache::CacheManager;
let cache = CacheManager::new()?;
let cache_path = cache.cache_dir();
println!("Cache is at: {:?}", cache_path);Sourcepub async fn ensure(&self, resolved_pack: &ResolvedPack) -> Result<CachedPack>
pub async fn ensure(&self, resolved_pack: &ResolvedPack) -> Result<CachedPack>
Ensure a pack is cached locally
Downloads the pack from its git repository if not already cached, or verifies the cached version matches the expected SHA256 checksum.
SHA256 verification: If resolved_pack.sha256 is provided and not empty,
the cached pack’s SHA256 is verified. If the checksum doesn’t match, the
corrupted cache is automatically removed and the pack is re-downloaded.
If no SHA256 is provided, the cached pack is used as-is without verification.
Automatic recovery: If a cached pack is found but its SHA256 doesn’t match the expected value, the method automatically removes the corrupted cache and re-downloads the pack. This ensures data integrity without manual intervention.
§Arguments
resolved_pack- Pack metadata including git URL, revision, and optional SHA256
§Returns
Returns information about the cached pack, including its local path.
§Errors
Returns an error if:
- The pack cannot be downloaded from git
- The SHA256 checksum doesn’t match after re-download (if provided)
- The cache directory cannot be accessed or created
- The corrupted cache cannot be removed (should not occur in normal use)
§Examples
§Success case
use ggen_core::cache::CacheManager;
use ggen_core::registry::ResolvedPack;
let cache = CacheManager::new()?;
let pack = ResolvedPack {
id: "io.ggen.example".to_string(),
version: "1.0.0".to_string(),
git_url: "https://github.com/example/pack.git".to_string(),
git_rev: "v1.0.0".to_string(),
sha256: "abc123...".to_string(),
};
let cached = cache.ensure(&pack).await?;
println!("Pack cached at: {:?}", cached.path);§Error case - Invalid git URL
use ggen_core::cache::CacheManager;
use ggen_core::registry::ResolvedPack;
let cache = CacheManager::new()?;
let pack = ResolvedPack {
id: "io.ggen.example".to_string(),
version: "1.0.0".to_string(),
git_url: "https://invalid-url-that-does-not-exist.git".to_string(),
git_rev: "v1.0.0".to_string(),
sha256: "".to_string(),
};
// This will fail because the git URL is invalid
let result = cache.ensure(&pack).await;
assert!(result.is_err());Sourcepub fn load_cached(&self, pack_id: &str, version: &str) -> Result<CachedPack>
pub fn load_cached(&self, pack_id: &str, version: &str) -> Result<CachedPack>
Load a cached pack
Returns information about a pack that is already cached locally.
§Errors
Returns an error if:
- The pack is not found in the cache
- The pack directory exists but is corrupted
- The manifest file cannot be read or parsed
§Examples
§Success case
use ggen_core::cache::CacheManager;
let cache = CacheManager::new()?;
// Assuming pack is already cached
let cached = cache.load_cached("io.ggen.example", "1.0.0")?;
println!("Pack path: {:?}", cached.path);
println!("Pack SHA256: {}", cached.sha256);§Error case - Pack not found
use ggen_core::cache::CacheManager;
let cache = CacheManager::new()?;
// This will fail because the pack is not cached
let result = cache.load_cached("nonexistent.pack", "1.0.0");
assert!(result.is_err());Sourcepub fn list_cached(&self) -> Result<Vec<CachedPack>>
pub fn list_cached(&self) -> Result<Vec<CachedPack>>
List all cached packs
§Example
use ggen_core::cache::CacheManager;
let cache = CacheManager::new()?;
let cached_packs = cache.list_cached()?;
for pack in cached_packs {
println!("{}@{}: {:?}", pack.id, pack.version, pack.path);
}Sourcepub fn remove(&self, pack_id: &str, version: &str) -> Result<()>
pub fn remove(&self, pack_id: &str, version: &str) -> Result<()>
Remove a cached pack
§Errors
Returns an error if:
- The pack directory cannot be removed
- The cache directory cannot be accessed
§Examples
§Success case
use ggen_core::cache::CacheManager;
let cache = CacheManager::new()?;
// Remove a specific version of a pack
cache.remove("io.ggen.example", "1.0.0")?;§Error case - Permission denied
use ggen_core::cache::CacheManager;
let cache = CacheManager::new()?;
// This may fail if we don't have permission to remove the pack
let result = cache.remove("io.ggen.example", "1.0.0");
// Handle error appropriately
if let Err(e) = result {
eprintln!("Failed to remove pack: {}", e);
}Sourcepub fn cleanup_old_versions(&self) -> Result<()>
pub fn cleanup_old_versions(&self) -> Result<()>
Clean up old versions, keeping only the latest
Trait Implementations§
Source§impl Clone for CacheManager
impl Clone for CacheManager
Source§fn clone(&self) -> CacheManager
fn clone(&self) -> CacheManager
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for CacheManager
impl RefUnwindSafe for CacheManager
impl Send for CacheManager
impl Sync for CacheManager
impl Unpin for CacheManager
impl UnwindSafe for CacheManager
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request