Skip to content

Commit 6710fb0

Browse files
committed
Add type alias for caches
1 parent 8b26f96 commit 6710fb0

File tree

4 files changed

+29
-34
lines changed

4 files changed

+29
-34
lines changed

src/cache.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,22 @@ use std::time::Instant;
44

55
use rocket::tokio::sync::RwLock;
66
use rocket::tokio::task;
7+
use rocket::State;
78

89
const CACHE_TTL_SECS: u64 = 120;
910

11+
pub type Cache<T> = State<Arc<RwLock<T>>>;
12+
1013
#[async_trait]
11-
pub trait Cache: Send + Sync + Clone + 'static {
14+
pub trait Cached: Send + Sync + Clone + 'static {
1215
fn get_timestamp(&self) -> Instant;
1316
async fn fetch() -> Result<Self, Box<dyn Error + Send + Sync>>;
14-
async fn get(cache: &Arc<RwLock<Self>>) -> Self {
17+
async fn get(cache: &Cache<Self>) -> Self {
1518
let cached = cache.read().await.clone();
1619
let timestamp = cached.get_timestamp();
1720
if timestamp.elapsed().as_secs() > CACHE_TTL_SECS {
1821
// Update the cache in the background
19-
let cache: Arc<_> = cache.clone();
22+
let cache: Arc<_> = cache.inner().clone();
2023
task::spawn(async move {
2124
match Self::fetch().await {
2225
Ok(data) => *cache.write().await = data,

src/main.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ mod rust_version;
3131
mod teams;
3232

3333
use cache::Cache;
34+
use cache::Cached;
3435
use production::User;
3536
use rocket::tokio::sync::RwLock;
36-
use rocket::State;
3737
use rust_version::RustReleasePost;
3838
use rust_version::RustVersion;
3939
use teams::encode_zulip_stream;
@@ -190,17 +190,17 @@ fn robots_txt() -> Option<content::RawText<&'static str>> {
190190

191191
#[get("/")]
192192
async fn index(
193-
version_cache: &State<Arc<RwLock<RustVersion>>>,
194-
release_post_cache: &State<Arc<RwLock<RustReleasePost>>>,
193+
version_cache: &Cache<RustVersion>,
194+
release_post_cache: &Cache<RustReleasePost>,
195195
) -> Template {
196196
render_index(ENGLISH.into(), version_cache, release_post_cache).await
197197
}
198198

199199
#[get("/<locale>", rank = 3)]
200200
async fn index_locale(
201201
locale: SupportedLocale,
202-
version_cache: &State<Arc<RwLock<RustVersion>>>,
203-
release_post_cache: &State<Arc<RwLock<RustReleasePost>>>,
202+
version_cache: &Cache<RustVersion>,
203+
release_post_cache: &Cache<RustReleasePost>,
204204
) -> Template {
205205
render_index(locale.0, version_cache, release_post_cache).await
206206
}
@@ -216,23 +216,23 @@ fn category_locale(category: Category, locale: SupportedLocale) -> Template {
216216
}
217217

218218
#[get("/governance")]
219-
async fn governance(teams_cache: &State<Arc<RwLock<RustTeams>>>) -> Result<Template, Status> {
219+
async fn governance(teams_cache: &Cache<RustTeams>) -> Result<Template, Status> {
220220
render_governance(ENGLISH.into(), teams_cache).await
221221
}
222222

223223
#[get("/governance/<section>/<team>", rank = 2)]
224224
async fn team(
225225
section: String,
226226
team: String,
227-
teams_cache: &State<Arc<RwLock<RustTeams>>>,
227+
teams_cache: &Cache<RustTeams>,
228228
) -> Result<Template, Result<Redirect, Status>> {
229229
render_team(section, team, ENGLISH.into(), teams_cache).await
230230
}
231231

232232
#[get("/<locale>/governance", rank = 8)]
233233
async fn governance_locale(
234234
locale: SupportedLocale,
235-
teams_cache: &State<Arc<RwLock<RustTeams>>>,
235+
teams_cache: &Cache<RustTeams>,
236236
) -> Result<Template, Status> {
237237
render_governance(locale.0, teams_cache).await
238238
}
@@ -242,7 +242,7 @@ async fn team_locale(
242242
section: String,
243243
team: String,
244244
locale: SupportedLocale,
245-
teams_cache: &State<Arc<RwLock<RustTeams>>>,
245+
teams_cache: &Cache<RustTeams>,
246246
) -> Result<Template, Result<Redirect, Status>> {
247247
render_team(section, team, locale.0, teams_cache).await
248248
}
@@ -368,8 +368,8 @@ fn concat_app_js(files: Vec<&str>) -> String {
368368

369369
async fn render_index(
370370
lang: String,
371-
version_cache: &State<Arc<RwLock<RustVersion>>>,
372-
release_post_cache: &State<Arc<RwLock<RustReleasePost>>>,
371+
version_cache: &Cache<RustVersion>,
372+
release_post_cache: &Cache<RustReleasePost>,
373373
) -> Template {
374374
#[derive(Serialize)]
375375
struct IndexData {
@@ -414,7 +414,7 @@ fn render_production(lang: String) -> Template {
414414

415415
async fn render_governance(
416416
lang: String,
417-
teams_cache: &State<Arc<RwLock<RustTeams>>>,
417+
teams_cache: &Cache<RustTeams>,
418418
) -> Result<Template, Status> {
419419
match teams::index_data(teams_cache).await {
420420
Ok(data) => {
@@ -434,7 +434,7 @@ async fn render_team(
434434
section: String,
435435
team: String,
436436
lang: String,
437-
teams_cache: &State<Arc<RwLock<RustTeams>>>,
437+
teams_cache: &Cache<RustTeams>,
438438
) -> Result<Template, Result<Redirect, Status>> {
439439
match teams::page_data(&section, &team, teams_cache).await {
440440
Ok(data) => {

src/rust_version.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
use std::env;
22
use std::error::Error;
3-
use std::sync::Arc;
43
use std::time::Instant;
54

6-
use rocket::tokio::sync::RwLock;
7-
use rocket::State;
8-
9-
use crate::cache::Cache;
5+
use crate::cache::{Cache, Cached};
106

117
static MANIFEST_URL: &str = "https://static.rust-lang.org/dist/channel-rust-stable.toml";
128
static RELEASES_FEED_URL: &str = "https://blog.rust-lang.org/releases.json";
@@ -47,7 +43,7 @@ impl Default for RustVersion {
4743
}
4844

4945
#[async_trait]
50-
impl Cache for RustVersion {
46+
impl Cached for RustVersion {
5147
fn get_timestamp(&self) -> Instant {
5248
self.1
5349
}
@@ -73,7 +69,7 @@ impl Default for RustReleasePost {
7369
}
7470

7571
#[async_trait]
76-
impl Cache for RustReleasePost {
72+
impl Cached for RustReleasePost {
7773
fn get_timestamp(&self) -> Instant {
7874
self.1
7975
}
@@ -88,10 +84,10 @@ impl Cache for RustReleasePost {
8884
}
8985
}
9086

91-
pub async fn rust_version(version_cache: &State<Arc<RwLock<RustVersion>>>) -> String {
87+
pub async fn rust_version(version_cache: &Cache<RustVersion>) -> String {
9288
RustVersion::get(version_cache).await.0
9389
}
9490

95-
pub async fn rust_release_post(release_post_cache: &State<Arc<RwLock<RustReleasePost>>>) -> String {
91+
pub async fn rust_release_post(release_post_cache: &Cache<RustReleasePost>) -> String {
9692
RustReleasePost::get(release_post_cache).await.0
9793
}

src/teams.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
use handlebars::{Context, Handlebars, Helper, HelperResult, Output, RenderContext, RenderError};
22
use percent_encoding::{utf8_percent_encode, AsciiSet, NON_ALPHANUMERIC};
3-
use rocket::tokio::sync::RwLock;
43
use rust_team_data::v1::{Team, TeamKind, Teams, BASE_URL};
54
use std::cmp::Reverse;
65
use std::error::Error;
76
use std::fmt;
8-
use std::sync::Arc;
97
use std::time::Instant;
108

11-
use crate::cache::Cache;
9+
use crate::cache::{Cache, Cached};
1210

1311
#[derive(Default, Serialize)]
1412
pub struct IndexData {
@@ -40,9 +38,7 @@ struct Data {
4038
const ENCODING_SET: AsciiSet = NON_ALPHANUMERIC.remove(b'-').remove(b'_');
4139

4240
impl Data {
43-
async fn load(
44-
teams_cache: &Arc<RwLock<RustTeams>>,
45-
) -> Result<Self, Box<dyn Error + Send + Sync>> {
41+
async fn load(teams_cache: &Cache<RustTeams>) -> Result<Self, Box<dyn Error + Send + Sync>> {
4642
Ok(Data {
4743
teams: RustTeams::get(teams_cache)
4844
.await
@@ -166,15 +162,15 @@ pub fn encode_zulip_stream(
166162
}
167163

168164
pub async fn index_data(
169-
teams_cache: &Arc<RwLock<RustTeams>>,
165+
teams_cache: &Cache<RustTeams>,
170166
) -> Result<IndexData, Box<dyn Error + Send + Sync>> {
171167
Data::load(teams_cache).await?.index_data()
172168
}
173169

174170
pub async fn page_data(
175171
section: &str,
176172
team_name: &str,
177-
teams_cache: &Arc<RwLock<RustTeams>>,
173+
teams_cache: &Cache<RustTeams>,
178174
) -> Result<PageData, Box<dyn Error + Send + Sync>> {
179175
Data::load(teams_cache).await?.page_data(section, team_name)
180176
}
@@ -189,7 +185,7 @@ impl Default for RustTeams {
189185
}
190186

191187
#[async_trait]
192-
impl Cache for RustTeams {
188+
impl Cached for RustTeams {
193189
fn get_timestamp(&self) -> Instant {
194190
self.1
195191
}

0 commit comments

Comments
 (0)