pub struct BlobCache { /* private fields */ }Expand description
Stores immutable Dex blob payloads in a bounded local directory.
A cache is thread-safe. Reads and writes may run concurrently, while deletion and close
coordinate with in-flight operations. Payloads survive process restarts; BlobCache::open
recovers valid committed entries and removes interrupted writes.
§Examples
use dex_blob_cache::{BlobCache, BlobCacheConfig};
let config = BlobCacheConfig::new("/tmp/dex-blobs", 64 * 1024 * 1024, 0)?;
let cache = BlobCache::open(config)?;
assert!(cache.put("orders/123", b"payload")?);
assert_eq!(cache.get("orders/123")?, Some(b"payload".to_vec()));
cache.close()?;Implementations§
Source§impl BlobCache
impl BlobCache
Sourcepub fn open(config: BlobCacheConfig) -> Result<Self, BlobCacheError>
pub fn open(config: BlobCacheConfig) -> Result<Self, BlobCacheError>
Opens a cache and recovers its committed entries.
config supplies the owned directory, byte budget, and admission-policy sizing. The call
creates the directory when needed and returns only after recovery completes.
§Errors
Returns BlobCacheError when the directory cannot be prepared, recovery finds an
unrecoverable storage failure, or the admission policy cannot be initialized.
Sourcepub fn get(&self, blob_id: &str) -> Result<Option<Vec<u8>>, BlobCacheError>
pub fn get(&self, blob_id: &str) -> Result<Option<Vec<u8>>, BlobCacheError>
Reads one payload and records an admission-policy access.
Returns Ok(Some(payload)) for a valid entry and Ok(None) when the ID is absent or its
file disappeared or became corrupt. Corrupt entries are invalidated before returning.
§Errors
Returns BlobCacheError::InvalidBlob for an invalid ID, BlobCacheError::Closed after
close, or a storage/policy error that cannot be treated as a cache miss.
Sourcepub fn put(&self, blob_id: &str, payload: &[u8]) -> Result<bool, BlobCacheError>
pub fn put(&self, blob_id: &str, payload: &[u8]) -> Result<bool, BlobCacheError>
Attempts to admit an immutable payload under blob_id.
Returns Ok(true) when the payload is committed or the identical payload already exists.
Returns Ok(false) when the payload exceeds the byte budget or the policy rejects it.
Reusing an ID for different bytes is an error.
§Errors
Returns BlobCacheError for invalid IDs, content mismatches, closed lifecycle, or failed
filesystem and policy operations.
Sourcepub fn delete(&self, blob_id: &str) -> Result<(), BlobCacheError>
pub fn delete(&self, blob_id: &str) -> Result<(), BlobCacheError>
Deletes one blob if present.
Missing IDs succeed, making deletion idempotent.
§Errors
Returns BlobCacheError for an invalid ID, a closed cache, or a failed storage/policy
operation.
Sourcepub fn delete_all(&self) -> Result<(), BlobCacheError>
pub fn delete_all(&self) -> Result<(), BlobCacheError>
Removes every cache entry while keeping the cache open.
The call excludes concurrent cache operations until both policy and disk state are cleared.
§Errors
Returns BlobCacheError::Closed after close or a reconciliation/storage failure when the
cache cannot fully purge its state.
Sourcepub fn close(&self) -> Result<(), BlobCacheError>
pub fn close(&self) -> Result<(), BlobCacheError>
Releases policy resources and rejects future operations.
Close is idempotent and preserves committed files for the next BlobCache::open.
§Errors
Returns a cleanup error after closing when a previously deferred file removal still fails.