From 06dae11e3528eb6acbc5d8528552941a89ff465f Mon Sep 17 00:00:00 2001 From: Christian Llontop Date: Wed, 17 Apr 2024 21:48:39 -0500 Subject: [PATCH 1/2] Update keyv package version to 0.2.0 --- Cargo.lock | 2 +- Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a9dd37d..789b09d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -780,7 +780,7 @@ dependencies = [ [[package]] name = "keyv" -version = "0.1.0" +version = "0.2.0" dependencies = [ "async-trait", "log", diff --git a/Cargo.toml b/Cargo.toml index 4d6a9dd..950d105 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "keyv" -version = "0.1.0" +version = "0.2.0" authors = ["Christian Llontop "] edition = "2021" description = "Simple key-value storage with support for multiple backends" From 7ab94d74d454fb7a563cf8d5267ef9e654d11ec7 Mon Sep 17 00:00:00 2001 From: Christian Llontop Date: Wed, 17 Apr 2024 21:55:35 -0500 Subject: [PATCH 2/2] Refactor database adapter methods Standardized handling of database results in MySQL and Postgres adapters by using `and_then` instead of a combination of `map` and `flatten`. Shifted position of `clear` method in InMemory adapter for consistency. Also, removed unnecessary reference in Postgres adapter's delete query binding. --- src/store/adapter/inmemory/inmemory.rs | 12 ++++++------ src/store/adapter/mysql/mysql.rs | 4 +--- src/store/adapter/postgres/postgres.rs | 7 +++---- 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/src/store/adapter/inmemory/inmemory.rs b/src/store/adapter/inmemory/inmemory.rs index ec448ce..5afeb18 100644 --- a/src/store/adapter/inmemory/inmemory.rs +++ b/src/store/adapter/inmemory/inmemory.rs @@ -41,12 +41,6 @@ impl Store for InMemoryStore { Ok(()) } - async fn clear(&self) -> Result<(), StoreError> { - let mut db_lock = self.db.lock().await; - db_lock.clear(); - Ok(()) - } - async fn remove_many(&self, keys: &[&str]) -> Result<(), StoreError> { let mut db_lock = self.db.lock().await; for key in keys { @@ -54,4 +48,10 @@ impl Store for InMemoryStore { } Ok(()) } + + async fn clear(&self) -> Result<(), StoreError> { + let mut db_lock = self.db.lock().await; + db_lock.clear(); + Ok(()) + } } diff --git a/src/store/adapter/mysql/mysql.rs b/src/store/adapter/mysql/mysql.rs index d1a2629..ecfe6a4 100644 --- a/src/store/adapter/mysql/mysql.rs +++ b/src/store/adapter/mysql/mysql.rs @@ -90,9 +90,7 @@ impl Store for MySqlStore { .await .map_err(|_| StoreError::QueryError("Failed to fetch the value".to_string()))?; - Ok(result - .map(|row| serde_json::from_str(row.get("value")).ok()) - .flatten()) + Ok(result.and_then(|row| serde_json::from_str(row.get("value")).ok())) } async fn set(&self, key: &str, value: Value, ttl: Option) -> Result<(), StoreError> { diff --git a/src/store/adapter/postgres/postgres.rs b/src/store/adapter/postgres/postgres.rs index fe73265..a6fe3b2 100644 --- a/src/store/adapter/postgres/postgres.rs +++ b/src/store/adapter/postgres/postgres.rs @@ -11,6 +11,7 @@ pub struct PostgresStore { pub(crate) table_name: String, pub(crate) schema: Option, } + impl PostgresStore { fn get_table_name(&self) -> String { match &self.schema { @@ -63,9 +64,7 @@ impl Store for PostgresStore { .await .map_err(|_| StoreError::QueryError("Failed to fetch the value".to_string()))?; - Ok(result - .map(|row| serde_json::from_str(row.get("value")).ok()) - .flatten()) + Ok(result.and_then(|row| serde_json::from_str(row.get("value")).ok())) } async fn set(&self, key: &str, value: Value, ttl: Option) -> Result<(), StoreError> { @@ -105,7 +104,7 @@ impl Store for PostgresStore { let query = format!("DELETE FROM {} WHERE key = ANY($1)", self.get_table_name()); sqlx::query(&query) - .bind(&keys) + .bind(keys) .execute(&*self.pool) .await .map_err(|_| StoreError::QueryError("Failed to remove the keys".to_string()))?;