mirror of
https://github.com/denoland/deno.git
synced 2025-09-26 20:29:11 +00:00
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:
parent
98cf8f402b
commit
a33dae6a2a
3 changed files with 63 additions and 10 deletions
|
@ -1190,10 +1190,14 @@ static ENV_VARIABLES_HELP: &str = cstr!(
|
||||||
<g>DENO_AUTH_TOKENS</> A semi-colon separated list of bearer tokens and hostnames
|
<g>DENO_AUTH_TOKENS</> A semi-colon separated list of bearer tokens and hostnames
|
||||||
to use when fetching remote modules from private repositories
|
to use when fetching remote modules from private repositories
|
||||||
<p(245)>(e.g. "abcde12345@deno.land;54321edcba@github.com")</>
|
<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_CERT</> Load certificate authorities from PEM encoded file
|
||||||
<g>DENO_DIR</> Set the cache directory
|
<g>DENO_DIR</> Set the cache directory
|
||||||
<g>DENO_INSTALL_ROOT</> Set deno install's output directory
|
<g>DENO_INSTALL_ROOT</> Set deno install's output directory
|
||||||
<p(245)>(defaults to $HOME/.deno/bin)</>
|
<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_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_NO_UPDATE_CHECK</> Set to disable checking if a newer Deno version is available
|
||||||
<g>DENO_SERVE_ADDRESS</> Override address for Deno.serve
|
<g>DENO_SERVE_ADDRESS</> Override address for Deno.serve
|
||||||
|
|
26
ext/cache/sqlite.rs
vendored
26
ext/cache/sqlite.rs
vendored
|
@ -36,15 +36,37 @@ pub struct SqliteBackedCache {
|
||||||
pub cache_storage_dir: PathBuf,
|
pub cache_storage_dir: PathBuf,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
enum Mode {
|
||||||
|
Disk,
|
||||||
|
InMemory,
|
||||||
|
}
|
||||||
|
|
||||||
impl SqliteBackedCache {
|
impl SqliteBackedCache {
|
||||||
pub fn new(cache_storage_dir: PathBuf) -> Result<Self, CacheError> {
|
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| {
|
std::fs::create_dir_all(&cache_storage_dir).map_err(|source| {
|
||||||
CacheError::CacheStorageDirectory {
|
CacheError::CacheStorageDirectory {
|
||||||
dir: cache_storage_dir.clone(),
|
dir: cache_storage_dir.clone(),
|
||||||
source,
|
source,
|
||||||
}
|
}
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
let path = cache_storage_dir.join("cache_metadata.db");
|
let path = cache_storage_dir.join("cache_metadata.db");
|
||||||
let connection = rusqlite::Connection::open(&path).unwrap_or_else(|_| {
|
let connection = rusqlite::Connection::open(&path).unwrap_or_else(|_| {
|
||||||
panic!("failed to open cache db at {}", path.display())
|
panic!("failed to open cache db at {}", path.display())
|
||||||
|
@ -57,6 +79,9 @@ impl SqliteBackedCache {
|
||||||
PRAGMA optimize;
|
PRAGMA optimize;
|
||||||
";
|
";
|
||||||
connection.execute_batch(initial_pragmas)?;
|
connection.execute_batch(initial_pragmas)?;
|
||||||
|
connection
|
||||||
|
};
|
||||||
|
|
||||||
connection.execute(
|
connection.execute(
|
||||||
"CREATE TABLE IF NOT EXISTS cache_storage (
|
"CREATE TABLE IF NOT EXISTS cache_storage (
|
||||||
id INTEGER PRIMARY KEY,
|
id INTEGER PRIMARY KEY,
|
||||||
|
@ -88,7 +113,6 @@ impl SqliteBackedCache {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl SqliteBackedCache {
|
impl SqliteBackedCache {
|
||||||
/// Open a cache storage. Internally, this creates a row in the
|
/// Open a cache storage. Internally, this creates a row in the
|
||||||
|
|
|
@ -90,6 +90,12 @@ deno_error::js_error_wrapper!(
|
||||||
"TypeError"
|
"TypeError"
|
||||||
);
|
);
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
enum Mode {
|
||||||
|
Disk,
|
||||||
|
InMemory,
|
||||||
|
}
|
||||||
|
|
||||||
#[async_trait(?Send)]
|
#[async_trait(?Send)]
|
||||||
impl<P: SqliteDbHandlerPermissions> DatabaseHandler for SqliteDbHandler<P> {
|
impl<P: SqliteDbHandlerPermissions> DatabaseHandler for SqliteDbHandler<P> {
|
||||||
type DB = denokv_sqlite::Sqlite;
|
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>;
|
Arc<dyn Fn() -> rusqlite::Result<rusqlite::Connection> + Send + Sync>;
|
||||||
let (conn_gen, notifier_key): (ConnGen, _) = spawn_blocking(move || {
|
let (conn_gen, notifier_key): (ConnGen, _) = spawn_blocking(move || {
|
||||||
denokv_sqlite::sqlite_retry_loop(|| {
|
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)
|
let (conn, notifier_key) = match (path.as_deref(), &default_storage_dir)
|
||||||
{
|
{
|
||||||
(Some(":memory:"), _) | (None, None) => (
|
(Some(":memory:"), _) | (None, None) => (
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue