Skip to content

Commit 83d6372

Browse files
authored
Cache votes list fetched for audit page (#657)
Signed-off-by: Sergio Castaño Arteaga <tegioz@icloud.com>
1 parent e504b54 commit 83d6372

1 file changed

Lines changed: 34 additions & 16 deletions

File tree

src/db.rs

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
//! This module defines an abstraction layer over the database.
22
33
use std::sync::Arc;
4+
#[cfg(not(test))]
5+
use std::time::Duration;
46

57
use anyhow::Result;
68
use async_trait::async_trait;
9+
#[cfg(not(test))]
10+
use cached::proc_macro::cached;
711
use deadpool_postgres::{Pool, Transaction};
812
#[cfg(test)]
913
use mockall::automock;
10-
use tokio_postgres::types::Json;
14+
use tokio_postgres::{Client, types::Json};
1115
use uuid::Uuid;
1216

1317
use crate::{
@@ -285,22 +289,36 @@ impl DB for PgDB {
285289

286290
/// [`DB::list_votes`]
287291
async fn list_votes(&self, repository_full_name: &str) -> Result<Vec<Vote>> {
288-
let db = self.pool.get().await?;
289-
let votes = db
290-
.query(
291-
"
292-
select *
293-
from vote
294-
where repository_full_name = $1::text
295-
order by created_at desc
296-
",
297-
&[&repository_full_name],
292+
#[cfg_attr(
293+
not(test),
294+
cached(
295+
time = 900,
296+
key = "String",
297+
convert = r#"{ repository_full_name.to_owned() }"#,
298+
sync_writes = "by_key",
299+
result = true
298300
)
299-
.await?
300-
.iter()
301-
.map(Vote::from)
302-
.collect();
303-
Ok(votes)
301+
)]
302+
async fn inner(client: &Client, repository_full_name: &str) -> Result<Vec<Vote>> {
303+
let votes = client
304+
.query(
305+
"
306+
select *
307+
from vote
308+
where repository_full_name = $1::text
309+
order by created_at desc
310+
",
311+
&[&repository_full_name],
312+
)
313+
.await?
314+
.iter()
315+
.map(Vote::from)
316+
.collect();
317+
Ok(votes)
318+
}
319+
320+
let client = self.pool.get().await?;
321+
inner(client.as_ref(), repository_full_name).await
304322
}
305323

306324
/// [`DB::store_vote`]

0 commit comments

Comments
 (0)