Repository: denoland/deno
Pull request: feat: allow forcing in-memory SQLite dbs

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.

Changed files:
filename: cli/args/flags.rs
patch: @@ -1190,10 +1190,14 @@ static ENV_VARIABLES_HELP: &str = cstr!(
   <g>DENO_AUTH_TOKENS</>      A semi-colon separated list of bearer tokens and hostnames
                          to use when fetching remote modules from private repositories
                           <p(245)>(e.g. "abcde12345@deno.land;54321edcba@github.com")</>
+  <g>DENO_CACHE_DB_MODE</>     Controls whether Web cache should use disk based or in-memory database.
   <g>DENO_CERT</>              Load certificate authorities from PEM encoded file
   <g>DENO_DIR</>               Set the cache directory
   <g>DENO_INSTALL_ROOT</>      Set deno install's output directory
                           <p(245)>(defaults to $HOME/.deno/bin)</>
+  <g>DENO_KV_DB_MODE</>        Controls whether Deno.openKv() API should use disk based or in-memory
+                         database.
+  <g>DENO_EMIT_CACHE_MODE</>   Control is the transpiled sources should be cached.
   <g>DENO_NO_PACKAGE_JSON</>   Disables auto-resolution of package.json
   <g>DENO_NO_UPDATE_CHECK</>   Set to disable checking if a newer Deno version is available
   <g>DENO_SERVE_ADDRESS</>     Override address for Deno.serve

filename: ext/cache/sqlite.rs
patch: @@ -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,
+    })
   }
 }
 

filename: ext/kv/sqlite.rs
patch: @@ -90,6 +90,12 @@ deno_error::js_error_wrapper!(
   "TypeError"
 );
 
+#[derive(Debug)]
+enum Mode {
+  Disk,
+  InMemory,
+}
+
 #[async_trait(?Send)]
 impl<P: SqliteDbHandlerPermissions> DatabaseHandler for SqliteDbHandler<P> {
   type DB = denokv_sqlite::Sqlite;
@@ -137,6 +143,25 @@ impl<P: SqliteDbHandlerPermissions> DatabaseHandler for SqliteDbHandler<P> {
       Arc<dyn Fn() -> rusqlite::Result<rusqlite::Connection> + Send + Sync>;
     let (conn_gen, notifier_key): (ConnGen, _) = spawn_blocking(move || {
       denokv_sqlite::sqlite_retry_loop(|| {
+        let mode = match std::env::var("DENO_KV_DB_MODE")
+          .unwrap_or_default()
+          .as_str()
+        {
+          "disk" | "" => Mode::Disk,
+          "memory" => Mode::InMemory,
+          _ => {
+            log::warn!("Unknown DENO_KV_DB_MODE value, defaulting to disk");
+            Mode::Disk
+          }
+        };
+
+        if matches!(mode, Mode::InMemory) {
+          return Ok::<_, SqliteBackendError>((
+            Arc::new(rusqlite::Connection::open_in_memory) as ConnGen,
+            None,
+          ));
+        }
+
         let (conn, notifier_key) = match (path.as_deref(), &default_storage_dir)
         {
           (Some(":memory:"), _) | (None, None) => (