reuse redis connection
This commit is contained in:
parent
9fa252a6af
commit
879ab82cae
1 changed files with 11 additions and 2 deletions
|
|
@ -1,20 +1,29 @@
|
||||||
use redis::AsyncCommands;
|
use redis::AsyncCommands;
|
||||||
use sha2::{Digest, Sha256};
|
use sha2::{Digest, Sha256};
|
||||||
|
use tokio::sync::OnceCell;
|
||||||
|
|
||||||
/// Redis caching layer implementing the key patterns and TTLs
|
/// Redis caching layer implementing the key patterns and TTLs
|
||||||
/// defined in DATA_MODEL.md section 3.
|
/// defined in DATA_MODEL.md section 3.
|
||||||
pub struct CacheService {
|
pub struct CacheService {
|
||||||
client: redis::Client,
|
client: redis::Client,
|
||||||
|
conn: OnceCell<redis::aio::MultiplexedConnection>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CacheService {
|
impl CacheService {
|
||||||
pub fn new(redis_url: &str) -> Result<Self, redis::RedisError> {
|
pub fn new(redis_url: &str) -> Result<Self, redis::RedisError> {
|
||||||
let client = redis::Client::open(redis_url)?;
|
let client = redis::Client::open(redis_url)?;
|
||||||
Ok(Self { client })
|
Ok(Self {
|
||||||
|
client,
|
||||||
|
conn: OnceCell::new(),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn conn(&self) -> Result<redis::aio::MultiplexedConnection, redis::RedisError> {
|
async fn conn(&self) -> Result<redis::aio::MultiplexedConnection, redis::RedisError> {
|
||||||
self.client.get_multiplexed_async_connection().await
|
let conn = self
|
||||||
|
.conn
|
||||||
|
.get_or_try_init(|| self.client.get_multiplexed_async_connection())
|
||||||
|
.await?;
|
||||||
|
Ok(conn.clone())
|
||||||
}
|
}
|
||||||
|
|
||||||
// ── Tile cache ──────────────────────────────────────────────
|
// ── Tile cache ──────────────────────────────────────────────
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue