diff options
author | Satya Rohith <me@satyarohith.com> | 2023-04-12 11:25:19 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-12 11:25:19 +0530 |
commit | 18e0ee368c6a77b476f3fec7574415465f9052b9 (patch) | |
tree | 3870030ceae5ec139a3d7e0fea8a786a7b2fdc44 /ext/cache/sqlite.rs | |
parent | 65b0d2316d9c05c12eccd4bb7821598af3085ad0 (diff) |
fix(ext/cache): cache.put overwrites previous call (#18649)
Diffstat (limited to 'ext/cache/sqlite.rs')
-rw-r--r-- | ext/cache/sqlite.rs | 36 |
1 files changed, 14 insertions, 22 deletions
diff --git a/ext/cache/sqlite.rs b/ext/cache/sqlite.rs index 0252934e5..2853f793d 100644 --- a/ext/cache/sqlite.rs +++ b/ext/cache/sqlite.rs @@ -285,29 +285,11 @@ impl Cache for SqliteBackedCache { async fn insert_cache_asset( db: Arc<Mutex<rusqlite::Connection>>, put: CachePutRequest, - body_key_start_time: Option<(String, u64)>, + response_body_key: Option<String>, ) -> Result<Option<String>, deno_core::anyhow::Error> { tokio::task::spawn_blocking(move || { let maybe_response_body = { let db = db.lock(); - let mut response_body_key = None; - if let Some((body_key, start_time)) = body_key_start_time { - response_body_key = Some(body_key); - let last_inserted_at = db.query_row(" - SELECT last_inserted_at FROM request_response_list - WHERE cache_id = ?1 AND request_url = ?2", - (put.cache_id, &put.request_url), |row| { - let last_inserted_at: i64 = row.get(0)?; - Ok(last_inserted_at) - }).optional()?; - if let Some(last_inserted) = last_inserted_at { - // Some other worker has already inserted this resource into the cache. - // Note: okay to unwrap() as it is always present when response_body_key is present. - if start_time > (last_inserted as u64) { - return Ok(None); - } - } - } db.query_row( "INSERT OR REPLACE INTO request_response_list (cache_id, request_url, request_headers, response_headers, @@ -368,13 +350,23 @@ impl CachePutResource { let mut file = resource.borrow_mut().await; file.flush().await?; file.sync_all().await?; - insert_cache_asset( + let maybe_body_key = insert_cache_asset( self.db.clone(), self.put_request.clone(), - Some((self.response_body_key.clone(), self.start_time)), + Some(self.response_body_key.clone()), ) .await?; - Ok(()) + match maybe_body_key { + Some(key) => { + assert_eq!(key, self.response_body_key); + Ok(()) + } + // This should never happen because we will always have + // body key associated with CachePutResource + None => Err(deno_core::anyhow::anyhow!( + "unexpected: response body key is None" + )), + } } } |