feat: allow forcing in-memory SQLite dbs (#29026)

This commit adds two env vars:
- "DENO_CACHE_DB_MODE"
- "DENO_KV_DB_MODE"

Both of these env vars accept either "disk" or "memory" values and
control the modes of backing databases for Web Cache API and
"Deno.openKv()" API.

By default both APIs use disk backed DBs, but they can be changed to use
in-memory
DB, making them effectively ephemeral.
This commit is contained in:
Bartek Iwańczuk 2025-04-28 11:06:38 +02:00 committed by GitHub
parent 98cf8f402b
commit a33dae6a2a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 63 additions and 10 deletions

44
ext/cache/sqlite.rs vendored
View file

@ -36,15 +36,37 @@ pub struct SqliteBackedCache {
pub cache_storage_dir: PathBuf,
}
#[derive(Debug)]
enum Mode {
Disk,
InMemory,
}
impl SqliteBackedCache {
pub fn new(cache_storage_dir: PathBuf) -> Result<Self, CacheError> {
let mode = match std::env::var("DENO_CACHE_DB_MODE")
.unwrap_or_default()
.as_str()
{
"disk" | "" => Mode::Disk,
"memory" => Mode::InMemory,
_ => {
log::warn!("Unknown DENO_CACHE_DB_MODE value, defaulting to disk");
Mode::Disk
}
};
let connection = if matches!(mode, Mode::InMemory) {
rusqlite::Connection::open_in_memory()
.unwrap_or_else(|_| panic!("failed to open in-memory cache db"))
} else {
std::fs::create_dir_all(&cache_storage_dir).map_err(|source| {
CacheError::CacheStorageDirectory {
dir: cache_storage_dir.clone(),
source,
}
})?;
let path = cache_storage_dir.join("cache_metadata.db");
let connection = rusqlite::Connection::open(&path).unwrap_or_else(|_| {
panic!("failed to open cache db at {}", path.display())
@ -57,14 +79,17 @@ impl SqliteBackedCache {
PRAGMA optimize;
";
connection.execute_batch(initial_pragmas)?;
connection.execute(
"CREATE TABLE IF NOT EXISTS cache_storage (
connection
};
connection.execute(
"CREATE TABLE IF NOT EXISTS cache_storage (
id INTEGER PRIMARY KEY,
cache_name TEXT NOT NULL UNIQUE
)",
(),
)?;
connection
(),
)?;
connection
.execute(
"CREATE TABLE IF NOT EXISTS request_response_list (
id INTEGER PRIMARY KEY,
@ -82,11 +107,10 @@ impl SqliteBackedCache {
)",
(),
)?;
Ok(SqliteBackedCache {
connection: Arc::new(Mutex::new(connection)),
cache_storage_dir,
})
}
Ok(SqliteBackedCache {
connection: Arc::new(Mutex::new(connection)),
cache_storage_dir,
})
}
}