File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11//! This module defines an abstraction layer over the database.
22
33use std:: sync:: Arc ;
4+ #[ cfg( not( test) ) ]
5+ use std:: time:: Duration ;
46
57use anyhow:: Result ;
68use async_trait:: async_trait;
9+ #[ cfg( not( test) ) ]
10+ use cached:: proc_macro:: cached;
711use deadpool_postgres:: { Pool , Transaction } ;
812#[ cfg( test) ]
913use mockall:: automock;
10- use tokio_postgres:: types:: Json ;
14+ use tokio_postgres:: { Client , types:: Json } ;
1115use uuid:: Uuid ;
1216
1317use 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`]
You can’t perform that action at this time.
0 commit comments