Skip to content

Commit 83b7453

Browse files
committed
perf(utils): cache sfw_only, enable_rss and disable_indexing in LazyLock
1 parent 430d7bc commit 83b7453

1 file changed

Lines changed: 7 additions & 12 deletions

File tree

src/utils.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,6 +1323,10 @@ pub async fn info(req: Request<Body>, msg: &str) -> Result<Response<Body>, Strin
13231323
Ok(Response::builder().status(200).header("content-type", "text/html").body(full(body)).unwrap_or_default())
13241324
}
13251325

1326+
static SFW_ONLY: LazyLock<bool> = LazyLock::new(|| matches!(get_setting("REDLIB_SFW_ONLY").as_deref(), Some("on")));
1327+
static ENABLE_RSS: LazyLock<bool> = LazyLock::new(|| matches!(get_setting("REDLIB_ENABLE_RSS").as_deref(), Some("on")));
1328+
static DISABLE_INDEXING: LazyLock<bool> = LazyLock::new(|| matches!(get_setting("REDLIB_ROBOTS_DISABLE_INDEXING").as_deref(), Some("on")));
1329+
13261330
/// Returns true if the config/env variable `REDLIB_SFW_ONLY` carries the
13271331
/// value `on`.
13281332
///
@@ -1331,20 +1335,14 @@ pub async fn info(req: Request<Body>, msg: &str) -> Result<Response<Body>, Strin
13311335
/// subreddits or posts or userpages for users Reddit has deemed NSFW will
13321336
/// be denied.
13331337
pub fn sfw_only() -> bool {
1334-
match get_setting("REDLIB_SFW_ONLY") {
1335-
Some(val) => val == "on",
1336-
None => false,
1337-
}
1338+
*SFW_ONLY
13381339
}
13391340

13401341
/// Returns true if the config/env variable REDLIB_ENABLE_RSS is set to "on".
13411342
/// If this variable is set as such, the instance will enable RSS feeds.
13421343
/// Otherwise, the instance will not provide RSS feeds.
13431344
pub fn enable_rss() -> bool {
1344-
match get_setting("REDLIB_ENABLE_RSS") {
1345-
Some(val) => val == "on",
1346-
None => false,
1347-
}
1345+
*ENABLE_RSS
13481346
}
13491347

13501348
/// Returns true if the config/env variable `REDLIB_ROBOTS_DISABLE_INDEXING` carries the
@@ -1353,10 +1351,7 @@ pub fn enable_rss() -> bool {
13531351
/// If this variable is set as such, the instance will block all robots in robots.txt and
13541352
/// insert the noindex, nofollow meta tag on every page.
13551353
pub fn disable_indexing() -> bool {
1356-
match get_setting("REDLIB_ROBOTS_DISABLE_INDEXING") {
1357-
Some(val) => val == "on",
1358-
None => false,
1359-
}
1354+
*DISABLE_INDEXING
13601355
}
13611356

13621357
/// Determines if a request should redirect to a NSFW landing gate.

0 commit comments

Comments
 (0)