mirror of
https://github.com/denoland/deno.git
synced 2024-11-21 15:04:11 -05:00
fix(ext/cache): don't panic when creating cache (#26780)
This commit is contained in:
parent
bf82c6697a
commit
637b1d5508
2 changed files with 13 additions and 16 deletions
6
ext/cache/lib.rs
vendored
6
ext/cache/lib.rs
vendored
|
@ -33,7 +33,9 @@ pub enum CacheError {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct CreateCache<C: Cache + 'static>(pub Arc<dyn Fn() -> C>);
|
pub struct CreateCache<C: Cache + 'static>(
|
||||||
|
pub Arc<dyn Fn() -> Result<C, CacheError>>,
|
||||||
|
);
|
||||||
|
|
||||||
deno_core::extension!(deno_cache,
|
deno_core::extension!(deno_cache,
|
||||||
deps = [ deno_webidl, deno_web, deno_url, deno_fetch ],
|
deps = [ deno_webidl, deno_web, deno_url, deno_fetch ],
|
||||||
|
@ -231,7 +233,7 @@ where
|
||||||
if let Some(cache) = state.try_borrow::<CA>() {
|
if let Some(cache) = state.try_borrow::<CA>() {
|
||||||
Ok(cache.clone())
|
Ok(cache.clone())
|
||||||
} else if let Some(create_cache) = state.try_borrow::<CreateCache<CA>>() {
|
} else if let Some(create_cache) = state.try_borrow::<CreateCache<CA>>() {
|
||||||
let cache = create_cache.0();
|
let cache = create_cache.0()?;
|
||||||
state.put(cache);
|
state.put(cache);
|
||||||
Ok(state.borrow::<CA>().clone())
|
Ok(state.borrow::<CA>().clone())
|
||||||
} else {
|
} else {
|
||||||
|
|
19
ext/cache/sqlite.rs
vendored
19
ext/cache/sqlite.rs
vendored
|
@ -42,7 +42,7 @@ pub struct SqliteBackedCache {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SqliteBackedCache {
|
impl SqliteBackedCache {
|
||||||
pub fn new(cache_storage_dir: PathBuf) -> Self {
|
pub fn new(cache_storage_dir: PathBuf) -> Result<Self, CacheError> {
|
||||||
{
|
{
|
||||||
std::fs::create_dir_all(&cache_storage_dir)
|
std::fs::create_dir_all(&cache_storage_dir)
|
||||||
.expect("failed to create cache dir");
|
.expect("failed to create cache dir");
|
||||||
|
@ -57,18 +57,14 @@ impl SqliteBackedCache {
|
||||||
PRAGMA synchronous=NORMAL;
|
PRAGMA synchronous=NORMAL;
|
||||||
PRAGMA optimize;
|
PRAGMA optimize;
|
||||||
";
|
";
|
||||||
connection
|
connection.execute_batch(initial_pragmas)?;
|
||||||
.execute_batch(initial_pragmas)
|
connection.execute(
|
||||||
.expect("failed to execute pragmas");
|
|
||||||
connection
|
|
||||||
.execute(
|
|
||||||
"CREATE TABLE IF NOT EXISTS cache_storage (
|
"CREATE TABLE IF NOT EXISTS cache_storage (
|
||||||
id INTEGER PRIMARY KEY,
|
id INTEGER PRIMARY KEY,
|
||||||
cache_name TEXT NOT NULL UNIQUE
|
cache_name TEXT NOT NULL UNIQUE
|
||||||
)",
|
)",
|
||||||
(),
|
(),
|
||||||
)
|
)?;
|
||||||
.expect("failed to create cache_storage table");
|
|
||||||
connection
|
connection
|
||||||
.execute(
|
.execute(
|
||||||
"CREATE TABLE IF NOT EXISTS request_response_list (
|
"CREATE TABLE IF NOT EXISTS request_response_list (
|
||||||
|
@ -86,12 +82,11 @@ impl SqliteBackedCache {
|
||||||
UNIQUE (cache_id, request_url)
|
UNIQUE (cache_id, request_url)
|
||||||
)",
|
)",
|
||||||
(),
|
(),
|
||||||
)
|
)?;
|
||||||
.expect("failed to create request_response_list table");
|
Ok(SqliteBackedCache {
|
||||||
SqliteBackedCache {
|
|
||||||
connection: Arc::new(Mutex::new(connection)),
|
connection: Arc::new(Mutex::new(connection)),
|
||||||
cache_storage_dir,
|
cache_storage_dir,
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue