Files
GeneIT/geneit_backend/src/connections/redis_connection.rs
Pierre Hubert b1398fee2d
All checks were successful
continuous-integration/drone/push Build is passing
Update dependency filesize to v10.1.1 (#116)
This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [filesize](https://filesizejs.com) ([source](https://github.com/avoidwork/filesize.js)) | dependencies | patch | [`10.1.0` -> `10.1.1`](https://renovatebot.com/diffs/npm/filesize/10.1.0/10.1.1) |

---

### Release Notes

<details>
<summary>avoidwork/filesize.js (filesize)</summary>

### [`v10.1.1`](https://github.com/avoidwork/filesize.js/blob/HEAD/CHANGELOG.md#1011)

[Compare Source](https://github.com/avoidwork/filesize.js/compare/10.1.0...10.1.1)

-   Rework types to allow Parameters\<typeof filesize> to function properly [`#180`](https://github.com/avoidwork/filesize.js/pull/180)
-   Bump [@&#8203;babel/traverse](https://github.com/babel/traverse) from 7.23.0 to 7.23.2 [`#178`](https://github.com/avoidwork/filesize.js/pull/178)
-   change: rework types to allow usages like Parameters\<typeof filesize> [`fbfc87f`](fbfc87f4d2)
-   Generating CHANGELOG.md [`d80c457`](d80c4579e1)
-   fix: types for partial now return a higher order function [`af4a6ef`](af4a6efe8e)

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4yNjUuMCIsInVwZGF0ZWRJblZlciI6IjM3LjI2NS4wIiwidGFyZ2V0QnJhbmNoIjoibWFzdGVyIn0=-->

Reviewed-on: #116
2024-03-22 10:38:13 +00:00

59 lines
1.5 KiB
Rust

//! # Redis connection management
use crate::app_config::AppConfig;
use redis::Commands;
use serde::de::DeserializeOwned;
use std::cell::RefCell;
use std::time::Duration;
thread_local! {
static REDIS_CONNECTION: RefCell<Option<redis::Client>> = const { RefCell::new(None) };
}
/// Execute a request on Redis
fn execute_request<E, I>(cb: E) -> anyhow::Result<I>
where
E: FnOnce(&mut redis::Client) -> anyhow::Result<I>,
{
// Establish connection if required
if REDIS_CONNECTION.with(|i| i.borrow().is_none()) {
let conn = redis::Client::open(AppConfig::get().redis_connection_config())?;
REDIS_CONNECTION.with(|i| *i.borrow_mut() = Some(conn))
}
REDIS_CONNECTION.with(|i| cb(i.borrow_mut().as_mut().unwrap()))
}
/// Get a value stored on Redis
pub async fn get_value<E>(key: &str) -> anyhow::Result<Option<E>>
where
E: DeserializeOwned,
{
let value: Option<String> = execute_request(|conn| Ok(conn.get(key)?))?;
Ok(match value {
None => None,
Some(v) => serde_json::from_str(&v)?,
})
}
/// Set a new value on Redis
pub async fn set_value<E>(key: &str, value: &E, lifetime: Duration) -> anyhow::Result<()>
where
E: serde::Serialize,
{
let value_str = serde_json::to_string(value)?;
execute_request(|conn| Ok(conn.set_ex(key, value_str, lifetime.as_secs())?))?;
Ok(())
}
/// Remove a value from Redis
pub async fn remove_value(key: &str) -> anyhow::Result<()> {
execute_request(|conn| Ok(conn.del(key)?))?;
Ok(())
}