use crate::cache::{BlobCache, BlobCacheBackend};
use crate::error::CacheError;
use std::sync::Arc;
#[cfg(feature = "persistent")]
use std::path::PathBuf;
#[cfg(feature = "persistent")]
use crate::persistent_cache::PersistentBlobCache;
#[cfg(feature = "s3")]
use crate::s3_cache::{S3BlobCache, S3CacheConfig};
#[cfg(feature = "persistent")]
const DEFAULT_CACHE_DB: &str = "blobs.sqlite";
#[derive(Debug, Clone)]
pub enum CacheType {
Memory,
#[cfg(feature = "persistent")]
Persistent {
path: PathBuf,
},
#[cfg(feature = "s3")]
S3(S3CacheConfig),
}
#[allow(clippy::derivable_impls)]
impl Default for CacheType {
fn default() -> Self {
#[cfg(feature = "persistent")]
{
CacheType::Persistent {
path: zlayer_paths::ZLayerDirs::system_default()
.cache()
.join(DEFAULT_CACHE_DB),
}
}
#[cfg(not(feature = "persistent"))]
{
CacheType::Memory
}
}
}
impl CacheType {
pub fn from_env() -> Result<Self, CacheError> {
let cache_type = std::env::var("ZLAYER_CACHE_TYPE")
.unwrap_or_else(|_| "persistent".to_string())
.to_lowercase();
match cache_type.as_str() {
"memory" => Ok(CacheType::Memory),
"persistent" => {
#[cfg(feature = "persistent")]
{
let default_cache_dir = zlayer_paths::ZLayerDirs::system_default()
.cache()
.to_string_lossy()
.into_owned();
let cache_dir = std::env::var("ZLAYER_CACHE_DIR").unwrap_or(default_cache_dir);
let path = PathBuf::from(cache_dir).join(DEFAULT_CACHE_DB);
Ok(CacheType::Persistent { path })
}
#[cfg(not(feature = "persistent"))]
{
Err(CacheError::Database(
"persistent cache requires the 'persistent' feature".to_string(),
))
}
}
"s3" => {
#[cfg(feature = "s3")]
{
let bucket = std::env::var("ZLAYER_S3_BUCKET").map_err(|_| {
CacheError::Database(
"ZLAYER_S3_BUCKET environment variable is required for S3 cache"
.to_string(),
)
})?;
let mut config = S3CacheConfig::new(bucket);
if let Ok(region) = std::env::var("ZLAYER_S3_REGION") {
config = config.with_region(region);
}
if let Ok(endpoint) = std::env::var("ZLAYER_S3_ENDPOINT") {
config = config.with_endpoint(endpoint);
}
if let Ok(prefix) = std::env::var("ZLAYER_S3_PREFIX") {
config = config.with_prefix(prefix);
}
if let Ok(path_style) = std::env::var("ZLAYER_S3_PATH_STYLE") {
if path_style.to_lowercase() == "true" || path_style == "1" {
config = config.with_path_style();
}
}
Ok(CacheType::S3(config))
}
#[cfg(not(feature = "s3"))]
{
Err(CacheError::Database(
"S3 cache requires the 's3' feature".to_string(),
))
}
}
unknown => Err(CacheError::Database(format!(
"unknown cache type '{unknown}', expected 'memory', 'persistent', or 's3'"
))),
}
}
#[allow(clippy::unused_async)]
pub async fn build(&self) -> Result<Arc<Box<dyn BlobCacheBackend>>, CacheError> {
match self {
CacheType::Memory => {
let cache = BlobCache::new()?;
Ok(Arc::new(Box::new(cache) as Box<dyn BlobCacheBackend>))
}
#[cfg(feature = "persistent")]
CacheType::Persistent { path } => {
let cache = PersistentBlobCache::open(path).await?;
Ok(Arc::new(Box::new(cache) as Box<dyn BlobCacheBackend>))
}
#[cfg(feature = "s3")]
CacheType::S3(config) => {
let cache = S3BlobCache::new(config.clone()).await?;
Ok(Arc::new(Box::new(cache) as Box<dyn BlobCacheBackend>))
}
}
}
#[must_use]
pub fn memory() -> Self {
CacheType::Memory
}
#[cfg(feature = "persistent")]
#[must_use]
pub fn persistent() -> Self {
CacheType::Persistent {
path: zlayer_paths::ZLayerDirs::system_default()
.cache()
.join(DEFAULT_CACHE_DB),
}
}
#[cfg(feature = "persistent")]
pub fn persistent_at(path: impl Into<PathBuf>) -> Self {
CacheType::Persistent { path: path.into() }
}
#[cfg(feature = "s3")]
#[must_use]
pub fn s3(config: S3CacheConfig) -> Self {
CacheType::S3(config)
}
#[must_use]
pub fn is_memory(&self) -> bool {
matches!(self, CacheType::Memory)
}
#[cfg(feature = "persistent")]
#[must_use]
pub fn is_persistent(&self) -> bool {
matches!(self, CacheType::Persistent { .. })
}
#[cfg(feature = "s3")]
#[must_use]
pub fn is_s3(&self) -> bool {
matches!(self, CacheType::S3(_))
}
}
#[cfg(feature = "s3")]
pub async fn s3_cache_from_env() -> Result<Option<Arc<Box<dyn BlobCacheBackend>>>, CacheError> {
let Ok(bucket) = std::env::var("ZLAYER_S3_BUCKET") else {
return Ok(None);
};
if bucket.trim().is_empty() {
return Ok(None);
}
let mut config = S3CacheConfig::new(bucket);
if let Ok(region) = std::env::var("ZLAYER_S3_REGION") {
config = config.with_region(region);
}
if let Ok(endpoint) = std::env::var("ZLAYER_S3_ENDPOINT") {
config = config.with_endpoint(endpoint);
}
if let Ok(prefix) = std::env::var("ZLAYER_S3_PREFIX") {
config = config.with_prefix(prefix);
}
if let Ok(path_style) = std::env::var("ZLAYER_S3_PATH_STYLE") {
if path_style.to_lowercase() == "true" || path_style == "1" {
config = config.with_path_style();
}
}
let cache = S3BlobCache::new(config).await?;
Ok(Some(Arc::new(Box::new(cache) as Box<dyn BlobCacheBackend>)))
}
#[cfg(not(feature = "s3"))]
#[allow(clippy::unused_async)]
pub async fn s3_cache_from_env() -> Result<Option<Arc<Box<dyn BlobCacheBackend>>>, CacheError> {
Ok(None)
}
#[must_use]
pub fn default_registry_from_env() -> Option<String> {
std::env::var("ZLAYER_DEFAULT_REGISTRY")
.ok()
.map(|s| s.trim().to_string())
.filter(|s| !s.is_empty())
}
#[cfg(test)]
mod tests {
use super::*;
use serial_test::serial;
use std::env;
#[cfg(feature = "persistent")]
use zlayer_paths::ZLayerDirs;
const ENV_CACHE_TYPE: &str = "ZLAYER_CACHE_TYPE";
const ENV_CACHE_DIR: &str = "ZLAYER_CACHE_DIR";
const ENV_S3_BUCKET: &str = "ZLAYER_S3_BUCKET";
const ENV_S3_REGION: &str = "ZLAYER_S3_REGION";
const ENV_S3_ENDPOINT: &str = "ZLAYER_S3_ENDPOINT";
const ENV_S3_PREFIX: &str = "ZLAYER_S3_PREFIX";
const ENV_S3_PATH_STYLE: &str = "ZLAYER_S3_PATH_STYLE";
struct EnvGuard;
impl EnvGuard {
fn new() -> Self {
Self::clear_all();
Self
}
fn clear_all() {
env::remove_var(ENV_CACHE_TYPE);
env::remove_var(ENV_CACHE_DIR);
env::remove_var(ENV_S3_BUCKET);
env::remove_var(ENV_S3_REGION);
env::remove_var(ENV_S3_ENDPOINT);
env::remove_var(ENV_S3_PREFIX);
env::remove_var(ENV_S3_PATH_STYLE);
}
}
impl Drop for EnvGuard {
fn drop(&mut self) {
Self::clear_all();
}
}
#[test]
fn test_default_cache_type() {
let cache = CacheType::default();
#[cfg(feature = "persistent")]
assert!(cache.is_persistent());
#[cfg(not(feature = "persistent"))]
assert!(cache.is_memory());
}
#[test]
fn test_memory_cache() {
let cache = CacheType::memory();
assert!(cache.is_memory());
}
#[cfg(feature = "persistent")]
#[test]
fn test_persistent_cache() {
let cache = CacheType::persistent();
assert!(cache.is_persistent());
let custom = CacheType::persistent_at("/tmp/test.sqlite");
if let CacheType::Persistent { path } = custom {
assert_eq!(path, PathBuf::from("/tmp/test.sqlite"));
} else {
panic!("expected Persistent variant");
}
}
#[cfg(feature = "s3")]
#[test]
fn test_s3_cache() {
let config = S3CacheConfig::new("test-bucket");
let cache = CacheType::s3(config);
assert!(cache.is_s3());
}
#[test]
#[serial]
fn test_from_env_memory() {
let _guard = EnvGuard::new();
env::set_var(ENV_CACHE_TYPE, "memory");
let cache = CacheType::from_env().unwrap();
assert!(cache.is_memory());
}
#[test]
#[serial]
fn test_from_env_unknown_type() {
let _guard = EnvGuard::new();
env::set_var(ENV_CACHE_TYPE, "invalid");
let result = CacheType::from_env();
assert!(result.is_err());
}
#[cfg(feature = "s3")]
#[test]
#[serial]
fn test_from_env_s3_missing_bucket() {
let _guard = EnvGuard::new();
env::set_var(ENV_CACHE_TYPE, "s3");
let result = CacheType::from_env();
assert!(result.is_err());
}
#[cfg(feature = "s3")]
#[test]
#[serial]
fn test_from_env_s3_with_all_options() {
let _guard = EnvGuard::new();
env::set_var(ENV_CACHE_TYPE, "s3");
env::set_var(ENV_S3_BUCKET, "my-bucket");
env::set_var(ENV_S3_REGION, "us-west-2");
env::set_var(ENV_S3_ENDPOINT, "https://s3.example.com");
env::set_var(ENV_S3_PREFIX, "custom/prefix/");
env::set_var(ENV_S3_PATH_STYLE, "true");
let cache = CacheType::from_env().unwrap();
if let CacheType::S3(config) = cache {
assert_eq!(config.bucket, "my-bucket");
assert_eq!(config.region, Some("us-west-2".to_string()));
assert_eq!(config.endpoint, Some("https://s3.example.com".to_string()));
assert_eq!(config.prefix, "custom/prefix/");
assert!(config.path_style);
} else {
panic!("expected S3 variant");
}
}
#[tokio::test]
async fn test_build_memory_cache() {
let cache_type = CacheType::memory();
let cache = cache_type.build().await.unwrap();
let test_data = b"test data for cache";
let digest = crate::cache::compute_digest(test_data);
cache.put(&digest, test_data).await.unwrap();
let retrieved = cache.get(&digest).await.unwrap();
assert_eq!(retrieved, Some(test_data.to_vec()));
}
#[cfg(feature = "persistent")]
#[tokio::test]
async fn test_build_persistent_cache() {
let temp_dir = ZLayerDirs::system_default()
.scratch_dir("test-build-persistent-cache-")
.unwrap();
let cache_path = temp_dir.path().join("test.sqlite");
let cache_type = CacheType::persistent_at(&cache_path);
let cache = cache_type.build().await.unwrap();
let test_data = b"persistent test data";
let digest = crate::cache::compute_digest(test_data);
cache.put(&digest, test_data).await.unwrap();
let retrieved = cache.get(&digest).await.unwrap();
assert_eq!(retrieved, Some(test_data.to_vec()));
}
}